/**
* 导出功能,根据勾选的设备信息导出
*
* @param id
* @param response
*/
@PostMapping("/exportById")
public void exportById(Integer id, String type, String fileName, HttpServletResponse response) throws IOException {
Workbook workbook = new Workbook();
ByteArrayOutputStream out = null;
try {
//清空response
response.reset();
ReportRecordModelJson reportRecordModelJson = reportRecordModelService.getJson(id);
workbook.fromJson(reportRecordModelJson.getModelJson());
//workbook.processTemplate();
if (type.equals("excel")) {
out = new ByteArrayOutputStream();
fileName = fileName + ".xlsx";
response.setContentType("application/vnd.ms-excel;charset=utf-8");
workbook.save(out, SaveFileFormat.Xlsx);
} else if (type.equals("pdf")) {
out = new ByteArrayOutputStream();
//设置response的Header
fileName = fileName + ".pdf";
response.setContentType("application/pdf;charset=utf-8");
//第一页展示到一页上
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getPageSetup().setIsPercentScale(false);
worksheet.getPageSetup().setFitToPagesTall(1);
worksheet.getPageSetup().setFitToPagesWide(1);
workbook.save(out, SaveFileFormat.Pdf);
}
byte[] bytes = out.toByteArray();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// out.close();
} |