用jExcel API 实现在jsp中把数据导入Excel中,怎么才能让Excel在线打开或作为附件下载?
还要请教各位高手:
别人给我一个用java Excel API 写的javabean实现在jsp页面中将读取的数据库数据导入excel中,并且让该Excel 文件作为附件让客户在IE中下载这个excel,或者让用户在线打开Excel. 但我在jsp中直接调用时却出现乱码,而且不能下载: 想请教各位高手是不是javabean的问题,还是要在resin中进行配置?
请各位高手详细指点,谢谢!
package test;
import java.util.Collection;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Iterator;
import jxl.*;
import jxl.write.*;
import jxl.SheetSettings;
import jxl.format.PaperSize;
import java.util.Vector;
public class ExcelBean {
public void exoportToExcel(Collection colData, HttpServletResponse response) throws
Exception {
if (colData == null || colData.size() == 0) {
throw new Exception("no record");
}
response.setContentType("application/vnd.ms-excel");
//在线打开
response.addHeader("Content-Disposition","inline;filename=Report.rm");
//作为附件
//response.addHeader("Content-Disposition", "attachment;filename=Report.xls");
OutputStream out = response.getOutputStream();
WritableWorkbook wb = Workbook.createWorkbook(out);
wb.setColourRGB(Colour.BLUE, 100, 300, 100);
/*以下是字体以及背景色的设置
*/
// Font : Arial, BOLD, 12size
WritableFont fmtTitle = new WritableFont(WritableFont.ARIAL);
fmtTitle.setBoldStyle(WritableFont.BOLD);
fmtTitle.setPointSize(12);
// Font: Arial, Bold, 10size
WritableFont fmtCaption = new WritableFont(WritableFont.ARIAL);
fmtCaption.setBoldStyle(WritableFont.BOLD);
fmtCaption.setPointSize(WritableFont.DEFAULT_POINT_SIZE);
// CellFormat: for Table Title
WritableCellFormat fmtCenterTitle = new WritableCellFormat(fmtTitle);
fmtCenterTitle.setAlignment(Alignment.CENTRE);
// CellFormat: for each Table Column Caption
WritableCellFormat fmtCenterCaption = new WritableCellFormat(fmtCaption);
fmtCenterCaption.setAlignment(Alignment.CENTRE);
fmtCenterCaption.setBackground(Colour.BLUE); //设置背景色
//红色字体
WritableFont fntRedData = new WritableFont(WritableFont.ARIAL);
fntRedData.setBoldStyle(WritableFont.BOLD);
fntRedData.setPointSize(WritableFont.DEFAULT_POINT_SIZE);
fntRedData.setColour(Colour.RED);
WritableCellFormat fmtRedData = new WritableCellFormat(fntRedData);
fmtRedData.setAlignment(Alignment.CENTRE);
fmtRedData.setBackground(Colour.YELLOW);
//绿色字体
WritableFont fntGreenData = new WritableFont(WritableFont.ARIAL);
fntGreenData.setBoldStyle(WritableFont.BOLD);
fntGreenData.setPointSize(WritableFont.DEFAULT_POINT_SIZE);
//fntGreenData.setColour(Colour.GREEN);
WritableCellFormat fmtGreenData = new WritableCellFormat(fntGreenData);
//fmtGreenData.setAlignment(Alignment.CENTRE);
//SheetSettings set=new SheetSettings();
// set.setPassword("123");
//System.out.println("modifiing");
// set.setPaperSize(PaperSize.A3);
//set.setPageStart(0);
//定义一个sheet
WritableSheet ws = wb.createSheet("sheet1", 0);
SheetSettings set = new SheetSettings();
set.setPageStart(1);
set.setPaperSize(PaperSize.A3);
set.setZoomFactor(50);
set.getPaperSize();
//ws.setPageSetup();
// 定义每一列的宽度
ws.setColumnView(0, 20);
ws.setColumnView(1, 10);
ws.setColumnView(2, 15);
ws.setColumnView(3, 20);
//CellFormat cellFormat=//new XFRecord ();
// ws.setColumnView(0,);
// ws.setColumnView();
ws.setColumnView(1, 10, fmtRedData);
// Define Table Title
Label lblTitle = new Label(0, 0, "学生成绩报表", fmtCenterTitle);
ws.mergeCells(0, 0, 4, 0);
ws.addCell(lblTitle);
// Define each Column Caption in Table
Label lblColumn = null;
//jxl.write.Number numColumn = null;
lblColumn = new Label(0, 2, "姓名", fmtCenterCaption);
ws.addCell(lblColumn);
lblColumn = new Label(1, 2, "学号", fmtCenterCaption);
ws.addCell(lblColumn);
lblColumn = new Label(2, 2, "性别", fmtCenterCaption);
ws.addCell(lblColumn);
lblColumn = new Label(3, 2, "成绩", fmtCenterCaption);
ws.addCell(lblColumn);
int intRow = 3;
for (Iterator it = colData.iterator(); it.hasNext(); intRow++) {
String[] strModel = (String[]) it.next();
if (strModel[0] == null) {
strModel[0] = "";
}
if (strModel[1] == null) {
strModel[1] = "";
}
if (strModel[2] == null) {
strModel[2] = "";
}
if (strModel[3] == null) {
strModel[3] = "";
}
lblColumn = new Label(0, intRow, strModel[0]);
ws.addCell(lblColumn);
lblColumn = new Label(1, intRow, strModel[1]);
ws.addCell(lblColumn);
lblColumn = new Label(2, intRow, strModel[2]);
ws.addCell(lblColumn);
lblColumn = new Label(3, intRow, strModel[3]);
ws.addCell(lblColumn);
}
wb.write();
wb.close();
}
}
请大家帮我看看: