自己写的demo
public Result exportDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
PageInfo<AdmsTableLevelAnalyzeVO> pageInfo = service.queryByPage(null, new AdmsTableLevelAnalyzeVO());
List<AdmsTableLevelAnalyzeVO> list = pageInfo.getList();
if(list == null || list.size() < 1) {
return null;
}
// 清除首部的空白行
response.reset();
// 告诉浏览器这是一个要保存到本地的下载的文件
response.setContentType("application/x-msdownload");
// 向server发送http请求确认该资源是否有修改,有的话返回200,无的话返回304
response.setHeader("Cache-Control", "max-age=0");
// 告诉浏览器不要参与处理,而是要客户自己来处理该文件
response.setHeader("Content-Disposition", "attachment;filename=tableLvlTask.xls");
// 字节输出流,可以将java程序中的数据写到文件中
OutputStream out = response.getOutputStream();
String templateFilePath = "src/main/resources/template/tableTemplate.xls";
// 文档对象
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(templateFilePath));
// 第一个表单
HSSFSheet sheet = workbook.getSheetAt(0);
AdmsTableLevelAnalyzeVO vo = null;
for(int i = 0; i < list.size(); i++) {
vo = list.get(i);
// 增加行,模板中已经有两行了,所以从第三行开始
HSSFRow row = sheet.createRow(i+2);
HSSFCell cell = row.createCell(0);
cell.setCellValue(vo.getBdpSrcTableTrunkName());
cell = row.createCell(2);
cell.setCellValue(vo.getSrcSysIdent());
}
workbook.write(out);
out.flush();
out.close();
return ResultGenerator.genSuccessResult();
}