Problem in creating spreadsheet(xls) file

怎甘沉沦 提交于 2019-12-08 09:31:24

问题


I am creating an .xls-file using following code. i have got reference from http://www.vogella.de/articles/JavaExcel/article.html and http://www.andykhan.com/jexcelapi/tutorial.html Please suggest solution or alternate way to do this.

below is the code

String filename = "SpreadsheetTest.xls";
File root = Environment.getExternalStorageDirectory();

WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook = Workbook.createWorkbook(new File(root, filename), wbSettings);
WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

Label label = new Label(0, 2, "A label record"); 
sheet.addCell(label); 

Number number = new Number(3, 4, 3.1459); 
sheet.addCell(number); 

workbook.write(); 
workbook.close();

The above code is giving NullPointerException:

08-19 14:34:46.354: ERROR/AndroidRuntime(3298): Uncaught handler: thread main exiting due to uncaught exception
08-19 14:34:46.362: ERROR/AndroidRuntime(3298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spreadsheet/com.spreadsheet.MainScreen}: java.lang.NullPointerException
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.os.Looper.loop(Looper.java:123)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at java.lang.reflect.Method.invoke(Method.java:521)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at dalvik.system.NativeStart.main(Native Method)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298): Caused by: java.lang.NullPointerException
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.biff.StringHelper.getUnicodeBytes(StringHelper.java:141)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.biff.FontRecord.getData(FontRecord.java:289)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.biff.WritableRecordData.getBytes(WritableRecordData.java:71)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.write.biff.File.write(File.java:147)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.biff.Fonts.write(Fonts.java:110)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:821)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at com.spreadsheet.MainScreen.onCreate(MainScreen.java:59)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-19 14:34:46.362: ERROR/AndroidRuntime(3298):     ... 11 more

Please guide me to solve this issue. Please suggest me any alternate way to generate xls file if any.


回答1:


Add user permission to write the external storage.




回答2:


Have you seen this blog post:

It is using Apache POI to read/write Excel from Android.




回答3:


I had the same error. For me it was caused as I had not specified any memory for sd card in the simulator.

File got created, once I edited the simulator using the device manager.



来源:https://stackoverflow.com/questions/7119462/problem-in-creating-spreadsheetxls-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!