java excel 导出慢的问题

shenchengwoshi shenchengwoshi 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
现在要做一个excel下载的功能,下载之前要把excel的列编辑之后再下载,我直接BufferedOutputStream的write下载特别慢,求大神帮忙看看。怎么解决。。

HttpServletResponse response = ServletActionContext.getResponse();
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
String path = "E:/BDC/SrcModule/obis-web/src/excel/财务费用.xls";

String fileName = URLEncoder.encode("财务费用明细表.xls", "UTF-8");
File f = new File(path);
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
response.setHeader("Content-Length", String.valueOf(f.length()));
in = new BufferedInputStream(new FileInputStream(f));

// 创建Excel工作薄
Workbook work = new HSSFWorkbook(in);
Sheet sheet = work.getSheet("财务费用");

for (int i = 0; i <= sheet.getLastRowNum(); i++) {
if (i == 0 || i == 2 || i == 3) {
continue;
} else if (i == 1 || i == 4 || i == 5 || i == 6 || i == 7
|| i == 8) {

if (i == 1) {
Row row = sheet.getRow((short) i);

String gsmc = (String) httpSession.getAttribute("gsmc");

Cell gsmcCell = row.getCell((short) 2);
gsmcCell.setCellValue("编制单位:" + gsmc);

Cell yuefenCell = row.getCell((short) 6);
yuefenCell.setCellValue(nf + "年" + yuefen + "月");
} else {
Row row = sheet.getRow((short) i);

Cell cell1 = row.getCell((short) 1);
Cell cell4 = row.getCell((short) 6);

cell1.setCellValue(listcaiwufeiyongInfoBean.get(i - 4)
.getSybqlj());
cell4.setCellValue(listcaiwufeiyongInfoBean.get(i - 4)
.getTqlj());
}
} else {
continue;
}
}

out = new BufferedOutputStream(response.getOutputStream());

work.write(out);

} catch (Exception e) {
logger.error("下载模版异常:", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
logger.error("下载模版异常:", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
logger.error("下载模版异常:", e);
}
}
}
...全文
评论 2
收藏 已收藏
分享
返回顶部
shenchengwoshi
Java EE 67,535 成员 225,843 内容