sal*_*lai 1 java excel android android-intent
我正在开发一个使用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
该行intent.setType("application/excel");
用于过滤excel文件。但android文件管理器不选择任何excel文件。它保持在禁用阶段。我通过替换intent.setType("application/excel");
为intent.setType("file/.mp3");
它来检查此代码它工作正常并且它选择 mp3 文件。我也试过intent.setType("file/.xls");
没有运气。
在此链接中,您可以参考 Microsoft Office MIME 列表
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
Run Code Online (Sandbox Code Playgroud)
您可以搜索如下 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);
}
Run Code Online (Sandbox Code Playgroud)
你可以得到如下路径:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
有两种读取 Excel 文件的方法(可能更多),由于其扩展名(.xls 和 .xlsx),请参阅以下链接:
您可以在读取可能性之间切换并写入 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();
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你!
归档时间: |
|
查看次数: |
1285 次 |
最近记录: |