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();
}
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.