若依系统下载固定excel模板并填充数据
@Log(title = "作业底稿", businessType = BusinessType.EXPORT)
@PostMapping("/export")
@ResponseBody
public AjaxResult export(TProjectAuditWordArchives tProjectAuditWordArchives) {
String fileName = "作业底稿模板.xlsx";
String realPath = String.valueOf(getClass().getProtectionDomain().getCodeSource().getLocation()).replace("file:","");
// 文件在项目中路径
String filePath =realPath +"/filetemplate/"+ fileName;
FileInputStream fileInputStream = null;
XSSFWorkbook workBook = null;
OutputStream out = null;
try {
fileInputStream = new FileInputStream(filePath); // 输入流
workBook=new XSSFWorkbook(fileInputStream);
XSSFSheet sheet0 = workBook.getSheetAt(0);
XSSFSheet sheet1 = workBook.getSheetAt(1);
// 写入数据
List<TProjectAuditWordArchives> list = tProjectAuditWordArchivesService.selectTProjectAuditWordArchivesList(tProjectAuditWordArchives);
for (int i = 0; i < list.size(); i++) {
// 索引从0开始,模板中从第4行开始填充数据
XSSFRow row = sheet0.createRow(3 + i);
row.createCell(0).setCellValue(list.get(i).getId());
row.createCell(1).setCellValue(list.get(i).getAuditCode());
row.createCell(2).setCellValue(list.get(i).getAuditMatter());
row.createCell(3).setCellValue(list.get(i).getArchivesId());
row.createCell(4).setCellValue(list.get(i).getAuditProcessRecord());
row.createCell(5).setCellValue(list.get(i).getAuditProcessResult());
}
for (int i = 0; i < 5; i++) {
XSSFRow row = sheet1.createRow(3 + i);
row.createCell(0).setCellValue(i);
row.createCell(1).setCellValue(i);
row.createCell(2).setCellValue(i);
row.createCell(3).setCellValue(i);
row.createCell(4).setCellValue(i);
row.createCell(5).setCellValue(i);
}
ExcelUtil excelUtil = new ExcelUtil(null);
out = new FileOutputStream(excelUtil.getAbsoluteFile(fileName));
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != workBook) {
try {
workBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return AjaxResult.success(fileName);
}