我正在使用apache poi开发一个用于读取/写入excel文件的android应用程序。为了从文件系统中选择文件,我使用了以下代码
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/excel");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
行intent.setType("application/excel");
用于过滤excel文件。但是android文件管理器不会选择任何excel文件。它仍处于禁用阶段。我通过将intent.setType("application/excel");
替换为intent.setType("file/.mp3");
来检查此代码。它工作正常并且它选择了mp3文件。我也试过intent.setType("file/.xls");
没有运气。
答案 0 :(得分:2)
在此链接中,您可以引用Microsoft Office MIME列表
https://stackoverflow.com/a/4212908/4300670
Extension MIME Type
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12
.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.mdb application/vnd.ms-access
您可以搜索如下所示的Excel文件
private void searchExcelFile() {
String[] mimeTypes = {"application/vnd.ms-excel" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};
Intent searchExcel = new Intent();
searchExcel.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
searchExcel.setAction(Intent.ACTION_GET_CONTENT);
//searchExcel.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
searchExcel.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
searchExcel.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
searchExcel.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(searchExcel,"Selecione Arquivo Excel"), EXCEL_IMPORTED);
}
您将获得如下所示的路径:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == EXCEL_IMPORTED) {
if (data.getClipData() == null) {
readExcelData(data.getData().getPath());
} else {
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Log.i("debinf cliinfo", "data.getClipData().getItemAt(i).getUri().toString()" + data.getClipData().getItemAt(i).getUri().toString());
}
}
}
} else {
Log.i("debinf cliinfo", "resultCol NOT OK");
}
}
有两种读取Excel文件的方法(可能更多),由于其扩展名(.xls和.xlsx),请参见下面的链接:
https://stackoverflow.com/a/33047554/4300670
您可以在可能的阅读方式之间切换,并按如下所示在SQLiteData中进行书写:
private void readExcelData(String ExcelFilePath) {
// HSSFWorkbook is for .xls
// XSSFWorkbook is for .xlsx
// Check the extension of the Excel file
String[] path = ExcelFilePath.split(":");
Workbook workbook = null;
if (ExcelFilePath.endsWith(".xls")) {
try {
InputStream inputStream = new FileInputStream(new File(path[1]));
workbook = new HSSFWorkbook(inputStream);
} catch (FileNotFoundException e) {
Log.i("debinf cliinf", "readExcelData: FileNotFoundException " + e.getMessage());
} catch (IOException e) {
Log.i("debinf cliinf", "readExcelData: Error reading InputStream " + e.getMessage());
}
} else if (ExcelFilePath.endsWith(".xlsx")) {
try {
InputStream inputStream = new FileInputStream(new File(path[1]));
workbook = new XSSFWorkbook(inputStream);
} catch (FileNotFoundException e) {
Log.i("debinf cliinf", "readExcelData: FileNotFoundException " + e.getMessage());
} catch (IOException e) {
Log.i("debinf cliinf", "readExcelData: Error reading InputStream " + e.getMessage());
}
}
Log.i("debinf cliinf", "uri is " + ExcelFilePath.endsWith(".xls"));
Sheet sheet = workbook.getSheetAt(0);
Log.i("debinf cliinfo", "sheet is " + sheet);
if (sheet == null) {
return;
}
File dbfile = new File(Environment.getExternalStorageDirectory()+"/"+groupKeyIntent+"/"+"client.db");
if (!dbfile.isFile()) {
String Folder = Environment.getExternalStorageDirectory()+"/"+groupKeyIntent+"/";
new File(Folder).mkdirs();
Log.i("debinf cliinfo", "rootFolder created: " + Folder);
ClientDatabaseHelper createDatabaseHelper = new ClientDatabaseHelper(this,"client.db", Folder);
clientListTable = createDatabaseHelper.getWritableDatabase();
clientRepository = new ClientRepository(clientListTable);
//clientList = clientRepository.SearchAllClients();
//mClientListAdapter.notifyDataSetChanged();
}
clientRepository.insertExcelToSqlite(sheet, groupKeyIntent);
clientList = clientRepository.SearchAllClients();
mClientListAdapter = new ClientListAdapter(clientList, clientRepository, groupNameIntent, groupKeyIntent, groupCreatorIntent);
mClientList.setAdapter(mClientListAdapter);
//mClientListAdapter.notifyDataSetChanged();
}
希望对您有所帮助!
答案 1 :(得分:0)
我通过使用正确的MIME类型
解决了这个问题ScopedTypeVariables
感谢CommonsWare