使用easypoi导出带有图片的excel文件。
使用的版本是4.4.0
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
excel对应的实体类ImageInfo
@Data
@ExcelTarget("ImageInfo")
public class ImageInfo implements Serializable {
@Excel(name = "商品编号", width = 20)
private String code;
@Excel(name = "图片路径", width = 40)
private String imgUrl;
/**
* type =2 该字段类型为图片
* imageType=1 (默认可以不填)表示从file读取,字段类型是个字符串类型,
* 可以用相对路径也可以用绝对路径、绝对路径优先依次获取。
* imageType=2 表示从数据库或者已经读取完毕,字段类型是个字节数组,直接使用,
* 同时image 类型的cell最好设置好宽和高,会百分百缩放到cell那么大,不是原尺寸。
**/
@TableField(exist = false)
@Excel(name = "图片", type = 2, width = 100, imageType = 2)
private byte[] imgFile;
}
导出的方法
@GetMapping("export/img")
public void exportImg(HttpServletResponse response){
ImageInfo imageInfo = new ImageInfo();
imageInfo.setCode("100000003654");
imageInfo.setImgUrl("http://img13.360buyimg.com/n1/jfs/t1/148817/19/10384/93951/5f826b9eEb0d3655e/52d4816c50fd29cb.jpg");
imageInfo.setImgFile(UtilsImage.getNetImgByUrl(imageInfo.getImgUrl()));
ImageInfo imageInfo2 = new ImageInfo();
imageInfo2.setCode("100000003604");
imageInfo2.setImgUrl("http://img13.360buyimg.com/n1/jfs/t1/198314/6/29756/92927/63808035Ea9f936c0/7f3fb34863d5d955.jpg");
imageInfo2.setImgFile(UtilsImage.getNetImgByUrl(imageInfo2.getImgUrl()));
List<ImageInfo> result = new ArrayList<>();
result.add(imageInfo);
result.add(imageInfo2);
String fileName = "img.xls";
ExportParams exportParams = new ExportParams("图片信息", "图片信息");
exportParams.setType(ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ImageInfo.class, list);
Sheet sheetA = workbook.getSheetAt(0);
sheetA.setColumnWidth((short) 2, (short) 50 * 100);
int rows = sheetA.getLastRowNum();
for (int index = 1; index <= rows; index++) {
Row row = sheetA.getRow(index);
row.setHeight((short)((index + 1) * 500));
}
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(outputStream);
} catch (IOException e) {
log.error("导出图片失败");
throw new RuntimeException(e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
工具类
public class UtilsImage {
/**
* 获取网络图片转成字节流
* @param strUrl 完整图片地址
* @return 图片资源数组
*/
public static byte[] getNetImgByUrl(String strUrl) {
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(2 * 1000);
// 通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
// 得到图片的二进制数据
byte[] btImg = readInputStream(inStream);
return btImg;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从输入流中获取字节流数据
* @param inStream 输入流
* @return 图片流
*/
private static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 设置每次读取缓存区大小
byte[] buffer = new byte[1024*10];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}