使用 Apache-poi 将图像插入 excel [英] insert image into excel with Apache-poi
问题描述
好吧,我修改了我的代码以消除其他因素:
包com.shangzhu.drt;导入 org.apache.poi.ss.usermodel.Picture;导入 org.apache.poi.ss.usermodel.Workbook;导入 org.apache.poi.xssf.usermodel.*;导入 javax.imageio.ImageIO;导入 java.awt.image.BufferedImage;导入 java.io.ByteArrayOutputStream;导入 java.io.File;导入 java.io.FileOutputStream;导入 java.io.OutputStream;/*** 由 lixiaoming 于 2017/6/26 创建.*/公共类 ImageTest2 {私有静态无效 insertImageWithPOI() 抛出异常 {XSSFWorkbook wwb = new XSSFWorkbook();XSSFSheet ws = wwb.createSheet("sheet0");BufferedImage image = ImageIO.read(new File("D:/poi.png"));ByteArrayOutputStream baps = new ByteArrayOutputStream();ImageIO.write(image,"png",baps);int pictureIdx = wwb.addPicture(baps.toByteArray(), Workbook.PICTURE_TYPE_PNG);XSSFDrawing 绘图 = ws.createDrawingPatriarch();XSSFCreationHelper helper = wwb.getCreationHelper();XSSFClientAnchor 锚点 = helper.createClientAnchor();锚点.setCol1(1);锚点.setRow1(1);图片图片=drawing.createPicture(anchor,pictureIdx);图片.调整大小();File excelFile = new File("D:/POI.xlsx");OutputStream ops = new FileOutputStream(excelFile);wwb.write(ops);}公共静态无效主(字符串 [] args){尝试 {insertImageWithPOI();} 捕获(异常 e){e.printStackTrace();}}}
下面是代码中的图片("D:/poi.png"):D:/poi.png
我不认为处理图像的源代码有问题,但我不知道我错过了什么
我确认使用默认列大小时有问题.XSSFPicture.resize
需要计算以像素为单位的列宽以获取 XSSFClientAnchor
Col2
和 Dx2
.只要使用默认列大小,那么这个计算似乎是错误的.
解决方法可能是在使用 XSSFPicture.resize
之前定义明确的列大小.那么以像素为单位的列宽计算似乎是正确的.
在您的代码中:
<预><代码>...图片图片=drawing.createPicture(anchor,pictureIdx);for (int c=0; c<20; c++) ws.setColumnWidth(c, 11*256);图片.调整大小();...well,I modified my code to eliminate other factors:
package com.shangzhu.drt;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by lixiaoming on 2017/6/26.
*/
public class ImageTest2 {
private static void insertImageWithPOI() throws Exception {
XSSFWorkbook wwb = new XSSFWorkbook();
XSSFSheet ws = wwb.createSheet("sheet0");
BufferedImage image = ImageIO.read(new File("D:/poi.png"));
ByteArrayOutputStream baps = new ByteArrayOutputStream();
ImageIO.write(image,"png",baps);
int pictureIdx = wwb.addPicture(baps.toByteArray(), Workbook.PICTURE_TYPE_PNG);
XSSFDrawing drawing = ws.createDrawingPatriarch();
XSSFCreationHelper helper = wwb.getCreationHelper();
XSSFClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize();
File excelFile = new File("D:/POI.xlsx");
OutputStream ops = new FileOutputStream(excelFile);
wwb.write(ops);
}
public static void main(String[] args) {
try {
insertImageWithPOI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
below is the picture("D:/poi.png") in the code: D:/poi.png
I don't think the source code which is dealing image has problems,But I don't know what I missed
I confirm that there is a problem when default column size is used. XSSFPicture.resize
needs calculating the column widths in pixels to get the XSSFClientAnchor
Col2
and the Dx2
. As long as default column size is used, then this calculation seems to be wrong.
A workaround could be defining explicit column sizes before using XSSFPicture.resize
. Then the calculation of the column widths in pixels seems to be correct.
In your code:
...
Picture picture = drawing.createPicture(anchor, pictureIdx);
for (int c=0; c<20; c++) ws.setColumnWidth(c, 11*256);
picture.resize();
...
这篇关于使用 Apache-poi 将图像插入 excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!