From cbdba9a0cda2efa09778f58c23ba0fa8b760eede Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Mon, 19 Aug 2019 19:10:54 +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 --- img/readme/quickstart/write/imageWrite.png | Bin 0 -> 9060 bytes quickstart.md | 53 ++++++++++++++++++ .../easyexcel/test/demo/write/ImageData.java | 30 ++++++++++ .../easyexcel/test/demo/write/WriteTest.java | 35 +++++++++++- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 img/readme/quickstart/write/imageWrite.png create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/write/ImageData.java diff --git a/img/readme/quickstart/write/imageWrite.png b/img/readme/quickstart/write/imageWrite.png new file mode 100644 index 0000000000000000000000000000000000000000..c6a0c67a91dc5a7022a8cd04ff727258f34794d0 GIT binary patch literal 9060 zcmY*PtLjTbKlo>-`6>Ly`C679W@Hl+oS*hfI>rESswrZ;_%}G2m$^n zYe%dD0Dvy_vE9@A|GRyMY& zsVQDwUH}|DI5>#;{F#lNoftyY+uOUnvr~($&CbqN(|o9eKsAhLW6u1P%Zo@(}p=&~QLZbQBtg3keDF@%6EJ{#;d6O;ug# z4iAihkv`vB*(-m%xus=xZZ0)7_36{6Mn*;tRTN;HEcCZ%+uGak(_>>}rcd#N!C$|A zt)-=EMPD={)dFdaX1PMgQHMzCW~ks9EG2R zi=svGt1&1z3XY$|AeEKT_yG(CfWfk|KBG_=(X{tCG}_b48>#FSpl@So_0%m=6pdd2 z17I*XtuLiM$r)N`^P3IeFt3Eh{PD_;j#3N`?*^S};Rt}EkjhAyu-^MLQ49`)4^1>a zNZHG)qs-=gnhg$(LZeYyXi+wrW(_=NoEpkXhQ5=V>5p^`l;}D(wwl{oBeLe5r-~!C>#9#mvBGUw+EfMXu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 -16,6 +16,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja * [复杂头写入](#complexHeadWrite) * [重复多次写入](#repeatedWrite) * [日期、数字或者自定义格式转换](#converterWrite) +* [图片导出](#imageWrite) * [根据模板写入](#templateWrite) * [列宽、行高](#widthAndHeightWrite) * [自定义样式](#styleWrite) @@ -537,6 +538,58 @@ public class ConverterData { } ``` +### 图片导出 +##### excel示例 +![img](img/readme/quickstart/write/imageWrite.png) +##### 对象 +```java +@Data +@ContentRowHeight(100) +@ColumnWidth(100 / 8) +public class ImageData { + private File file; + private InputStream inputStream; + /** + * 如果string类型 必须指定转换器,string默认转换成string + */ + @ExcelProperty(converter = StringImageConverter.class) + private String string; + private byte[] byteArray; +} +``` +##### 代码 +```java + /** + * 图片导出 + *
  • 1. 创建excel对应的实体对象 参照{@link ImageData} + *
  • 2. 直接写即可 + */ + @Test + public void imageWrite() throws Exception { + String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx"; + // 如果使用流 记得关闭 + InputStream inputStream = null; + try { + List list = new ArrayList(); + ImageData imageData = new ImageData(); + list.add(imageData); + String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; + // 放入四种类型的图片 实际使用只要选一种即可 + imageData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath))); + imageData.setFile(new File(imagePath)); + imageData.setString(imagePath); + inputStream = FileUtils.openInputStream(new File(imagePath)); + imageData.setInputStream(inputStream); + EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list); + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + } +``` + + ### 根据模板写入 ##### 模板excel示例 参照:[模板excel示例](#simpleReadExcel) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageData.java new file mode 100644 index 00000000..951026cf --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageData.java @@ -0,0 +1,30 @@ +package com.alibaba.easyexcel.test.demo.write; + +import java.io.File; +import java.io.InputStream; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.alibaba.excel.annotation.write.style.ContentRowHeight; +import com.alibaba.excel.converters.string.StringImageConverter; + +import lombok.Data; + +/** + * 图片导出类 + * + * @author Jiaju Zhuang + */ +@Data +@ContentRowHeight(100) +@ColumnWidth(100 / 8) +public class ImageData { + private File file; + private InputStream inputStream; + /** + * 如果string类型 必须指定转换器,string默认转换成string + */ + @ExcelProperty(converter = StringImageConverter.class) + private String string; + private byte[] byteArray; +} 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 baffae64..9bd94677 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 @@ -1,6 +1,7 @@ package com.alibaba.easyexcel.test.demo.write; import java.io.File; +import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -20,6 +21,7 @@ import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; +import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; @@ -117,6 +119,35 @@ public class WriteTest { EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data()); } + /** + * 图片导出 + *
  • 1. 创建excel对应的实体对象 参照{@link ImageData} + *
  • 2. 直接写即可 + */ + @Test + public void imageWrite() throws Exception { + String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx"; + // 如果使用流 记得关闭 + InputStream inputStream = null; + try { + List list = new ArrayList(); + ImageData imageData = new ImageData(); + list.add(imageData); + String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; + // 放入四种类型的图片 实际使用只要选一种即可 + imageData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath))); + imageData.setFile(new File(imagePath)); + imageData.setString(imagePath); + inputStream = FileUtils.openInputStream(new File(imagePath)); + imageData.setInputStream(inputStream); + EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list); + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + } + /** * 根据模板写入 *
  • 1. 创建excel对应的实体对象 参照{@link IndexData} @@ -192,8 +223,7 @@ public class WriteTest { // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写 LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0); // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 - EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板") - .doWrite(data()); + EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data()); } /** @@ -297,4 +327,5 @@ public class WriteTest { } return list; } + }