圖中紅框框是處理單元格內容和批注的地方。
參考:https://blog.csdn.net/qq_38974638/article/details/114837631
//SXSSFWorkbook public static void createExcelMergeSetCellColor1(SXSSFWorkbook workbook, SXSSFSheet sheet, String title, final String[] headers, List<List<String>> dataset, boolean isSortDataSet, final Integer[] mergeBasis, final Integer[] mergeCells, final Integer[] sumCells, final Integer[] timeCells) { sheet.setDefaultColumnWidth(15); // 設置表格默認列寬度為15個字節 sheet.setDefaultRowHeight((short)20); sheet.setDefaultRowHeightInPoints(20); CellStyle headStyle = createHeadStyle(workbook); // 生成頭部樣式 CellStyle commonDataStyle = createCommonDataStyle(workbook); // 生成一般數據樣式 commonDataStyle.setWrapText(true); CellStyle commonDataStyleRed = createCommonDataStyleRed(workbook);//生成紅色文本樣式 commonDataStyleRed.setWrapText(true); CellStyle numStyle = createNumStyle(workbook); //生成數字類型保留兩位小數樣式 CellStyle sumRowStyle = createSumRowStyle(workbook); //生成合計行樣式 if (headers == null || headers.length <= 0) { return; } SXSSFDrawing drawing = sheet.createDrawingPatriarch(); SXSSFRow row = sheet.createRow(0); // 產生表格標題行 row.setHeightInPoints(30); //標題行的行高為25 for (int index = 0; index < headers.length; index++) { SXSSFCell cell = row.createCell(index); cell.setCellStyle(headStyle); XSSFRichTextString text = new XSSFRichTextString(headers[index]); cell.setCellValue(text); String str = text.toString(); if(str.contains("雇員唯一號") || str.contains("雇員姓名") || str.contains("證件號") || str.contains("繳金地") || str.contains("社保額")) { Comment comment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 5, cell.getRowIndex() + 6)); comment.setString(new XSSFRichTextString("必填項")); cell.setCellComment(comment); } else if(str.contains("證件類型")) { Comment comment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 5, cell.getRowIndex() + 6)); comment.setString(new XSSFRichTextString("必填項,證件類型只支持 身份證")); cell.setCellComment(comment); } else if(str.contains("賬單年月")) { Comment comment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 5, cell.getRowIndex() + 6)); comment.setString(new XSSFRichTextString("必填項,日期格式:\n" + "202109\n")); cell.setCellComment(comment); } } // 遍歷集合數據,產生數據行 Iterator<List<String>> it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); row.setHeightInPoints(20); //一般數據的行高為20 List<String> dataSources = it.next(); for (int i = 0; i < dataSources.size(); i++) { SXSSFCell cell = row.createCell(i); cell.setCellStyle(commonDataStyle); cell.setCellValue(dataSources.get(i)); } } }