poi操作
創建一個excel關聯對象HSSFWorkbook:
HSSFWorkbook book = new HSSFWorkbook();
創建一個sheet:
HSSFSheet st = book.createSheet("sheet1");
創建第i行:
HSSFRow row = st.createRow(i);
創建第i行的j列:
HSSFCell cell = row.createCell(j);
設置cell屬性
給單元格設置邊框屬性:
HSSFCellStyle style = book.createCellStyle(); // 左右上下邊框樣式 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 左右上下邊框顏色(深藍色),只有設置了上下左右邊框以后給邊框設置顏色才會生效 style.setLeftBorderColor(HSSFColor.BLACK.index); style.setRightBorderColor(HSSFColor.BLACK.index); style.setTopBorderColor(HSSFColor.BLACK.index); style.setBottomBorderColor(HSSFColor.BLACK.index);
給單元格設置背景:
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 設置了背景色才有效果 style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
給單元格設置字體:
// 單元格字體 HSSFFont font = book.createFont(); font.setFontName("宋體");
設置字體以后,需要把字體加入到style中:
style.setFont(font);
設置好單元格屬性以后,需要這種屬性的單元格就可以調用此style:
cell.setCellStyle(style);
設置sheet表單的列寬:
st.setColumnWidth(i, cellWidths.get(i).intValue() * 160);
列寬的設置方法在HSSFSheet中,方法參數:第一個參數表示第幾列,從0開始數;第二個參數表示寬度為多少,大小由使用者調整。
合並單元格:
st.addMergedRegion(new CellRangeAddress(0, 1, 0, keys.size() - 1));
單元格合並方法也是在HSSFSheet中,方法參數:一個CellRangeAddress,該類構造函數的4個參數分別表示為:合並開始行,合並結束行,合並開始列,合並結束列
注:
合並方法最好寫在最后面,不然有可能會影響到某些單元格添加單元格屬性的操作
下面是我寫的一個根據傳入的數據,把數據導出到excel的接口:
/** * 導出到excel 導出的路徑以及導出文件名稱在配置文件中定義 * 在任何地方此方法可以作為組件調用的,只需要提供需要保存的數據,每一列的屬性,以及對應的中文名稱,每一列的寬度,文件路徑,文件名稱 * * @param list * the data which will be saved to excel * @param keys * the key of the column * @param cnames * the name described in Chinese * @param cellWidths * the width of olumns * @param excelPath * the path of excel * @param fileName * the name of the file in server */ public HSSFWorkbook doExportResults(List<Map<String, Object>> list, List<String> keys, List<String> cnames, List<Integer> cellWidths, String excelPath, String fileName) { File excel = new File(excelPath);// 創建文件 String sheetName = fileName.substring(0, fileName.lastIndexOf(".")); String dateStr = fileName.substring(fileName.indexOf("_") + 1, fileName.lastIndexOf(".")); HSSFWorkbook book = new HSSFWorkbook();// 創建excel HSSFSheet st = book.createSheet(sheetName); // 第一行,標題 HSSFRow row = st.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue(sheetName); // 單元格屬性 第一行的屬性在最后設置 HSSFCellStyle style = book.createCellStyle(); // 左右上下邊框樣式 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 左右上下邊框顏色(深藍色) style.setLeftBorderColor(HSSFColor.BLACK.index); style.setRightBorderColor(HSSFColor.BLACK.index); style.setTopBorderColor(HSSFColor.BLACK.index); style.setBottomBorderColor(HSSFColor.BLACK.index); // 背景 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 設置了背景色才有效果 style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); // 單元格字體 HSSFFont font = book.createFont(); font.setFontName("宋體"); style.setFont(font); // 第二行,日期 row = st.createRow(2); cell = row.createCell(0); cell.setCellValue("導出日期"); cell = row.createCell(1); cell.setCellValue(dateStr); //為日期行設置單元格屬性 for (int i = 0; i < keys.size(); i++) { if (row.getCell(i) != null) { cell = row.getCell(i); } else { cell = row.createCell(i); } cell.setCellStyle(style); } // 第三行,表頭 row = st.createRow(3); for (int i = 0; i < keys.size(); i++) { cell = row.createCell(i); cell.setCellValue(cnames.get(i)); cell.setCellStyle(style); st.setColumnWidth(i, cellWidths.get(i).intValue() * 160); } for (int i = 0; i < list.size(); i++) {// 創建每一行數據 row = st.createRow(4 + i); Map<String, Object> tmp = list.get(i); if (tmp == null || tmp.isEmpty()) { continue; } for (int j = 0; j < keys.size(); j++) {// 設置每一行的每一個單元格的值 cell = row.createCell(j); cell.setCellStyle(style); Object obj = tmp.get(keys.get(j)); if (obj == null) { cell.setCellValue(""); } else { cell.setCellValue(obj.toString()); } } } // 合並單元格 HSSFCellStyle s1 = book.createCellStyle(); s1.cloneStyleFrom(style); s1.setAlignment(HSSFCellStyle.ALIGN_CENTER); s1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); s1.setWrapText(true); s1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 設置了背景色才有效果 s1.setFillForegroundColor(HSSFColor.BROWN.index); HSSFFont fo = book.createFont(); fo.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); fo.setFontHeight((short) 350); fo.setFontName("宋體"); s1.setFont(fo); st.getRow(0).getCell(0).setCellStyle(s1); st.addMergedRegion(new CellRangeAddress(0, 1, 0, keys.size() - 1)); st.addMergedRegion(new CellRangeAddress(2, 2, 1, keys.size() - 1)); // 創建表格結束 FileOutputStream out = null; try { if (!excel.getParentFile().exists()) { System.out.println(excel.getParentFile().mkdirs()); } if (!excel.exists()) { System.out.println(excel.createNewFile()); } out = new FileOutputStream(excel); book.write(out);// 把excel寫入到本地文件 } catch (FileNotFoundException e) { logger.error("導出到文件時找不到文件:" + e.getMessage()); } catch (IOException e) { logger.error("導出到文件時輸出流錯誤:" + e.getMessage()); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { logger.error("導出到文件時關閉輸出流錯誤:" + e.getMessage()); } } return book; }