创建电子表格(xls)文件时出现问题

时间:2011-08-19 09:17:13

标签: android android-file

我正在使用以下代码创建.xls - 文件。我从http://www.vogella.de/articles/JavaExcel/article.htmlhttp://www.andykhan.com/jexcelapi/tutorial.html获得了参考。请提供解决方案或替代方法来执行此操作。

下面是代码

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();

以上代码正在提供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

请指导我解决这个问题。如果有的话,请建议我生成xls文件的任何替代方法。

3 个答案:

答案 0 :(得分:1)

添加用户写入外部存储空间的权限。

答案 1 :(得分:0)

你见过this blog post

正在使用Apache POI从Android读取/写入Excel。

答案 2 :(得分:0)

我有同样的错误。对我来说,这是因为我没有在模拟器中为sd卡指定任何内存。

使用设备管理器编辑模拟器后,文件已创建。