@ApiLog("报告导出") @ApiOperation(value = "报告导出") @GetMapping("exportReport") public Map<String,Object> exportReport(String id, HttpServletResponse response) { Map<String,Object> resultMp = new HashMap<>(); FileInputStream is = null; Workbook wb = null; try { // 文件地址 在 Resource 目录下 Resource resource = new ClassPathResource("template/xxx.xlsx"); boolean isFile = resource.isFile(); //如果不存在返回 if(!isFile){ resultMp.put("code",1); resultMp.put("msg","文件不存在"); return resultMp; } String path = resource.getFile().getPath(); //获取文件路径 File fi = new File(path); is = new FileInputStream(fi); wb = WorkbookFactory.create(is); // 设置 sheet 名称 wb.setSheetName(0,"实验报告"); Sheet sheet1 = wb.getSheetAt(0); // 遍历设置列的的宽度 for (int i = 0; i < 8; i++) { sheet1.setColumnWidth(i, 18*256);//设置当前sheet页第一列宽度 } // // sheet1.setColumnWidth(0, 15*256);//设置当前sheet页第一列宽度 // sheet1.setColumnWidth(1, 15*256);//第二列宽度 // sheet1.setColumnWidth(1, 15*256);//第二列宽度 // sheet1.setColumnWidth(1, 15*256);//第二列宽度 // sheet1.setColumnWidth(1, 15*256);//第二列宽度 // sheet1.setColumnWidth(1, 15*256);//第二列宽度 CellStyle cellStyle = wb.createCellStyle(); //下边框 cellStyle.setBorderBottom(BorderStyle.THIN); //左边框 cellStyle.setBorderLeft(BorderStyle.THIN); //上边框 cellStyle.setBorderTop(BorderStyle.THIN); //右边框 cellStyle.setBorderRight(BorderStyle.THIN); //增加水平居中样式 cellStyle.setAlignment(HorizontalAlignment.CENTER); //增加垂直居中样式 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //自动换行样式增加 cellStyle.setWrapText(true);
// 创建行 Row dataRow = sheet1.createRow( startRow + 4 +j);
// 行上创建cell
dataRow.createCell(0).setCellStyle(cellStyle); dataRow.createCell(1).setCellStyle(cellStyle); dataRow.createCell(2).setCellStyle(cellStyle); dataRow.createCell(3).setCellStyle(cellStyle); dataRow.createCell(4).setCellStyle(cellStyle); dataRow.createCell(5).setCellStyle(cellStyle); // 获取行数并赋值 dataRow.getCell(0).setCellValue(dataList.get(j).get("xxxx").toString().trim()); dataRow.getCell(1).setCellValue(dataList.get(j).get("xxxx").toString().trim()); dataRow.getCell(2).setCellValue(dataList.get(j).get("xxxx").toString().trim()); dataRow.getCell(3).setCellValue(dataList.get(j).get("xxxx").toString().trim()); dataRow.getCell(4).setCellValue(dataList.get(j).get("xxxx").toString().trim()); dataRow.getCell(5).setCellValue(dataList.get(j).get("xxxx").toString().trim());
// response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(pName+"xxx"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx")); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); //设置编码 response.setCharacterEncoding("utf-8"); wb.write(response.getOutputStream()); wb.close(); is.close(); resultMp.put("code",0); resultMp.put("msg","下载xx成功"); } catch (Exception e) { e.printStackTrace(); resultMp.put("code",1); resultMp.put("msg","下载xxx失败"); }finally { try { if (wb != null) { wb.close(); } if (is != null) { is.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return resultMp; }