Apache Poi 安卓 [英] Apache Poi Android
问题描述
我在 Android 应用程序中使用 Apache Poi
,但是 HSSFWorkbook
有问题.
I am using Apache Poi
in an Android application, but I have a problem with the HSSFWorkbook
.
我只想读取在 Android 平板电脑内存中找到的 Excel 文件.(在Android应用中显示数据)
I just want to read an Excel file found in the internal memory of the Android tablet. (To display data in Android application)
我的代码:
@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 some thing like the following for the xls files :
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 文件:
1st - import the follwoing jar files :
(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).
(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).
2nd - 在 try catch 块中执行以下操作:
2nd - do the follwoing inside a try catch block :
如果您使用的是 File 对象,请执行以下操作:
if you are using a File object do the follwoing :
File file = new File("path");
InputStream caInput = new FileInputStream(file);
如果您正在使用 inputStram 读取文件,请执行以下操作:
if you are reading the file using inputStram do the following :
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);
但请注意,我根据我的 xslx 文件获取了 StringCellValue,您应该检查您的 xlsx 文件并查看它是否为字符串并使用 getStringCellValue() 或 Numeric 并使用 getNumericCellValue().
but please notice i am getting a StringCellValue according to my xslx file , you should check your xlsx file and see if its a string and use getStringCellValue() or a Numeric and use getNumericCellValue() .
请给我一些反馈
希望有所帮助.
这篇关于Apache Poi 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!