问题描述
在使用easypoi 模板方式导出图片excel 遇到了一个很奇怪的问题我参考教程去写了一个demo 发现设置宽高方式我怎么都看不到图片,而设置图片占用多少行就能够显示图片 ?
问题出现的环境背景及自己尝试过哪些方法
相关代码
参考代码
@Test
public void one() throws Exception {
TemplateExportParams params = new TemplateExportParams(
"doc/exportTemp_image.xls", true);
Map<String, Object> map = new HashMap<String, Object>();
// sheet 2
map.put("month", 10);
Map<String, Object> temp;
for (int i = 1; i < 8; i++) {
temp = new HashMap<String, Object>();
temp.put("per", i * 10);
temp.put("mon", i * 1000);
temp.put("summon", i * 10000);
ImageEntity image = new ImageEntity();
image.setHeight(200);
image.setWidth(500);
image.setUrl("imgs/company/baidu.png");
temp.put("image", image);
map.put("i" + i, temp);
}
Workbook book = ExcelExportUtil.exportExcel(params, map);
File savefile = new File("D:/excel/");
if (!savefile.exists()) {
savefile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/excel/exportTemp_image.xls");
book.write(fos);
fos.close();
}
我的尝试
@Test
public void test() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("template/excel/test1.xls");
TemplateExportParams params = new TemplateExportParams(classPathResource.getPath(), true);
Map<String, Object> map = new HashMap<>();
/*生成二维码图片byte[]*/
byte[] bytes = QrCodeUtil.generatePng("测试内容", 50, 50);
byte[] bytes1 = QrCodeUtil.generatePng("测试内容2", 50, 50);
/*设置宽高方式*/
ImageEntity image = new ImageEntity();
image.setWidth(100);
image.setHeight(100);
image.setData(bytes1);
/*设置占有多少行列方式*/
ImageEntity imageEntity = new ImageEntity();
imageEntity.setData(bytes);
imageEntity.setRowspan(10);
imageEntity.setColspan(4);
map.put("phone", imageEntity);
map.put("content", "测试内容");
map.put("picture", image);
map.put("title", "测试2");
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
File file = new File("C:\\Users\\x1522\\Desktop\\download\\test11.xls");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);
}
模板
效果
这是为啥,是我图片本身的大小设置问题吗?但是同样是50*50 的大小相同的图片为啥一个能显示出来而另一个无法显示,还有一个问题就是 ImageEntity 中的设置 width,height 是控制图片的什么参数呢?
由于之前没有使用过poi 和 easypoi 我对他们都不了解请帮我解答一下谢谢
应该是你的URL有问题,width 是图片宽度,height是图片高度