Map<String,Object> result = new HashMap<>();
SXSSFWorkbook wb = new SXSSFWorkbook(500);
//hssf是03版,xssf是07版的,但是这个07版的有内存泄露的问题,sxssf是该版本的改进版,可以解决内存溢出,就是导出的格式稍有变化.xlsx
CellStyle cellStyle =wb.createCellStyle();
cellStyle.setWrapText(true); //设置为文本格式,后边可以设置单元格的文本自动换行
SXSSFsheet sheet = wb.createSheet();
wb.setSheetName(0,"活动表");
sheet.setColumnWidth(1,20*256);
SXSSFRow row0 = sheet.create(0);
row0.createCell(0).setCellValue("活动编码");
row0.createCell(1).setCellValue("活动名称");
for(i){
//从数据库中取数据
SXSSFRow row = sheet.create(i);
row.createCell(0).setCellValue();
row.createCell(1).setCellValue();
//将样式设置到具体的单元格
row.getCell(1).setCellStyle(cellStyle);
}
String fileName = "活动表";
try{
outputStream = httpServletResponse.getOutputStream();
httpServletResponse.reset();
httpServletResponse.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(fileName,"utf-8"));
httpServletResponse.setContentType("application/msexcel");
httpServletResponse.setCharacterEncoding("utf-8");
wb.write(outputStream);
}catch(IOEXCEPTION e)(
e.printStackTrace;
)catch(EXCEPTION e)(
result.put("msg",e);
}finally{
try{
outputStream.flush(); //每500条数据从内存中写入到Excel一次
outputStream.close();
}catch(IOEXCEPTION e){
e.printStackTrace;
}
}
return result;
POI导出案例
最新推荐文章于 2023-02-06 14:30:10 发布