假如已这样的表头导出数据
1.把包含表头的excel添加到项目资源目录
2.编写代码读取表头所在sheet,并且加入需导出的数据
/**
* 导出excel
*/
public static void downloadExcel(List<List<Object>> list, HttpServletResponse response) throws IOException {
/*String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);*/
//获取模板文件第一个sheet
ClassPathResource resource = new ClassPathResource("cadreAndCompany.xls");
ExcelReader reader = ExcelUtil.getReader(resource.getStream());
Sheet rows = reader.getSheets().get(0);
BigExcelWriter bigExcelWriter = new BigExcelWriter(rows);
bigExcelWriter.passRows(3);//跳过表头
// 一次性写出内容,使用默认样式,强制输出标题
bigExcelWriter.write(list);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
ServletOutputStream out=response.getOutputStream();
// 终止后删除临时文件
//file.deleteOnExit();
bigExcelWriter.flush(out, true);
//此处记得关闭输出Servlet流
IoUtil.close(out);
}