1、引入maven依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <!-- 操作File好用 可选 --> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2、代码实现导出
//列表标题 LinkedHashMap<String, Integer> cellTitle = new LinkedHashMap<>(); cellTitle.put("序号",3000); cellTitle.put("受理编号",9000); cellTitle.put("专业",6000); cellTitle.put("档案",4000); cellTitle.put("文书",4000); cellTitle.put("检材数",4000); cellTitle.put("姓名1",6000); cellTitle.put("接收时间",7000); //内容列表 List<Map<String, Object>> datalist = new ArrayList<>(); Map<String, Object> data = new HashMap<>(); data.put("RN",1); data.put("ACCEPT_NO","J530100-M-202102595"); data.put("IDENTIFY_ITEM_NAME","检验"); data.put("entrNum", 1); data.put("appraNum", null); data.put("evidNum", 3); data.put("TRANSFER_PERSON_NAME","李某"); data.put("UPDATE_DATE","2021年8月20日"); datalist.add(data); //创建HSSFWorkbook工作薄对象 HSSFWorkbook workbook=new HSSFWorkbook(); //创建HSSFSheet对象 HSSFSheet sheet=workbook.createSheet("sheet1"); //设置单元格标题宽度 Integer titleIndex = 0; for (String key : cellTitle.keySet()){ sheet.setColumnWidth(titleIndex,cellTitle.get(key)); titleIndex++; } HSSFFont font = workbook.createFont(); font.setFontName("宋体"); //加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints(new Short("13")); HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//上下居中 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //设置标识内容,创建行的单元格,从0开始 HSSFRow row = sheet.createRow(0); row.setHeight(new Short("600")); Integer cellTitleIndex = 0; for (String key : cellTitle.keySet()){ HSSFCell cell = row.createCell(cellTitleIndex); cell.setCellStyle(style); cell.setCellValue(key); cellTitleIndex++; } HSSFFont fontData = workbook.createFont(); fontData.setFontName("宋体"); //加粗 fontData.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); fontData.setFontHeightInPoints(new Short("13")); HSSFCellStyle styleData = workbook.createCellStyle(); styleData.setFont(fontData); styleData.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中 styleData.setAlignment(HSSFCellStyle.ALIGN_CENTER);//上下居中 styleData.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 styleData.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 styleData.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 styleData.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //设置背景白色 styleData.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleData.setFillForegroundColor(HSSFColor.WHITE.index); //开始渲染记录数据 for (int i = 0; i < datalist.size(); i++) { HSSFRow rowData = sheet.createRow(i+1); rowData.setHeight(new Short("600")); Map<String, Object> dataMap = datalist.get(i); HSSFCell cell = rowData.createCell(0); cell.setCellStyle(styleData); cell.setCellValue(Objects.toString(dataMap.get("RN"))); HSSFCell cell1 = rowData.createCell(1); cell1.setCellStyle(styleData); cell1.setCellValue(Objects.toString(dataMap.get("ACCEPT_NO"))); HSSFCell cell2 = rowData.createCell(2); cell2.setCellStyle(styleData); cell2.setCellValue(Objects.toString(dataMap.get("IDENTIFY_ITEM_NAME"))); HSSFCell cell3 = rowData.createCell(3); cell3.setCellStyle(styleData); if(Objects.nonNull(dataMap.get("entrNum"))){ cell3.setCellValue("√"); }else{ cell3.setCellValue(""); } HSSFCell cell4 = rowData.createCell(4); cell4.setCellStyle(styleData); if(Objects.nonNull(dataMap.get("appraNum"))){ cell4.setCellValue("√"); }else{ cell4.setCellValue(""); } HSSFCell cell5 = rowData.createCell(5); cell5.setCellStyle(styleData); cell5.setCellValue(Objects.toString(dataMap.get("evidNum"))); HSSFCell cell6 = rowData.createCell(6); cell6.setCellStyle(styleData); cell6.setCellValue(Objects.toString(dataMap.get("TRANSFER_PERSON_NAME"))); HSSFCell cell7 = rowData.createCell(7); cell7.setCellStyle(styleData); cell7.setCellValue(Objects.toString(dataMap.get("UPDATE_DATE"))); } //创建文档信息 workbook.createInformationProperties(); //将文件存到浏览器设置的下载位置 String path = "E:\"; String filename = System.currentTimeMillis()+".xls"; try { OutputStream out = new FileOutputStream(path+filename); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); }
如果是web请求下载导出,可以使用流转换方式:
//将文件存到浏览器设置的下载位置 String filename = "xxxx"+new SimpleDateFormat("yyyy年MM月dd日HH点mm分").format(new Date()).toString()+".xls"; response.setContentType("application/ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8")))); OutputStream os = response.getOutputStream(); try { workbook.write(os);// 将数据写出去 os.flush(); String str = "导出" + filename + "成功!"; System.out.println(str); } catch (Exception e) { e.printStackTrace(); String str1 = "导出" + filename + "失败!"; System.out.println(str1); } finally { os.close(); }