匯出excel表工具
阿新 • • 發佈:2021-09-06
public static void main(String[] args) throws Exception{ String[] headers = {"學號"}; List<Object[]> dataset = new ArrayList<Object[]>(); dataset.add(new Object[]{"1"}); dataset.add(new Object[]{"2"}); dataset.add(new Object[]{"3"}); dataset.add(new Object[]{"4"}); dataset.add(new Object[]{"5"}); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd"); String format = dateFormat.format(date); String title = "測試POI匯出EXCEL文件"; String path = "D:/"; exportMessageExcelFile(title, headers, dataset,null, format); } /** * 匯出excel表 * * @param title 表格名稱 * @param headers excel列名 ['',''] String陣列 * @param dossierList excel資料 list<Object> * @param out 資料流 * @param pattern * @return */ public static Map<String, Object> exportMessageExcelFile(String title, String[] headers, List dossierList, String path, String pattern) { OutputStream out= null; try { if(StringUtils.isEmpty(path)){ path = "D:/fiscal/"; } File file = new File(path); if (!file.exists()) {// 判斷目錄是否存在 file.mkdir(); } out = new FileOutputStream(path + title + ".xls",true); } catch (Exception e) { e.printStackTrace(); } boolean flag = true; Map<String, Object> map = new HashMap<String, Object>(); StringBuffer messageFile = new StringBuffer(); // 宣告一個工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(title); // 設定表格預設列寬度為15個位元組 sheet.setDefaultColumnWidth((short) 15); // 生成一個樣式 HSSFCellStyle style = workbook.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //產生表格標題行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } for (int i = 0; i < dossierList.size(); i++) { Object[] obj = (Object[]) dossierList.get(i); row = sheet.createRow(i + 1); for (int j = 0; j < obj.length; j++) { HSSFCell cell = row.createCell(j); cell.setCellStyle(style); if (j == 0) { cell.setCellValue(i + 1);//序號 } if (j == 1) { //Logger.debug("obj[5]"+obj[5]); cell.setCellValue(obj[5] == null ? "" : obj[5].toString());//辦理人 } if (j == 2) { //Logger.debug("obj[3]"+obj[3]); cell.setCellValue(obj[3] == null ? "" : obj[3].toString());//辦理時間 } if (j == 3) { // Logger.debug("obj[2]"+obj[2]); cell.setCellValue(obj[2] == null ? obj[6] == null ? "" : obj[6].toString() : obj[2].toString());//辦理意見 if (null != obj[6] && !"".equals(obj[6])) { messageFile.append(obj[6].toString() + ","); } break; } } } map.put("messageFile", messageFile.toString().endsWith(",") ? messageFile.toString().substring(0, messageFile.toString().length() - 1) : messageFile.toString()); try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); flag = false; } finally { //清理資源 try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } map.put("flag", flag); return map; }