我想使用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();
我该如何解决?
答案 0 :(得分:2)
您不能使用涉及图形例程(java.awt
)的纯Java库,因为Android没有实现它们,因为它使用它自己的图形库。最重要的是,您无法使用Android所需的库,但我确信还有其他方法可以完成您想要执行的任务。
Android使用一些基本类型,即View
s,Canvas
和Drawable
。看看那些,看看它们是否符合您的需求。
答案 1 :(得分:1)
您无法在Android中使用 java.awt.*
包,请改用 Android UI elements 。见related question here