From b6e260a2f0d5c9ade660e06655d1f4438085905e Mon Sep 17 00:00:00 2001 From: hower Date: Sun, 28 May 2023 22:50:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=B3=BB=E5=88=97?= =?UTF-8?q?=E6=96=B9=E6=B3=95writeCol=EF=BC=8C=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8C=89=E5=88=97=E8=BE=93=E5=87=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/poi/excel/ExcelWriter.java | 81 +++++++++++++++++++ .../cn/hutool/poi/excel/ExcelWriteTest.java | 31 +++++++ 2 files changed, 112 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 e52d87a5f..fbb5bf0d1 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -1096,6 +1096,87 @@ public class ExcelWriter extends ExcelBase { return this; } + /** + * 从第1列开始按列写入数据(index 从0开始)
+ * 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
+ * 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式 + * + * @param colMap 一列的数据 + * @param isWriteKeyAsHead 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values + * @return this + */ + public ExcelWriter writeCol(Map colMap, boolean isWriteKeyAsHead){ + return writeCol(colMap, 0, isWriteKeyAsHead); + } + + /** + * 从指定列开始按列写入数据(index 从0开始)
+ * 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
+ * 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式 + * + * @param colMap 一列的数据 + * @param startColIndex 起始的列号,从0开始 + * @param isWriteKeyAsHead 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values + * @return this + */ + public ExcelWriter writeCol(Map colMap, int startColIndex, boolean isWriteKeyAsHead){ + for (Object k : colMap.keySet()) { + Object v = colMap.get(k); + if(v instanceof Iterable){ + writeCol(isWriteKeyAsHead?k:null,startColIndex, (Iterable) v, startColIndex != colMap.size() - 1); + startColIndex ++; + } + } + return this; + } + + + /** + * 为第一列写入数据
+ * 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
+ * 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式 + * + * @param headerVal 表头名称,如果为null则不写入 + * @param colData 需要写入的列数据 + * @param isResetRowIndex 如果为true,写入完毕后Row index 将会重置为写入之前的未知,如果为false,写入完毕后Row index将会在写完的数据下方 + * @return this + */ + public ExcelWriter writeCol(Object headerVal, Iterable colData, boolean isResetRowIndex){ + return writeCol(headerVal,0,colData,isResetRowIndex); + } + + /** + * 为第指定列写入数据
+ * 本方法只是将数据写入Workbook中的Sheet,并不写出到文件
+ * 写出的起始行为当前行号,可使用{@link #getCurrentRow()}方法调用,根据写出的的行数,当前行号自动+1
+ * 样式为默认样式,可使用{@link #getCellStyle()}方法调用后自定义默认样式 + * + * @param headerVal 表头名称,如果为null则不写入 + * @param colIndex 列index + * @param colData 需要写入的列数据 + * @param isResetRowIndex 如果为true,写入完毕后Row index 将会重置为写入之前的未知,如果为false,写入完毕后Row index将会在写完的数据下方 + * @return this + */ + public ExcelWriter writeCol(Object headerVal, int colIndex, Iterable colData, boolean isResetRowIndex){ + Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); + int currentRowIndex = currentRow.get(); + if(null != headerVal){ + writeCellValue(colIndex, currentRowIndex, headerVal,true); + currentRowIndex++; + } + for (Object colDatum : colData) { + writeCellValue(colIndex, currentRowIndex, colDatum); + currentRowIndex++; + } + if(!isResetRowIndex){ + currentRow.set(currentRowIndex); + } + return this; + } + /** * 给指定单元格赋值,使用默认单元格样式,默认不是Header * 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 ab47b91b9..07d6980a1 100755 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java @@ -885,4 +885,35 @@ public class ExcelWriteTest { final String disposition = writer.getDisposition("测试A12.xlsx", CharsetUtil.CHARSET_UTF_8); Assert.assertEquals("attachment; filename=\"%E6%B5%8B%E8%AF%95A12.xlsx\"", disposition); } + + @Test + @Ignore + public void writeColTest(){ + ExcelWriter writer = ExcelUtil.getWriter("G:\\test.xlsx"); + List list= new ArrayList<>(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + writer.writeCol("header1",0,list,true); + writer.writeCol("header2",1,list,false); + writer.writeCol("header3",0,list,true); + writer.writeCol("header4",1,list,false); + + Map> map1 = new LinkedHashMap<>(); + map1.put("map1_header1",list); + map1.put("map1_header2",list); + map1.put("map1_header3",list); + map1.put("map1_header4",list); + + Map> map2 = new LinkedHashMap<>(); + map2.put("map2_header1",list); + map2.put("map2_header2",list); + map2.put("map2_header3",list); + map2.put("map2_header4",list); + + writer.writeCol(map1,true); + writer.writeCol(map2,false); + + writer.close(); + } } From decda097e2f0aa3d13c73339781840a939c3766f Mon Sep 17 00:00:00 2001 From: hower Date: Mon, 29 May 2023 00:09:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/poi/excel/ExcelWriter.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 fbb5bf0d1..3f67bb596 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -1106,7 +1106,7 @@ public class ExcelWriter extends ExcelBase { * @param isWriteKeyAsHead 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values * @return this */ - public ExcelWriter writeCol(Map colMap, boolean isWriteKeyAsHead){ + public ExcelWriter writeCol(Map> colMap, boolean isWriteKeyAsHead){ return writeCol(colMap, 0, isWriteKeyAsHead); } @@ -1121,11 +1121,11 @@ public class ExcelWriter extends ExcelBase { * @param isWriteKeyAsHead 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values * @return this */ - public ExcelWriter writeCol(Map colMap, int startColIndex, boolean isWriteKeyAsHead){ + public ExcelWriter writeCol(Map> colMap, int startColIndex, boolean isWriteKeyAsHead){ for (Object k : colMap.keySet()) { - Object v = colMap.get(k); - if(v instanceof Iterable){ - writeCol(isWriteKeyAsHead?k:null,startColIndex, (Iterable) v, startColIndex != colMap.size() - 1); + Iterable v = colMap.get(k); + if(v != null){ + writeCol(isWriteKeyAsHead?k:null,startColIndex, v, startColIndex != colMap.size() - 1); startColIndex ++; } }