我已经完成了关于这个主题的所有其他问题,但没有一个对我有用。我想在Android 4.0及更高版本中阅读.xlsx文件,所以请相应地提出解决方案。我正在使用Android Studio 2.2.3
和尝试阅读.xlsx文件。
这是我的代码:
public class MainActivity extends AppCompatActivity {
Button writeExcelBtn,readExcelBtn;
public static final String appName = "ReadXL";
public static final String imagePath = Environment.getExternalStorageDirectory() + File.separator + "/"+"Download"+"/";
ArrayList<TokenDetailsModel> modelArrayList;
RecyclerView xlDataList;
XLDataListAdapter xlDataListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeExcelBtn = (Button)findViewById(R.id.writeExcel);
readExcelBtn = (Button)findViewById(R.id.readExcel);
modelArrayList = new ArrayList<>();
xlDataList = (RecyclerView)findViewById(R.id.data_list);
xlDataList.setLayoutManager(new LinearLayoutManager(MainActivity.this));
readExcelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readExcelFile(MainActivity.this,"all_tokens.xlsx");
}
});
}
private void readExcelFile(Context context, String filename) {
modelArrayList.clear();
if (!isExternalStorageAvailable() || isExternalStorageReadOnly())
{
Log.w("FileUtils", "Storage not available or read only");
return;
}
try{
// Creating Input Stream
//File file = new File(imagePath, filename);
File file = new File(imagePath+filename);
System.out.println("********** file path : "+imagePath+filename);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
//POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
// HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
XSSFWorkbook myBook = new XSSFWorkbook(myInput);
// Get the first sheet from workbook
//HSSFSheet mySheet = myWorkBook.getSheetAt(0);
XSSFSheet mySheet = myBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator<Row> rowIter = mySheet.rowIterator();
// Iterator<Column> columnIterator = mySheet.iterator();
TokenDetailsModel model = null;
while(rowIter.hasNext()){
model = new TokenDetailsModel();
//HSSFRow myRow = (HSSFRow) rowIter.next();
XSSFRow myRow = (XSSFRow)rowIter.next();
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
//HSSFCell myCell = (HSSFCell) cellIter.next();
Cell myCell = (XSSFCell) cellIter.next();
System.out.println("************ myCell.getRowIndex() and its data : "+myCell.getRowIndex()+" "+myCell.toString());
switch (myCell.getCellType()){
case CELL_TYPE_STRING :
System.out.println("************ string data : "+myCell.toString());
Toast.makeText(MainActivity.this,"String data : "+myCell.toString(),Toast.LENGTH_SHORT).show();
break;
case CELL_TYPE_NUMERIC : System.out.println("************ string data : "+myCell.getNumericCellValue());
Toast.makeText(MainActivity.this,"String data : "+myCell.getNumericCellValue(),Toast.LENGTH_SHORT).show();
break;
/* case 2 :model.setToken(myCell.toString());
break;
case 3 : model.setPhone1(myCell.toString());
break;
case 4 : model.setPhone2(myCell.toString());
break;*/
}
Log.w("FileUtils", "Cell Value: " + myCell.toString());
// Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}
// modelArrayList.add(model);
}
}catch (Exception e){
e.printStackTrace();
System.out.println("********** file exception : "+e);
}
/*xlDataListAdapter = new XLDataListAdapter(MainActivity.this,modelArrayList);
xlDataList.setAdapter(xlDataListAdapter);*/
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
这是我的日志:
01-06 15:06:51.570 4619-4619 / com.etaserve.readxlsx W / System.err:org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException 01-06 15:06:51.605 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62) 01-06 15:06:51.605 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:426) 01-06 15:06:51.610 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:431) 01-06 15:06:51.610 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155) 01-06 15:06:51.610 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:221) 01-06 15:06:51.610 4619-4619 / com.etaserve.readxlsx W / System.err:at com.etaserve.readxlsx.MainActivity.readExcelFile(MainActivity.java:83) 01-06 15:06:51.610 4619-4619 / com.etaserve.readxlsx W / System.err:at com.etaserve.readxlsx.MainActivity.access $ 000(MainActivity.java:29) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at com.etaserve.readxlsx.MainActivity $ 1.onClick(MainActivity.java:56) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.view.View.performClick(View.java:4475) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.view.View $ PerformClick.run(View.java:18786) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.os.Handler.handleCallback(Handler.java:730) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.os.Handler.dispatchMessage(Handler.java:92) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.os.Looper.loop(Looper.java:176) 01-06 15:06:51.615 4619-4619 / com.etaserve.readxlsx W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5419) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:at java.lang.reflect.Method.invokeNative(Native Method) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:at java.lang.reflect.Method.invoke(Method.java:525) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:at dalvik.system.NativeStart.main(Native Method) 01-06 15:06:51.620 4619-4619 / com.etaserve.readxlsx W / System.err:引起:java.lang.reflect.InvocationTargetException 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:at java.lang.reflect.Constructor.constructNative(Native Method) 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:... 18更多 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:引起:java.lang.NoClassDefFoundError:org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument $ Factory 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.xssf.usermodel.XSSFTable.readFrom(XSSFTable.java:72) 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:at org.apache.poi.xssf.usermodel.XSSFTable。(XSSFTable.java:67) 01-06 15:06:51.625 4619-4619 / com.etaserve.readxlsx W / System.err:... 21更多