diff --git a/img/readme/quickstart/write/imageWrite.png b/img/readme/quickstart/write/imageWrite.png new file mode 100644 index 00000000..c6a0c67a Binary files /dev/null and b/img/readme/quickstart/write/imageWrite.png differ diff --git a/quickstart.md b/quickstart.md index 42b0ea2c..28b8bf30 100644 --- a/quickstart.md +++ b/quickstart.md @@ -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; } + }