【发布时间】:2014-02-22 11:16:23
【问题描述】:
我想使用 Apache poi 将 PNG 图像插入到我的 Excel 工作表中。
为此,我使用以下代码:
//add picture data to this workbook.
InputStream is = new FileInputStream("/sdcard/MYAPPFOLDER/logo_app.png");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
CreationHelper helper = workbook.getCreationHelper();
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setCol1(0);
anchor.setRow1(0);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize();
但第一次我遇到了一个错误,我通过添加这个库commons-codec-1.8.jar 解决了这个错误,现在我遇到了这个错误:
02-21 10:10:51.466: E/AndroidRuntime(31691): FATAL EXCEPTION: main
02-21 10:10:51.466: E/AndroidRuntime(31691): java.lang.NoClassDefFoundError: java.awt.Dimension
02-21 10:10:51.466: E/AndroidRuntime(31691): at org.apache.poi.ss.util.ImageUtils.getImageDimension(ImageUtils.java:52)
02-21 10:10:51.466: E/AndroidRuntime(31691): at org.apache.poi.hssf.usermodel.HSSFPicture.getImageDimension(HSSFPicture.java:243)
02-21 10:10:51.466: E/AndroidRuntime(31691): at org.apache.poi.hssf.usermodel.HSSFPicture.getPreferredSize(HSSFPicture.java:163)
哪个指向这条线:
pict.resize();
我该如何解决?
【问题讨论】:
-
你找到解决这个问题的方法了吗?
标签: android excel image apache-poi