如何在Android中创建Excel文件?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (102)

我必须创建一个EXCEL文件编程。是否有任何API来创建EXCEL文件或其他方式?

我试过用这个链接创建一个Excel电子表格创建Excel散表

我得到了NullPointerExceptionworkbook.write();,我可以在sd卡上创建excel文件,但是当我使用ms office 2007打开该excel文件时,我将得到Unable to read file讯息

这里有堆栈跟踪,ExcelStudy是我的活动WriteExcel

W/System.err(  235): java.lang.NullPointerException
W/System.err(  235):    at jxl.biff.StringHelper.getUnicodeBytes(StringHelper.java:133)
W/System.err(  235):    at jxl.biff.FontRecord.getData(FontRecord.java:289)
W/System.err(  235):    at jxl.biff.WritableRecordData.getBytes(WritableRecordData.java:71)
W/System.err(  235):    at jxl.write.biff.File.write(File.java:132)
W/System.err(  235):    at jxl.biff.Fonts.write(Fonts.java:110)
W/System.err(  235):    at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:699)
W/System.err(  235):    at comm.study.code.WriteExcel.write(WriteExcel.java:49)
W/System.err(  235):    at comm.study.code.ExcelStudy.createExcelFile(ExcelStudy.java:64)
W/System.err(  235):    at comm.study.code.ExcelStudy$1.onClick(ExcelStudy.java:47)
W/System.err(  235):    at android.view.View.performClick(View.java:2364)
W/System.err(  235):    at android.view.View.onTouchEvent(View.java:4179)
W/System.err(  235):    at android.widget.TextView.onTouchEvent(TextView.java:6541)
W/System.err(  235):    at android.view.View.dispatchTouchEvent(View.java:3709)
W/System.err(  235):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
W/System.err(  235):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
W/System.err(  235):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
W/System.err(  235):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
W/System.err(  235):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
W/System.err(  235):    at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
W/System.err(  235):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
W/System.err(  235):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
W/System.err(  235):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(  235):    at android.os.Looper.loop(Looper.java:123)
W/System.err(  235):    at android.app.ActivityThread.main(ActivityThread.java:4363)
W/System.err(  235):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  235):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(  235):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/System.err(  235):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err(  235):    at dalvik.system.NativeStart.main(Native Method)
提问于
用户回答回答于
用户回答回答于

首先,必须访问这个链接,可以从该链接下载最新的库:

http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.tar.gz

之后,在onCreate或onResmeMehod上放置下面的代码:

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet firstSheet = workbook.createSheet("Sheet No: 1");
HSSFSheet secondSheet = workbook.createSheet("Sheet No: 2");
HSSFRow rowA = firstSheet.createRow(0);
HSSFCell cellA = rowA.createCell(0);
cellA.setCellValue(new HSSFRichTextString("Sheet One"));
HSSFRow rowB = secondSheet.createRow(0);
HSSFCell cellB = rowB.createCell(0);
cellB.setCellValue(new HSSFRichTextString("Sheet two"));
FileOutputStream fos = null;
try {
    String str_path = Environment.getExternalStorageDirectory().toString();
    File file ;
    file = new File(str_path, getString(R.string.app_name) + ".xls");
    fos = new FileOutputStream(file);
    workbook.write(fos);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Toast.makeText(MainActivity.this, "Excel Sheet Generated", Toast.LENGTH_SHORT).show();
}

//要查看此excel文件,请转到eclipse->sdCard路径->Excel.xls->拉->查看它中的FileExplorer。

扫码关注云+社区