From 0f5be71e006bcea1a7e02f069c155bc62b293991 Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Thu, 8 Aug 2019 13:16:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=BB=E5=86=99=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../write/longestMatchColumnWidthWrite.png | Bin 0 -> 6702 bytes quickstart.md | 51 ++++++++++++++++++ .../excel/context/WriteContextImpl.java | 4 +- .../easyexcel/test/demo/write/WriteTest.java | 3 ++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 img/readme/quickstart/write/longestMatchColumnWidthWrite.png diff --git a/img/readme/quickstart/write/longestMatchColumnWidthWrite.png b/img/readme/quickstart/write/longestMatchColumnWidthWrite.png new file mode 100644 index 0000000000000000000000000000000000000000..c8a06049062bb982f35903212ff1834a918ab3a9 GIT binary patch literal 6702 zcmZu#2T+q;vqm8rsz3k{DWMp;AP}S}34~7QRVe`hAqa>F(xgNMqDYq#1Oie+FH)pK zs3J{}Q2c00k)|Ms6uB>c|GhK!&V4hPIlJfVo_%(o-MkaLk%1N?9Ty!H6&0h7Hp+yG ziUvnTb%qs84QOiFW8MM6l99fd#=y!Dg+gg-YXkHvD=TATV?8}RYWZp@DJdipDJ(3k ztt|`z>`_6fs1SgFA+{0!ssMFc8xhc9^p1EqpOp)s~7 z3Z=D;hzTRML7@ngZvh5_2@Au7VW0?1*ucO5B8*tziz+QGZ6k(JK@njHC<0-xrlwXv zp#V0uwY8K2Uj*<#shXRcD=I252q>jBh8PwGoW%fY1g5&Wx|Kp962n?!Y$=oilr6A} zK%snnQ79s@jZ7xn#sC2j|DJ}vd-ra5co>L)h(W}(Qu_M(pqQ}1!NIW81ArRgRNm9k z(eXH1Haa@m7UgSXWW9m^b7WKax%5(8Kw5L7)q$vVMU*)S(xlx=dV>>WSJvYwvDjXl0Gt!8P(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mkqF0$YbkRPDX z!%DipDO;!A@eFbcP$i6!SB?~mVHTOw;R^+S8zb6akw~x#@I3!K3m^|0{2HB{@n0%v zsR7p{6BJ%;%U>YjA7xE1ivkHt#|k)J{k;PW&41^X)Ze)!TMh6lKTa0Q4CRv=S0~|l zmdoZQpJE;5(Ik99^N`IfFd^4(JYod;BRUTauM{iHhX{|8XEPcHZ2_lONq82Yk9=Ci zu%#SAs?2o`K36BfPvPQ`xGG~b&HZ8uXkNe%d7oLG?ldKiGc$4`BI)_#1avXFW zVb}#XbD;Yrp5cZY0m_o_)Jx~mwSk~q#>vj+=yjFrN7Yxx$@d86kQDP%{zqX2lq~-5 zy7F-5pm8h`atgl~cC{Gm_^V;}LUN#CAT@=Bko&|wV7GTQpNZUtEt)xv}(BhK=_Cq^F`VY gr%Q@LBEWlWMo!*<&7uu|I~DNgXc(Z%)G)aJ0)Kgni2wiq literal 0 HcmV?d00001 diff --git a/quickstart.md b/quickstart.md index 4b2d4ac..3a7fd41 100644 --- a/quickstart.md +++ b/quickstart.md @@ -22,6 +22,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja * [合并单元格](#mergeWrite) * [使用table去写入](#tableWrite) * [动态头,实时生成头写入](#dynamicHeadWrite) +* [自动列宽(不太精确)](#longestMatchColumnWidthWrite) * [web中的写](#webWrite) ## 读excel样例 @@ -739,6 +740,56 @@ public class WidthAndHeightData { } ``` +### 自动列宽(不太精确) +##### excel示例 +![img](img/readme/quickstart/write/longestMatchColumnWidthWrite.png) +##### 对象 +```java +@Data +public class LongestMatchColumnWidthData { + @ExcelProperty("字符串标题") + private String string; + @ExcelProperty("日期标题很长日期标题很长日期标题很长很长") + private Date date; + @ExcelProperty("数字") + private Double doubleData; +} +``` +##### 代码 +```java + /** + * 自动列宽(不太精确) + *

+ * 这个目前不是很好用,比如有数字就会导致换行。而且长度也不是刚好和实际长度一致。 所以需要精确到刚好列宽的慎用。 当然也可以自己参照 + * {@link LongestMatchColumnWidthStyleStrategy}重新实现. + *

+ * poi 自带{@link SXSSFSheet#autoSizeColumn(int)} 对中文支持也不太好。目前没找到很好的算法。 有的话可以推荐下。 + * + *

  • 1. 创建excel对应的实体对象 参照{@link DemoData} + *
  • 3. 注册策略{@link LongestMatchColumnWidthStyleStrategy} + *
  • 2. 直接写即可 + */ + @Test + public void longestMatchColumnWidthWrite() { + String fileName = + TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx"; + // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + EasyExcelFactory.write(fileName, LongestMatchColumnWidthData.class).sheet("模板").doWrite(dataLong()); + } + + private List dataLong() { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + LongestMatchColumnWidthData data = new LongestMatchColumnWidthData(); + data.setString("测试很长的字符串测试很长的字符串测试很长的字符串" + i); + data.setDate(new Date()); + data.setDoubleData(1000000000000.0); + list.add(data); + } + return list; + } +``` + ### web中的写 ##### 示例代码 DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java) diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 1833130..ff2215e 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -117,7 +117,9 @@ public class WriteContextImpl implements WriteContext { throw new IllegalArgumentException("Sheet argument cannot be null"); } if (writeSheet.getSheetNo() == null || writeSheet.getSheetNo() <= 0) { - LOGGER.info("Sheet number is null"); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Sheet number is null"); + } writeSheet.setSheetNo(0); } if (writeWorkbookHolder.getHasBeenInitializedSheet().containsKey(writeSheet.getSheetNo())) { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index d105fb7..8b5fae8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -7,6 +7,7 @@ import java.util.List; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.xssf.streaming.SXSSFSheet; import org.junit.Ignore; import org.junit.Test; @@ -243,6 +244,8 @@ public class WriteTest { *

    * 这个目前不是很好用,比如有数字就会导致换行。而且长度也不是刚好和实际长度一致。 所以需要精确到刚好列宽的慎用。 当然也可以自己参照 * {@link LongestMatchColumnWidthStyleStrategy}重新实现. + *

    + * poi 自带{@link SXSSFSheet#autoSizeColumn(int)} 对中文支持也不太好。目前没找到很好的算法。 有的话可以推荐下。 * *

  • 1. 创建excel对应的实体对象 参照{@link DemoData} *
  • 3. 注册策略{@link LongestMatchColumnWidthStyleStrategy}