用EasyPoi导出Excel中单元格图片(线上阿里云):
导出数据需要包含图片
问题描述
提示:这里描述项目中遇到的问题:
数据库中存储图片格式是阿里云(oss)的图片url,导出excel显示图片,如图
代码如下
实体
@Excel(name = "签收单号",width = 30)
private String signingRemark;
@Excel(name = "签收单", width = 40, type = 2 , height = 20,imageType = 2 )
private byte[] uploadSignPhotos;
/**
* 备注
*/
@Excel(name = "备注",width = 30)
private String remark;
工具类(String转byte[])
public class ImageStreamUtil {
public static byte[] image2byte(String path){
// 设置参数
InputStream in = null;
ByteArrayOutputStream byteArrOps = null;
int length;
byte[] buffer = new byte[1024 * 5];
byte[] data = null;
try {
URL url = new URL(path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(1000 * 60);
if (urlConnection.getResponseCode() == 200) {
in = urlConnection.getInputStream();
byteArrOps = new ByteArrayOutputStream();
while ((length = in.read(buffer)) != -1) {
byteArrOps.write(buffer, 0, length);
}
byteArrOps.flush();
data = byteArrOps.toByteArray();
// 下载大小: data.length
}
} catch (Exception e) {
return null;
} finally {
try {
if (byteArrOps != null) {
byteArrOps.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
}
实现类中
byte[] photos= ImageStreamUtil.image2byte(photo);
控制层
ExportParams params = new ExportParams();
params.setTitle("XXX订单");
Map<String,Object> map = new HashMap<>();
map.put(NormalExcelConstants.DATA_LIST, data列表);
map.put(NormalExcelConstants.CLASS, 实体.class);
map.put(NormXXXalExcelConstants.PARAMS, params);
map.put(NormalExcelConstants.FILE_NAME, "XXX订单");
PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
解决方案:
尝试过好几次,最终还是用byte[]形式转换了,大佬们有用String类型的,欢迎交流
一开始用的String图片地址直接转的byte[],一直报错,后来用工具类把oss的url转换成byte[]后,将注解中imageType=2
,就可以导出来了