我Apache Poi
在Android应用程序中使用,但是出现了问题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:
1-导入以下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);
但请注意,根据我的xslx文件,我正在获取StringCellValue,应检查xlsx文件,并查看其是否为字符串,并使用getStringCellValue()或Numeric并使用getNumericCellValue()。
请给我一些反馈
希望能有所帮助。