用jExcel API 实现在jsp中把数据导入Excel中,怎么才能让Excel在线打开或作为附件下载?

wangnanwn 2004-03-31 04:42:19
还要请教各位高手:
别人给我一个用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();
}
}

请大家帮我看看:

...全文
313 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangnanwn 2004-04-03
  • 打赏
  • 举报
回复
不好意思,上面是写错了
response.addHeader("Content-Disposition","inline;filename=Report.xls");
locust2002 2004-04-02
  • 打赏
  • 举报
回复
我也碰到这个问题
wangnanwn 2004-04-02
  • 打赏
  • 举报
回复
不是上面楼主说的注释掉的问题,有两种方式打开excel,一种是在线打开,一种是作为附件让用户下载的.

各位高手快帮忙啊!比较急啊!
CoolAbu 2004-04-02
  • 打赏
  • 举报
回复
response.addHeader("Content-Disposition","inline;filename=Report.rm");
你如果用这个来设置Header,那你有Report.rm文件么?它是干吗的?
CoolAbu 2004-04-01
  • 打赏
  • 举报
回复
response.addHeader("Content-Disposition","inline;filename=Report.rm");
//作为附件
//response.addHeader("Content-Disposition", "attachment;filename=Report.xls");

你干吗要把下面的注释掉呀?Excel的应该是把上面的注释掉,而不是把下面的注释掉。
101monster 2004-04-01
  • 打赏
  • 举报
回复
呵呵,UP!
mu_gong 2004-04-01
  • 打赏
  • 举报
回复
我遇到的是这样的,如果有这么个EXCEL文件,如果需要在页面显示,就在头部加上
<..........content type='vns-excel'..>具体我忘记了,然后,就可以在此JSP页面显示EXCEL表,然后用户可以选择 文件-》另存为。。。。等等,

直接下载???好像没有必要吧,那还不如在上个页面写个连接EXCEL的下载连接得了
wangnanwn 2004-04-01
  • 打赏
  • 举报
回复
没有人回啊,自己up!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧