本文介绍了使用 Apache poi & 在 Android 应用程序中插入图像时出现 NoClassDefFoundError微软Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Apache poi 将 PNG 图像插入到我的 Excel 工作表中.

I want to insert a PNG image into my Excel sheet using Apache poi.

为此,我使用以下代码:

To do that I use this code:

//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解决了这个错误,现在我有这个错误:

But in first time I have one error which I have solved by adding this library commons-codec-1.8.jar and now I have this error:

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();

我该如何解决?

推荐答案

您不能使用涉及图形例程 (java.awt) 的纯 Java 库,因为 Android 尚未实现它们,因为它使用它自己的图形库.最重要的是,你不能在 Android 上使用你想要的库,尽管我确信还有其他方法可以完成你想做的任务.

You can't use pure Java libraries that involve graphics routines (java.awt), because Android hasn't implemented them, as it uses it's own graphics library. Bottom line is, you can't use the library you want with Android, although I'm sure there's other ways to do the task you want to do.

Android 使用一些基本类型,即 Views、CanvasDrawables.看看这些,看看它们是否满足您的需求.

Android uses a few base types, namely Views, Canvas, and Drawables. Take a look at those, and see if they meet your needs.

这篇关于使用 Apache poi & 在 Android 应用程序中插入图像时出现 NoClassDefFoundError微软Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 12:25