Java 把資料庫獲取的資料 列印到Excel表格中
阿新 • • 發佈:2018-11-05
這兩天寫Excel匯出,以前寫過,但現在實在是想不起了,就翻了一下以前的專案程式碼,特此記錄!
//生成檔案路徑 String toPath = rootPath + "cost/tmp/" + fileName; //建立workbook HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(tempPath))); //cell風格、樣式 HSSFCellStyle cellStyle = wb.createCellStyle(); //迴圈list資料,迴圈一次,寫一行資料 for (int i = 0; i < borrowDatList.size(); i++) { //建立行 Row row = dataSheet.createRow(costLen + i + 1); //獲取list資料,轉為map Map map1 = (Map) borrowDatList.get(i); //轉為entry Set<Map.Entry> entry = map1.entrySet(); String[] dept_code = map1.get("dept_code").toString().split("-"); map1.put("DEPT_CODE", dept_code[0]); //準備遍歷 Iterator<Map.Entry> ite = entry.iterator(); int j = 0; //遍歷資料,往Excel表格寫資料 while (ite.hasNext()) { Map.Entry entry1 = ite.next(); entry1.getKey(); String val = Tools.filterNull(entry1.getValue()); Cell cell = row.createCell(j++); cell.setCellValue(val); } } FileOutputStream out = new FileOutputStream(toPath); wb.write(out); out.close();