昨天寫了一篇將Excel中的數據保存到數據庫中的文檔,今天續寫一篇將數據庫中的數據導出成Excel文件的
寫的不好,也是最基礎的,請多多指教
直接貼上代碼:
1.還是一樣先導入pom文件
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
2.將數據導出成Excel文件的Util類
/** * 將數據導出成Excel文件 * * @param sheetName sheet名稱 * @param title 標題 * @param values 內容 * @param wb HSSFWorkbook對象 * @return */ public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) { // 第一步,創建一個HSSFWorkbook,對應一個Excel文件 if (wb == null) { wb = new HSSFWorkbook(); } // 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制 HSSFRow row = sheet.createRow(0); // 第四步,創建單元格,並設置值表頭 設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); // 創建一個居中格式 //設置單元格寬度自適應,在此基礎上把寬度調至1.5倍 for (int i = 0; i < title.length; i++) { sheet.autoSizeColumn(i, true); sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 15 / 10); } // 聲明列對象 HSSFCell cell = null; // 創建標題 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); } // 創建內容 for (int i = 0; i < values.length; i++) { row = sheet.createRow(i + 1); for (int j = 0; j < values[i].length; j++) { // 將內容按順序賦給對應的列對象 row.createCell(j).setCellValue(values[i][j]); } } return wb; }
3.控制層
/** * 導出數據為Excel文件 * 下面注釋的代碼為直接響應到服務器的 * @param request * @param response * @return */ @GetMapping("/exportExcel") public String exportExcel(HttpServletRequest request, HttpServletResponse response) { List<TzJobSet> list = tzJobSetService.list(); String resultName =""; String[] title = {"ID", "標題", "排序", "創建時間"}; // String filename = "jobSet.xls"; String sheetName = "職稱設置表"; String[][] content = new String[list.size()][4]; try { for (int i = 0; i < list.size(); i++) { content[i][0] = String.valueOf(list.get(i).getId()); content[i][1] = list.get(i).getTitle(); content[i][2] = String.valueOf(list.get(i).getSortId()); content[i][3] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(list.get(i).getAddTime()); } } catch (Exception e) { e.printStackTrace(); } HSSFWorkbook wb = ImportExcel.getHSSFWorkbook(sheetName, title, content, null); try { String ctxPath = "D://upFiles"; String name = new SimpleDateFormat("ddHHmmss").format(new Date()); String fileName=name+"jobSet.xlsx"; String bizPath = "files"; String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date()); File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday); if (!file.exists()) { file.mkdirs();// 創建文件根目錄 } String savePath = file.getPath() + File.separator + fileName; resultName = bizPath + File.separator + nowday+ File.separator + fileName; if (resultName.contains("\\")) { resultName = resultName.replace("\\", "/"); } System.out.print(resultName); System.out.print(savePath); // 響應到客戶端需要下面注釋的代碼 // this.setResponseHeader(response, filename); // OutputStream os = response.getOutputStream(); //響應到服務器 OutputStream os = new FileOutputStream(savePath); // 保存到當前路徑savePath wb.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } return resultName; }
下面的代碼是向客戶端發送響應流方法(個人喜歡將文件保存返回地址)
/** * 向客戶端發送響應流方法 * * @param response * @param fileName */ public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); } catch (Exception ex) { ex.printStackTrace(); } }
--------------------------------請多多指教
有問題可加微信聯系我一起學習探討 : 18237185359