在此链接中,您可以参考 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
您可以在读取可能性之间切换并写入 SQLiteDatabe,如下所示:
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();
}
希望对你有帮助!