Spring boot 导出Excel
Html页面: window.location.href="adjectfkController/exportTemplate?adjOrg="+ adjOrg +"&&adjDepart=" +adjDepart +"&&adjSubject="+adjSubject+"&&oaId="+oaId;
@RequestMapping(value="/exportTemplate",method=RequestMethod.GET) public void download(HttpServletResponse response,@RequestParam String adjOrg,@RequestParam String adjSubject, @RequestParam String adjDepart,@RequestParam String oaId) throws IOException{ // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个Excel表单,参数为sheet的名字 HSSFSheet sheet = workbook.createSheet("模板表"); //创建表头 setTitle(workbook, sheet); List<Map<String, Object>> oalist = budgetAdjustService.getOainform(oaId); //数据行,并且设置单元格数据 HSSFRow hssfRow = sheet.createRow(1); for(Map map :oalist) { hssfRow.createCell(0).setCellValue(map.get("adjustType")+""); hssfRow.createCell(1).setCellValue(map.get("applyDate")+""); hssfRow.createCell(2).setCellValue(map.get("processCode")+""); hssfRow.createCell(3).setCellValue(map.get("applyOrganization")+""); hssfRow.createCell(4).setCellValue(map.get("applyDepartment")+""); hssfRow.createCell(5).setCellValue(map.get("flag")+""); } hssfRow.createCell(6).setCellValue(adjOrg); hssfRow.createCell(7).setCellValue(adjDepart); hssfRow.createCell(8).setCellValue(adjSubject); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String fileName = "Template -" + new Date().getTime() + ".xls"; //清空response response.reset(); //设置response的Header response.addHeader("Content-Disposition", "attachment;filename="+ fileName); OutputStream os = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=gb2312"); //将excel写入到输出流中 workbook.write(os); os.flush(); os.close(); } //设置Excel表头 private void setTitle(HSSFWorkbook workbook, HSSFSheet sheet){ HSSFRow row = sheet.createRow(0); //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度 /* sheet.setColumnWidth(0, 0*256); sheet.setColumnWidth(2, 20*256);*/ sheet.setColumnWidth(7, 60*256); //设置为居中加粗 HSSFCellStyle style = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setBold(true); style.setFont(font); //导出的Excel头部字段 String[] headers = { "调整类型", "申请日期", "OA流程编号", "申请组织","申请部门","申请科目","是否涉及人力成本", "调出组织","调出部门","调出科目","调出月份","调出金额","调入组织","调入部门","调入科目","调入月份","调入金额","查询费控系统"}; // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 18); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); cell.setCellStyle(style); } }
posted on 2018-10-08 00:47 Steven5007 阅读(320) 评论(0) 编辑 收藏 举报