通过jsp导入导出数据到Excel文档?该怎么做?

发布网友 发布时间:2022-04-23 19:46

我来回答

2个回答

懂视网 时间:2022-04-21 03:49

请大家帮个忙啊
----------------
我使用jsp导出excel, 在jsp页面写的java代码把数据取出来,然后导出excel文件。
现在出现的问题是,如果数量少的时候还可以。但如果大量数据比如5万条记录的话,数据可以取到jsp页面上,但是页面就会一直卡住,无法导出excel,大家知道怎么解决吗


回复讨论(解决方案)

为什么非要在jsp上写java代码,很麻烦的。一般的做法是把取数据的条件提交到后台,然后从数据库里重新取一边,用POI操作Excel,生出完了,再导出来。

这事情应该交给数据库做

热心网友 时间:2022-04-21 00:57

public static void crExcel(List list, String absoluteFileString)
throws Exception {

// 字体格式
WritableFont wfc0 = new WritableFont(WritableFont.ARIAL, 22,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormat wcfFC0 = new WritableCellFormat(wfc0);
wcfFC0.setAlignment(jxl.format.Alignment.CENTRE);
wcfFC0.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfFC0.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25);
// 表头字体
WritableFont wfc8 = new WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormat wcfFC8 = new WritableCellFormat(wfc8);
wcfFC8.setAlignment(jxl.format.Alignment.CENTRE);
wcfFC8.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfFC8.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25);
WritableFont wfc1 = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormat wcfFC1 = new WritableCellFormat(wfc1);
wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
wcfFC1.setBorder(Border.ALL, BorderLineStyle.THIN,jxl.format.Colour.GRAY_25);

// 创建excel
OutputStream os = new FileOutputStream(absoluteFileString);

String readPath = I18nMessages.getText("efine.excel.dir");

InputStream input = new FileInputStream(readPath
+ "FKBalanceReport.xls");

Workbook workbook = Workbook.getWorkbook(input);
WritableWorkbook book = Workbook.createWorkbook(os, workbook);
WritableSheet sheet = book.getSheet(0);

// 数据写入
Label label = null;
// Title
//label = new jxl.write.Label(3, 0, "到期付款信息表", wcfFC0);
//sheet.addCell(label);
/*
* label = new jxl.write.Label(0, 3, "备注", wcfFC4);
* sheet.addCell(label);
*/

label = new jxl.write.Label(0, 0, "到期日", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(1, 0, "部门名称", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(2, 0, "币种", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(3, 0, "银行", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(4, 0, "金额", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(5, 0, "业务类型", wcfFC8);
sheet.addCell(label);
label = new jxl.write.Label(6, 0, "摘要", wcfFC8);
sheet.addCell(label);
sheet.setName("到期付款信息查询");

int j = 1;
for (int i = 0; i <list.size(); i++) {
DNCM02 dto = (DNCM02) list.get(i);

label = new Label(0, i + j, CommonUtil.dateToStr(dto.getEndDay()),
wcfFC1);
sheet.addCell(label);
label = new Label(1, i + j, dto.getEndDeptName(), wcfFC1);
sheet.addCell(label);
label = new Label(2, i + j, dto.getCurrencyName(), wcfFC1);
sheet.addCell(label);
label = new Label(3, i + j, dto.getEndBankName(), wcfFC1);
sheet.addCell(label);
label = new Label(4, i + j, CommonUtil.formatString(dto
.getEndAmount().toString()), wcfFC1);
sheet.addCell(label);
label = new Label(5, i + j, dto.getEndTypeName(), wcfFC1);
sheet.addCell(label);
label = new Label(6, i + j, dto.getRemark(), wcfFC1);
sheet.addCell(label);

}

book.write();
book.close();
os.close();
}

先看看 不会再说 参数 list 是你查询结果,另一个是excel路径

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com