Apache Poi Android

我在Android应用程序中使用Apache Poi ,但是我遇到了HSSFWorkbook的问题。

我只想读取Android平板电脑内部存储器中的Excel文件。 (在Android应用程序中显示数据)

我的代码:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { AssetManager assetManager = this.getAssets(); InputStream input = assetManager.open("test42.XLS"); HSSFWorkbook classeur = new HSSFWorkbook(input); //HSSFSheet feuille = classeur.getSheetAt(0); //Cell cell = feuille.getRow(1).getCell(1); //String value = cell.getStringCellValue(); TextView tv = new TextView(this); tv.setText("Test"); setContentView(tv); } catch (IOException e) { e.printStackTrace(); System.out.println("prob"); } } 

日志:

 01-28 14:09:55.036: E/dalvikvm(6919): Could not find class 'org.apache.xmlbeans.XmlOptions', referenced from method org.apache.poi.POIXMLDocumentPart.<clinit> 01-28 14:09:55.036: W/dalvikvm(6919): VFY: unable to resolve new-instance 2751 (Lorg/apache/xmlbeans/XmlOptions;) in Lorg/apache/poi/POIXMLDocumentPart; 01-28 14:09:55.036: D/dalvikvm(6919): VFY: replacing opcode 0x22 at 0x0008 01-28 14:09:55.056: E/AndroidRuntime(6919): FATAL EXCEPTION: main 01-28 14:09:55.056: E/AndroidRuntime(6919): java.lang.ExceptionInInitializerError 01-28 14:09:55.056: E/AndroidRuntime(6919): at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:72) 01-28 14:09:55.056: E/AndroidRuntime(6919): at android.app.Activity.performCreate(Activity.java:5267) 01-28 14:09:55.056: E/AndroidRuntime(6919): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 01-28 14:09:55.056: E/AndroidRuntime(6919): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 01-28 14:09:55.056: E/AndroidRuntime(6919): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295) 01-28 14:09:55.056: E/AndroidRuntime(6919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 01-28 14:09:55.056: E/AndroidRuntime(6919): at java.lang.reflect.Method.invokeNative(Native Method) 

新的日志猫:

 01-29 10:00:44.809: I/dalvikvm(7833): Could not find method org.apache.commons.codec.digest.DigestUtils.md5, referenced from method org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture 01-29 10:00:44.809: W/dalvikvm(7833): VFY: unable to resolve static method 6355: Lorg/apache/commons/codec/digest/DigestUtils;.md5 ([B)[B 01-29 10:00:44.809: D/dalvikvm(7833): VFY: replacing opcode 0x71 at 0x0004 01-29 10:00:44.829: W/System.err(7833): java.io.IOException: Invalid header signature; read 0x0010000000060409, expected 0xE11AB1A1E011CFD0 01-29 10:00:44.829: W/System.err(7833): at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140) 01-29 10:00:44.829: W/System.err(7833): at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104) 01-29 10:00:44.829: W/System.err(7833): at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138) 01-29 10:00:44.829: W/System.err(7833): at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322) 01-29 10:00:44.829: W/System.err(7833): at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303) 01-29 10:00:44.829: W/System.err(7833): at com.example.test.MainActivity.onCreate(MainActivity.java:41) 01-29 10:00:44.829: W/System.err(7833): at android.app.Activity.performCreate(Activity.java:5267) 01-29 10:00:44.829: W/System.err(7833): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 01-29 10:00:44.829: W/System.err(7833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 01-29 10:00:44.829: W/System.err(7833): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295) 01-29 10:00:44.829: W/System.err(7833): at android.app.ActivityThread.access$700(ActivityThread.java:150) 01-29 10:00:44.829: W/System.err(7833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 01-29 10:00:44.829: W/System.err(7833): at android.os.Handler.dispatchMessage(Handler.java:99) 01-29 10:00:44.829: W/System.err(7833): at android.os.Looper.loop(Looper.java:176) 01-29 10:00:44.829: W/System.err(7833): at android.app.ActivityThread.main(ActivityThread.java:5279) 01-29 10:00:44.829: W/System.err(7833): at java.lang.reflect.Method.invokeNative(Native Method) 01-29 10:00:44.829: W/System.err(7833): at java.lang.reflect.Method.invoke(Method.java:511) 01-29 10:00:44.829: W/System.err(7833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 01-29 10:00:44.829: W/System.err(7833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 01-29 10:00:44.829: W/System.err(7833): at dalvik.system.NativeStart.main(Native Method) 01-29 10:00:44.899: W/ResourceType(7833): Failure getting entry for 0x01080a03 (t=7 e=2563) in package 0 (error -75) 01-29 10:00:44.999: D/libEGL(7833): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so 01-29 10:00:45.049: D/libEGL(7833): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so 01-29 10:00:45.059: D/libEGL(7833): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so 01-29 10:00:45.129: D/OpenGLRenderer(7833): Enabling debug mode 0 

为xls文件尝试一下如下所示:

  try { InputStream caInput = new BufferedInputStream(getAssets().open( "ie_data.xls")); HSSFWorkbook workbook = new HSSFWorkbook(caInput); HSSFSheet feuille = workbook.getSheetAt(0); Cell cell = feuille.getRow(1).getCell(1); String value = cell.getNumericCellValue()+""; Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG) .show(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 

和xlsx:

1st – 导入下面的jar文件:

(dom4j-1.6.1.jar,poi-ooxml-3.5-FINAL.jar,poi-ooxml-schemas-3.8-beta4.jar,poi-3.9-20121203.jar,xmlbeans-2.3.0.jar)。

第二步 – 在try catch块内进行下面的操作:

如果您正在使用File对象,请执行以下操作:

 File file = new File("path"); InputStream caInput = new FileInputStream(file); 

如果您正在使用inputStram读取文件,请执行以下操作:

 InputStream caInput = new BufferedInputStream(getAssets().open( "ie_data.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(caInput); XSSFSheet feuille = workbook.getSheetAt(0); Cell cell = feuille.getRow(0).getCell(0); String value = cell.getStringCellValue() + ""; System.out.print("hello = " + value); 

但请注意,我得到一个StringCellValue根据我的xslx文件,你应该检查你的xlsx文件,看看它是一个string,并使用getStringCellValue()或一个数字,并使用getNumericCellValue()。

请给我一些反馈

希望有所帮助。

  WorkbookFactory.create(fis); 

使用Workboook工厂创buildWorkoook。

请参阅Files vs InputStreams 在这里

最后,我在平板电脑中find了我的文件的path(以避免使用资源):“/storage/sdcard0/Documents/test42.xls”

该文件是由应用程序find的,但是在创buildHSSFWorkbook时仍然存在问题,“01-29 16:15:21.329:W / System.err(13127)”exception:java.io.IOException:无效的头标记;读取0x0010000000060409,预计0xE11AB1A1E011CFD0“

我不明白这个例外,但至less我find了我的档案“置换”

我的代码:

  try { File file = new File("/storage/sdcard0/Documents/test42.xls"); FileInputStream input = new FileInputStream(file); HSSFWorkbook classeur = new HSSFWorkbook(input); //HSSFSheet feuille = classeur.getSheetAt(0); //Cell cell = feuille.getRow(1).getCell(1); //String value = cell.getStringCellValue(); String path = Environment.getExternalStorageDirectory().getAbsolutePath(); TextView tv = new TextView(this); tv.setText(path ); setContentView(tv); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }