下面是由BigExcelWriter写出文件代码
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
BigExcelWriter bigWriter = ExcelUtil.getBigWriter(file);
// 重命名第一个Sheet的名称,不然会默认多出一个Sheet1的页
bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
for (SheetDtoUtils sheet : sheetList) {
// 指定要写出的 Sheet 页
bigWriter.setSheet(sheet.getSheetName());
// 设置字段和别名
//bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
// 设置只导出有别名的字段
//bigWriter.setOnlyAlias(true);
// 设置默认行高
bigWriter.setDefaultRowHeight(18);
// 设置冻结行
//bigWriter.setFreezePane(1);
// 一次性写出内容,使用默认样式,强制输出标题
bigWriter.write(sheet.getCollection(), true);
// 设置所有列为自动宽度,不考虑合并单元格
bigWriter.autoSizeColumnAll();
}
ServletOutputStream out = null;
try {
//response为HttpServletResponse对象
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" +
URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
out = response.getOutputStream();
bigWriter.flush(out, true);
// 终止后删除临时文件
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭writer,释放内存
bigWriter.close();
}
ExcelUtil读取文件内容代码(如下),这样读取BigExcelWriter 写出的文件是读取不到的,这时候ExcelUtil.readBySax读的是rid
ExcelUtil.readBySax(new FileInputStream(new File(path)), 0, new RowHandler() {
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
});
更改之后的代码(如下),这样第一个sheet文件就能读取出来了,这时候ExcelUtil.readBySax读的是idOrRid
ExcelUtil.readBySax(new FileInputStream(new File(path)), "0", new RowHandler() {
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
});