From b32865ad653e2b7a8045c0be2a5988550c0e42b1 Mon Sep 17 00:00:00 2001 From: znxdgwx <287265173@qq.com> Date: Mon, 26 Apr 2021 17:13:23 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=201?= =?UTF-8?q?=E3=80=81=E9=87=87=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83=E6=A0=BC=EF=BC=8C?= =?UTF-8?q?2=E3=80=81=E8=BE=93=E5=87=BA=E5=A4=8D=E6=9D=82=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E8=A1=A8=E5=A4=B4=E8=A1=8C=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83=E6=A0=BC=E6=97=B6?= =?UTF-8?q?=E8=BE=B9=E6=A1=86=E9=A2=9C=E8=89=B2=E4=B8=A2=E5=A4=B1=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/poi/excel/ExcelWriter.java | 56 +++++++++++++++++++ .../cn/hutool/poi/excel/cell/CellUtil.java | 4 ++ .../cn/hutool/poi/excel/ExcelWriteTest.java | 47 ++++++++++++++++ 3 files changed, 107 insertions(+) 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 ef1aedfcf..d17d51b83 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 @@ -38,6 +38,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @@ -731,6 +732,31 @@ public class ExcelWriter extends ExcelBase { return this; } + /** + * 合并单元格,并写入对象到单元格,使用指定的样式
+ * 指定样式传入null,则不使用任何样式 + * + * @param firstRow 起始行,0开始 + * @param lastRow 结束行,0开始 + * @param firstColumn 起始列,0开始 + * @param lastColumn 结束列,0开始 + * @param content 合并单元格后的内容 + * @param cellStyle 合并后单元格使用的样式,可以为null + * @return this + */ + public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, CellStyle cellStyle){ + Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); + + CellUtil.mergingCells(this.getSheet(), firstRow, lastRow, firstColumn, lastColumn, cellStyle); + + // 设置内容 + if (null != content) { + final Cell cell = getOrCreateCell(firstColumn, firstRow); + CellUtil.setCellValue(cell, content, cellStyle); + } + return this; + } + /** * 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件
* 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动增加
@@ -845,6 +871,36 @@ public class ExcelWriter extends ExcelBase { return this; } + /** + * 写出复杂标题的第二行标题数据
+ * 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
+ * 样式为默认标题样式,可使用{@link #getHeadCellStyle()}方法调用后自定义默认样式 + * + * @param rowData 一行的数据 + * @return this + */ + public ExcelWriter writeSecHeadRow(Iterable rowData){ + final Row row = RowUtil.getOrCreateRow(this.sheet,this.currentRow.getAndIncrement()); + Iterator iterator = rowData.iterator(); + if (row.getLastCellNum() != 0){ + for (int i = 0; i < this.workbook.getSpreadsheetVersion().getMaxColumns(); i++) { + Cell cell = row.getCell(i); + if (cell == null) { + if(iterator.hasNext()){ + cell = row.createCell(i); + CellUtil.setCellValue(cell, iterator.next(), this.styleSet, true); + }else{ + break; + } + } + } + } else { + writeHeadRow(rowData); + } + return this; + } + /** * 写出一行,根据rowBean数据类型不同,写出情况如下: * 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 acdabe0e3..8ebff48c7 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 @@ -361,6 +361,10 @@ public class CellUtil { RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); + RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(),cellRangeAddress,sheet); + RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(),cellRangeAddress,sheet); + RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(),cellRangeAddress,sheet); + RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(),cellRangeAddress,sheet); } return sheet.addMergedRegion(cellRangeAddress); } 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 60ebadc47..33a25fedc 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 @@ -13,6 +13,9 @@ 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.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; @@ -520,4 +523,48 @@ public class ExcelWriteTest { columnStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00")); writer.close(); } + + @Test + @Ignore + public void writeSecHeadRowTest() { + List row1 = CollUtil.newArrayList(1,"aa", "bb", "cc", "dd", "ee"); + List row2 = CollUtil.newArrayList(2,"aa1", "bb1", "cc1", "dd1", "ee1"); + List row3 = CollUtil.newArrayList(3,"aa2", "bb2", "cc2", "dd2", "ee2"); + List row4 = CollUtil.newArrayList(4,"aa3", "bb3", "cc3", "dd3", "ee3"); + List row5 = CollUtil.newArrayList(5,"aa4", "bb4", "cc4", "dd4", "ee4"); + + List> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5); + + // 通过工具类创建writer + ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeSecHeadRowTest.xlsx"); + + CellStyle cellStyle = writer.getWorkbook().createCellStyle(); + cellStyle.setWrapText(false); + cellStyle.setAlignment(HorizontalAlignment.CENTER); + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + //设置标题内容字体 + Font font = writer.createFont(); + font.setBold(true); + font.setFontHeightInPoints((short) 15); + font.setFontName("Arial"); + //设置边框样式 + StyleUtil.setBorder(cellStyle,BorderStyle.THICK,IndexedColors.RED); + cellStyle.setFont(font); + + // 合并单元格后的标题行,使用设置好的样式 + writer.merge(0,1,0,row1.size() - 1, "标题XXXXXXXX",cellStyle); + System.out.println(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); + // 一次性写出内容,使用默认样式 + writer.write(rows); + // 关闭writer,释放内存 + writer.close(); + } } From b923783b345fc78d3791909838d065e5ef9bbb97 Mon Sep 17 00:00:00 2001 From: znxdgwx <287265173@qq.com> Date: Tue, 27 Apr 2021 09:36:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AE=E9=A2=98=20more?= =?UTF-8?q?=20than=203=20if/for/while/switch/try?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/poi/excel/ExcelWriter.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 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 d17d51b83..ae251f6cc 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 @@ -744,7 +744,7 @@ public class ExcelWriter extends ExcelBase { * @param cellStyle 合并后单元格使用的样式,可以为null * @return this */ - public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, CellStyle cellStyle){ + public ExcelWriter merge(int firstRow, int lastRow, int firstColumn, int lastColumn, Object content, CellStyle cellStyle) { Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); CellUtil.mergingCells(this.getSheet(), firstRow, lastRow, firstColumn, lastColumn, cellStyle); @@ -883,16 +883,17 @@ public class ExcelWriter extends ExcelBase { public ExcelWriter writeSecHeadRow(Iterable rowData){ final Row row = RowUtil.getOrCreateRow(this.sheet,this.currentRow.getAndIncrement()); Iterator iterator = rowData.iterator(); - if (row.getLastCellNum() != 0){ + if (row.getLastCellNum() != 0) { for (int i = 0; i < this.workbook.getSpreadsheetVersion().getMaxColumns(); i++) { Cell cell = row.getCell(i); - if (cell == null) { - if(iterator.hasNext()){ - cell = row.createCell(i); - CellUtil.setCellValue(cell, iterator.next(), this.styleSet, true); - }else{ - break; - } + if (cell != null) { + continue; + } + if (iterator.hasNext()) { + cell = row.createCell(i); + CellUtil.setCellValue(cell, iterator.next(), this.styleSet, true); + } else { + break; } } } else { From ee8e7eb6f1f56a4d3056fdd4340fbd975212b35d Mon Sep 17 00:00:00 2001 From: znxdgwx <287265173@qq.com> Date: Tue, 27 Apr 2021 09:47:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java | 1 + 1 file changed, 1 insertion(+) 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 ae251f6cc..e7a9af2d0 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 @@ -883,6 +883,7 @@ public class ExcelWriter extends ExcelBase { public ExcelWriter writeSecHeadRow(Iterable rowData){ final Row row = RowUtil.getOrCreateRow(this.sheet,this.currentRow.getAndIncrement()); Iterator iterator = rowData.iterator(); + //如果获取的row存在单元格,则执行复杂表头逻辑,否则直接调用writeHeadRow(Iterable rowData) if (row.getLastCellNum() != 0) { for (int i = 0; i < this.workbook.getSpreadsheetVersion().getMaxColumns(); i++) { Cell cell = row.getCell(i);