0

I tried the following code to export image in excel on Android but I got an error. Copying logcat... please help!!

Code:

        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

Logcat:

05-13 19:11:47.665: E/AndroidRuntime(7288): FATAL EXCEPTION: main 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): at com.android.testexcel.MyWorkBook.writeImageToExcel(MyWorkBook.java:30) 05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.testexcel.MainActivity.onClick(MainActivity.java:71) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.view.View.performClick(View.java:2408) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.view.View$PerformClick.run(View.java:8816) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Handler.handleCallback(Handler.java:587) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Handler.dispatchMessage(Handler.java:92) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Looper.loop(Looper.java:123) 05-13 19:11:47.665: E/AndroidRuntime(7288): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-13 19:11:47.665: E/AndroidRuntime(7288): at java.lang.reflect.Method.invokeNative(Native Method) 05-13 19:11:47.665: E/AndroidRuntime(7288): at 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): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-13 19:11:47.665: E/AndroidRuntime(7288): at dalvik.system.NativeStart.main(Native Method)

Thanks in Advance, Hemant

yorkw
  • 40,926
  • 10
  • 117
  • 130
Hemant
  • 1
  • 2

1 Answers1

0

The crucial part of your error is this:

java.lang.NoClassDefFoundError: org.apache.commons.codec.digest.DigestUtils

You need to make sure you include commons codec in your project. See the Apache POI Components Page for the list of the dependencies of all the different bits of POI. (Or, if you use Maven, it'll handle that for you)

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • This problem is similar to http://stackoverflow.com/questions/9126567/method-not-found-using-digestutils-in-android I am still not able to find the best way of editing the source files. Can ayone help me with this ? – Hemant May 19 '12 at 07:51