我尝试使用以下代码在 Android 上的 excel 中导出图像,但出现错误。复制logcat ...请帮助!
代码:
package com.android.testexcel;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class MyWorkBook {
void writeImageToExcel(String filePath, String imageFileName) {
int col = 1, row = 1;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet testsheet = wb.createSheet("test");
System.out.println("The work book is created");
try {
FileOutputStream fos = new FileOutputStream(filePath);
System.out.println("File sample.xls is created");
FileInputStream fis = new FileInputStream(imageFileName);
ByteArrayOutputStream img_bytes = new ByteArrayOutputStream();
int b;
while ((b = fis.read()) != -1)
img_bytes.write(b);
fis.close();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) col, row, (short) ++col, ++row);
int index = wb.addPicture(img_bytes.toByteArray(),
HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFSheet sheet = wb.getSheet("test");
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, index);
anchor.setAnchorType(2);
wb.write(fos);
System.out.println("Writing data to the xls file");
fos.close();
System.out.println("File closed");
} catch (IOException ioe) {
System.out
.println("Hi ! You got an exception. " + ioe.getMessage());
ioe.printStackTrace();
}
}
}// end of class MyWorkBook
日志猫:
05-13 19:11:47.665:E/AndroidRuntime(7288):致命异常:主要 05-13 19:11:47.665:E/AndroidRuntime(7288):java.lang.NoClassDefFoundError:org.apache.commons.codec。 digest.DigestUtils 05-13 19:11:47.665: E/AndroidRuntime(7288): at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1580) 05-13 19:11:47.665: E /AndroidRuntime(7288): 在 com.android.testexcel.MyWorkBook.writeImageToExcel(MyWorkBook.java:30) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 com.android.testexcel.MainActivity.onClick( MainActivity.java:71) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 android.view.View.performClick(View.java:2408) 05-13 19:11:47.665: E/AndroidRuntime( 7288): 在 android.view.View$PerformClick.run(View.java:8816) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 android.os.Handler.handleCallback(Handler.java:587) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 android.os.Handler.dispatchMessage(Handler.java:92) 05-13 19:11:47.665: E/AndroidRuntime(7288) : 在 android.os.Looper.loop(Looper.java:123) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 java.lang.reflect。 Method.invoke(Method.java:521) 05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-13 19:11:47.665: E/AndroidRuntime(7288): 在 dalvik .system.NativeStart。主要(本机方法)
提前致谢,赫曼特