Browse Source

优化读写逻辑

bugfix
zhuangjiaju 5 years ago
parent
commit
cbdba9a0cd
  1. BIN
      img/readme/quickstart/write/imageWrite.png
  2. 53
      quickstart.md
  3. 30
      src/test/java/com/alibaba/easyexcel/test/demo/write/ImageData.java
  4. 35
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

BIN
img/readme/quickstart/write/imageWrite.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

53
quickstart.md

@ -16,6 +16,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
* [复杂头写入](#complexHeadWrite) * [复杂头写入](#complexHeadWrite)
* [重复多次写入](#repeatedWrite) * [重复多次写入](#repeatedWrite)
* [日期、数字或者自定义格式转换](#converterWrite) * [日期、数字或者自定义格式转换](#converterWrite)
* [图片导出](#imageWrite)
* [根据模板写入](#templateWrite) * [根据模板写入](#templateWrite)
* [列宽、行高](#widthAndHeightWrite) * [列宽、行高](#widthAndHeightWrite)
* [自定义样式](#styleWrite) * [自定义样式](#styleWrite)
@ -537,6 +538,58 @@ public class ConverterData {
} }
``` ```
### <span id="imageWrite" />图片导出
##### 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
/**
* 图片导出
* <li>1. 创建excel对应的实体对象 参照{@link ImageData}
* <li>2. 直接写即可
*/
@Test
public void imageWrite() throws Exception {
String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
// 如果使用流 记得关闭
InputStream inputStream = null;
try {
List<ImageData> list = new ArrayList<ImageData>();
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();
}
}
}
```
### <span id="templateWrite" />根据模板写入 ### <span id="templateWrite" />根据模板写入
##### 模板excel示例 ##### 模板excel示例
参照:[模板excel示例](#simpleReadExcel) 参照:[模板excel示例](#simpleReadExcel)

30
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;
}

35
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.demo.write; package com.alibaba.easyexcel.test.demo.write;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; 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.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight; 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.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteTable;
@ -117,6 +119,35 @@ public class WriteTest {
EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data());
} }
/**
* 图片导出
* <li>1. 创建excel对应的实体对象 参照{@link ImageData}
* <li>2. 直接写即可
*/
@Test
public void imageWrite() throws Exception {
String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
// 如果使用流 记得关闭
InputStream inputStream = null;
try {
List<ImageData> list = new ArrayList<ImageData>();
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();
}
}
}
/** /**
* 根据模板写入 * 根据模板写入
* <li>1. 创建excel对应的实体对象 参照{@link IndexData} * <li>1. 创建excel对应的实体对象 参照{@link IndexData}
@ -192,8 +223,7 @@ public class WriteTest {
// 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写 // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0); LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板") EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());
.doWrite(data());
} }
/** /**
@ -297,4 +327,5 @@ public class WriteTest {
} }
return list; return list;
} }
} }

Loading…
Cancel
Save