1.Maven需要的依賴
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>3.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.14</version> </dependency>
2.導入示例
1 /** 2 * 黑名單查詢 3 * @param blackListBean 4 * @return 5 */ 6 @RequestMapping(value = "/blackList", method = RequestMethod.POST) 7 @ResponseBody 8 public BaseReturnModel blackList( 9 @RequestBody BlackListBean blackListBean 10 ) { 11 BaseReturnModel baseReturnModel = new BaseReturnModel(); 12 List<BlackListBean> resultList = null; 13 int total = 0; 14 try { 15 if (blackListBean.getDownLoad() == null) { 16 int start = (blackListBean.getPagination() - 1) * blackListBean.getRownum(); // 分頁處理 //rownum頁數 17 blackListBean.setStart(start); 18 } 19 if (blackListBean.getUserIds() != null && !"".equals(blackListBean.getUserIds())) { 20 blackListBean.setIdUsers(blackListBean.getUserIds().split(",")); 21 } 22 resultList = blackListSerivce.selectBlackList(blackListBean); 23 total = blackListSerivce.selectResult(blackListBean); 24 25 } catch (Exception e) { 26 // TODO: handle exception 27 e.printStackTrace(); 28 baseReturnModel.setCode(200);// 狀態 29 baseReturnModel.setMess("系統錯誤"); 30 } 31 if (blackListBean.getDownLoad() != null) { 32 if (resultList.size() > 0) { 33 //創建一個表格 34 SXSSFWorkbook workbook = new SXSSFWorkbook(); 35 //創建一個工作表 36 SXSSFSheet sheet = workbook.createSheet("sheet1"); 37 //設置表頭行 38 SXSSFRow xssfRow = sheet.createRow(0); 39 //設置單元格格式居中 40 CellStyle cellStyle = workbook.createCellStyle(); 41 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); 42 //添加表頭內容 43 SXSSFCell headCell = xssfRow.createCell(0); 44 headCell.setCellValue("站點ID"); 45 headCell.setCellStyle(cellStyle); 46 // 單元格列寬 0代表列位置 47 sheet.setColumnWidth(0, 5500); 48 sheet.setColumnWidth(1, 5500); 49 sheet.setColumnWidth(2, 5500); 50 sheet.setColumnWidth(3, 5500); 51 sheet.setColumnWidth(4, 5500); 52 53 //設置每列的參數 54 headCell = xssfRow.createCell(1); 55 headCell.setCellValue("站點名稱"); 56 headCell.setCellStyle(cellStyle); 57 58 headCell = xssfRow.createCell(2); 59 headCell.setCellValue("黑名單手機號/單號"); 60 headCell.setCellStyle(cellStyle); 61 62 headCell = xssfRow.createCell(3); 63 headCell.setCellValue("理由"); 64 headCell.setCellStyle(cellStyle); 65 66 headCell = xssfRow.createCell(4); 67 headCell.setCellValue("時間"); 68 headCell.setCellStyle(cellStyle); 69 70 71 int b = 1; 72 String s = JSON.toJSONString(resultList); 73 SimpleDateFormat sdf = new SimpleDateFormat 74 ("yyyy-MM-dd HH:mm:ss"); 75 List<BlackListBean> list = JSON.parseArray(s, BlackListBean.class); 76 for (BlackListBean obj : list) { 77 xssfRow = sheet.createRow(b++); 78 79 //時間戳轉換 80 Long aLong = new Long(obj.getCreateTime()); 81 Date date = new Date(aLong); 82 83 //創建單元格,並添加值 84 SXSSFCell cell = xssfRow.createCell(0); 85 cell.setCellValue(obj.getIdUser()); 86 cell.setCellStyle(cellStyle); 87 88 cell = xssfRow.createCell(1); 89 cell.setCellValue(obj.getName()); 90 cell.setCellStyle(cellStyle); 91 92 cell = xssfRow.createCell(2); 93 cell.setCellValue(obj.getMobile()); 94 cell.setCellStyle(cellStyle); 95 96 cell = xssfRow.createCell(3); 97 cell.setCellValue(obj.getNote()); 98 cell.setCellStyle(cellStyle); 99 100 cell = xssfRow.createCell(4); 101 cell.setCellValue(sdf.format(date)); 102 cell.setCellStyle(cellStyle); 103 } 104 //保存文件 105 String imgpath = "/usr/local/tomcat/apache-tomcat-8.5.32/webapps/excel/"; 106 File file = new File(imgpath); 107 if (!file.exists()) { 108 file.mkdirs(); 109 } 110 try { 111 String imgFilePath = imgpath + "blackList" + ".xlsx"; 112 OutputStream outputStream = new FileOutputStream(imgFilePath); 113 baseReturnModel.setObj("/excel/" + "blackList" + ".xlsx"); 114 baseReturnModel.setCode(200); 115 baseReturnModel.setMess("正在下載"); 116 117 try { 118 workbook.write(outputStream); 119 } catch (IOException e) { 120 e.printStackTrace(); 121 baseReturnModel.setCode(500); 122 baseReturnModel.setMess("寫入失敗"); 123 baseReturnModel.setObj(""); 124 } 125 126 try { 127 outputStream.close(); 128 workbook.close(); 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 133 } catch (FileNotFoundException e) { 134 e.printStackTrace(); 135 baseReturnModel.setCode(500); 136 baseReturnModel.setMess("下載失敗"); 137 } 138 return baseReturnModel; 139 } else { 140 baseReturnModel.setCode(500); 141 baseReturnModel.setMess("沒有可提供下載得內容"); 142 return baseReturnModel; 143 } 144 } 145 baseReturnModel.setCode(200);// 狀態 146 baseReturnModel.setMess("成功"); 147 baseReturnModel.setObj(resultList); 148 baseReturnModel.setTotal(total); 149 return baseReturnModel; 150 }