From c984f3555fd5cf2203625a19811b80115ca6efb1 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 9 May 2021 01:04:46 +0800 Subject: [PATCH] add methods --- CHANGELOG.md | 5 +++-- .../main/java/cn/hutool/poi/excel/ExcelWriter.java | 13 ++++++------- .../java/cn/hutool/poi/excel/ExcelWriteTest.java | 9 ++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c824818a3..22cbf130a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ * 【http 】 HttpUtil增加closeCookie方法 * 【core 】 NumberUtil增加方法decimalFormat重载(issue#I3OSA2@Gitee) * 【extra 】 Ftp的remoteVerificationEnabled改为false(issue#I3OSA2@Gitee) -* 【core 】 MaskBit增加掩码反向转换的方法getMaskBit()(pr#1563@Github) -* 【core 】 ReUtil等增加indexOf、delLast等方法(pr#1555@Github) +* 【core 】 MaskBit增加掩码反向转换的方法getMaskBit()(pr#1563@Github) +* 【core 】 ReUtil等增加indexOf、delLast等方法(pr#1555@Github) +* 【poi 】 ExcelWriter增加writeSecHeadRow,增加合并单元格边框颜色样式(pr#318@Gitee) ### 🐞Bug修复 * 【core 】 修复createScheduledExecutor单位不是毫秒的问题(issue#I3OYIW@Gitee) 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 e7a9af2d0..d4abc369b 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 @@ -722,14 +722,8 @@ public class ExcelWriter extends ExcelBase { if (null != this.styleSet) { style = (isSetHeaderStyle && null != this.styleSet.headCellStyle) ? this.styleSet.headCellStyle : this.styleSet.cellStyle; } - CellUtil.mergingCells(this.sheet, firstRow, lastRow, firstColumn, lastColumn, style); - // 设置内容 - if (null != content) { - final Cell cell = getOrCreateCell(firstColumn, firstRow); - CellUtil.setCellValue(cell, content, this.styleSet, isSetHeaderStyle); - } - return this; + return merge(firstRow, lastRow, firstColumn, lastColumn, content, style); } /** @@ -743,6 +737,7 @@ public class ExcelWriter extends ExcelBase { * @param content 合并单元格后的内容 * @param cellStyle 合并后单元格使用的样式,可以为null * @return this + * @since 5.6.5 */ public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, CellStyle cellStyle) { Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); @@ -877,6 +872,10 @@ public class ExcelWriter extends ExcelBase { * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
* 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式 * + *

+ * 此方法的逻辑是:将一行数据写出到当前行,遇到已存在的单元格跳过,不存在的创建并赋值。 + *

+ * * @param rowData 一行的数据 * @return this */ diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java index 33a25fedc..f14b7824b 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java @@ -4,18 +4,19 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.Console; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.poi.excel.style.StyleUtil; +import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; -import org.apache.poi.ss.usermodel.HorizontalAlignment; -import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.util.CellRangeAddressList; import org.junit.Ignore; import org.junit.Test; @@ -553,13 +554,15 @@ public class ExcelWriteTest { // 合并单元格后的标题行,使用设置好的样式 writer.merge(0,1,0,row1.size() - 1, "标题XXXXXXXX",cellStyle); - System.out.println(writer.getCurrentRow()); + Console.log(writer.getCurrentRow()); + //设置复杂表头 writer.merge(2,3,0,0,"序号",true); writer.merge(2,2,1,2,"AABB",true); writer.merge(2,3,3,3,"CCCC",true); writer.merge(2,2,4,5,"DDEE",true); writer.setCurrentRow(3); + List sechead = CollUtil.newArrayList("AA","BB","DD","EE"); writer.writeSecHeadRow(sechead); // 一次性写出内容,使用默认样式