0

So this is probably a dumb question but can an android app that runs on an android device write to excel documents using apache poi? I havent been able to find any tutorials online that say specifically. I have found java tutorials that make a simple workbook and it creates a xls document just fine. if i put that same code into an android application i get

11-18 23:02:10.311: D/AndroidRuntime(12615): Shutting down VM
11-18 23:02:10.311: W/dalvikvm(12615): threadid=1: thread exiting with uncaught exception (group=0x41643d40)
11-18 23:02:10.314: E/AndroidRuntime(12615): FATAL EXCEPTION: main
11-18 23:02:10.314: E/AndroidRuntime(12615): Process: com.example.prtmanager, PID: 12615
11-18 23:02:10.314: E/AndroidRuntime(12615): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prtmanager/com.example.prtmanager.MainActivity}: java.lang.ClassCastException: com.example.prtmanager.MainActivity cannot be cast to android.app.Activity
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2135)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.os.Looper.loop(Looper.java:136)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread.main(ActivityThread.java:5102)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at java.lang.reflect.Method.invoke(Method.java:515)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at dalvik.system.NativeStart.main(Native Method)
11-18 23:02:10.314: E/AndroidRuntime(12615): Caused by: java.lang.ClassCastException: com.example.prtmanager.MainActivity cannot be cast to android.app.Activity
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
11-18 23:02:10.314: E/AndroidRuntime(12615):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2126)
11-18 23:02:10.314: E/AndroidRuntime(12615):    ... 11 more

class

 package com.example.prtmanager;

 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 import java.io.FileOutputStream;

 public class MainActivity {

 public static void main(String[] args){
    Workbook workbook = new HSSFWorkbook();

    try {
        FileOutputStream output = new FileOutputStream("Test.xls");
        workbook.write(output);
        output.close();
    } catch(Exception e){
        e.printStackTrace();
    }
 }
 }
5
  • nothing to do with POI. This code is no an Android app. Commented Nov 19, 2014 at 6:07
  • So my next question is can android apps do this? Commented Nov 19, 2014 at 6:08
  • @dtrodriguez Where is your launcher activity. Commented Nov 19, 2014 at 6:09
  • @dtrodriguez yes you can do this in android app. Commented Nov 19, 2014 at 6:11
  • @dtrodriguez at the time of application open,which class will open first. Commented Nov 19, 2014 at 6:22

1 Answer 1

2

This is how you do it in Android, no main function. OnCreate is kind of main function here, now all you need to do is to make this functionFile() work.

public class Sample extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        functionFile();
    }

    public void functionFile() {
        Workbook workbook = new HSSFWorkbook();

        try {
            File file = new File("");
            FileOutputStream output = new FileOutputStream("Test.xls");
            workbook.write(output);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You seem to be new to Android, read more How to make activities.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.