From c3eefb614cb4c942224f37edf9c7829c160b2581 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 22 Apr 2021 00:23:58 +0800 Subject: [PATCH] add methods --- .../java/cn/hutool/poi/excel/ExcelWriter.java | 13 +++++ .../java/cn/hutool/poi/excel/RowUtil.java | 12 ++++- .../cn/hutool/poi/excel/cell/CellUtil.java | 51 ++++++++++++++----- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index f682866b4..ef1aedfcf 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -1033,6 +1033,19 @@ public class ExcelWriter extends ExcelBase { return this; } + /** + * 设置列的默认样式 + * + * @param x 列号,从0开始 + * @param style 样式 + * @return this + * @since 5.6.4 + */ + public ExcelWriter setColumnStyle(int x, CellStyle style){ + this.sheet.setDefaultColumnStyle(x, style); + return this; + } + /** * 创建字体 * diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/RowUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/RowUtil.java index 42096c938..d9ebe75c2 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/RowUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/RowUtil.java @@ -85,12 +85,22 @@ public class RowUtil { return cellValues; } + /** + * 写一行数据,无样式,非标题 + * + * @param row 行 + * @param rowData 一行的数据 + */ + public static void writeRow(Row row, Iterable rowData) { + writeRow(row, rowData, null, false); + } + /** * 写一行数据 * * @param row 行 * @param rowData 一行的数据 - * @param styleSet 单元格样式集,包括日期等样式 + * @param styleSet 单元格样式集,包括日期等样式,null表示无样式 * @param isHeader 是否为标题行 */ public static void writeRow(Row row, Iterable rowData, StyleSet styleSet, boolean isHeader) { diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java index 0179caea3..acdabe0e3 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java @@ -148,7 +148,7 @@ public class CellUtil { * * @param cell 单元格 * @param value 值 - * @param styleSet 单元格样式集,包括日期等样式 + * @param styleSet 单元格样式集,包括日期等样式,null表示无样式 * @param isHeader 是否为标题单元格 */ public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) { @@ -166,25 +166,21 @@ public class CellUtil { } } - if (value instanceof Date) { - if (null != styleSet && null != styleSet.getCellStyleForDate()) { - cell.setCellStyle(styleSet.getCellStyleForDate()); - } - } else if (value instanceof TemporalAccessor) { - if (null != styleSet && null != styleSet.getCellStyleForDate()) { - cell.setCellStyle(styleSet.getCellStyleForDate()); - } - } else if (value instanceof Calendar) { + if (value instanceof Date + || value instanceof TemporalAccessor + || value instanceof Calendar) { + // 日期单独定义格式 if (null != styleSet && null != styleSet.getCellStyleForDate()) { cell.setCellStyle(styleSet.getCellStyleForDate()); } } else if (value instanceof Number) { + // 数字单独定义格式 if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) { cell.setCellStyle(styleSet.getCellStyleForNumber()); } } - setCellValue(cell, value, null); + setCellValue(cell, value); } /** @@ -205,6 +201,23 @@ public class CellUtil { cell.setCellStyle(style); } + setCellValue(cell, value); + } + + /** + * 设置单元格值
+ * 根据传入的styleSet自动匹配样式
+ * 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置 + * + * @param cell 单元格 + * @param value 值 + * @since 5.6.4 + */ + public static void setCellValue(Cell cell, Object value) { + if (null == cell) { + return; + } + if (null == value) { cell.setCellValue(StrUtil.EMPTY); } else if (value instanceof FormulaCellValue) { @@ -318,7 +331,21 @@ public class CellUtil { * @param lastRow 结束行,0开始 * @param firstColumn 起始列,0开始 * @param lastColumn 结束列,0开始 - * @param cellStyle 单元格样式,只提取边框样式 + * @return 合并后的单元格号 + */ + public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn) { + return mergingCells(sheet, firstRow, lastRow, firstColumn, lastColumn, null); + } + + /** + * 合并单元格,可以根据设置的值来合并行和列 + * + * @param sheet 表对象 + * @param firstRow 起始行,0开始 + * @param lastRow 结束行,0开始 + * @param firstColumn 起始列,0开始 + * @param lastColumn 结束列,0开始 + * @param cellStyle 单元格样式,只提取边框样式,null表示无样式 * @return 合并后的单元格号 */ public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) {