java - 在 Android 中使用 POI 创建 Excel 文档后立即打开它们

标签 java android

我使用POI制作了一个文档,其名称为“Budget.xls”。它保存在我手机的 Android 文件夹中。特别是 Android/data/com.playmaker.BudgetOh/files/。但我希望它在创建后立即打开。但它似乎不起作用。请帮帮我。谢谢。

saveExcelFile(this,"Budget.xls");// This is a method that creates the file

            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
                    + "/Budget.xls/");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "Budget.xls");

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);



            try {
                startActivity(intent);
                //startActivity(Intent.createChooser(intent, "Open folder"));
            }
            catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
            }

这是我的 saveExcelFile() 代码

private boolean saveExcelFile(Context context, String fileName) {

    // check if available and not read only
    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
        Log.e(TAG, "Storage not available or read only");
        Toast.makeText(MainActivity.this, "Storage not available or read only", Toast.LENGTH_LONG).show();
        return false;
    }

    boolean success = false;

    //New Workbook
    Workbook wb = new HSSFWorkbook();

    Cell c = null;

    //Cell style for header row
    CellStyle cs = wb.createCellStyle();
    cs.setFillForegroundColor(HSSFColor.LIME.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    //New Sheet
    Sheet sheet1 = null;
    sheet1 = wb.createSheet("myOrder");

    // Generate column headings
    Row row = sheet1.createRow(0);
    //Row row2 = sheet1.createRow(1);


    c = row.createCell(0);
    c.setCellValue("Expense Title");
    c.setCellStyle(cs);


    c = row.createCell(1);
    c.setCellValue("Expense Amount");
    c.setCellStyle(cs);



    c = row.createCell(2);
    c.setCellValue("Categories");
    c.setCellStyle(cs);

    c = row.createCell(3);
    c.setCellValue("Date");
    c.setCellStyle(cs);

    c = row.createCell(4);
    c.setCellValue("Time");
    c.setCellStyle(cs);

    c = row.createCell(5);
    c.setCellValue("Location");
    c.setCellStyle(cs);



        String title;
        int counter=0;
    allExpenses = expenseManager.getAllExpenses();
    for (Expense expense : allExpenses) {
        allTitles.add(expense.getTitle());
        counter++;
    }
    Row  row2 = sheet1.createRow(1);
    int k=1;

    for ( int i=0; i < counter; i++) {
        title=allTitles.get(i);
        //Toast.makeText(getApplicationContext(),i, Toast.LENGTH_SHORT).show();
        expense = expenseManager.getExpense(title);

        int j = 0;
            c = row2.createCell(j);
            c.setCellValue(expense.getTitle());
           // c.setCellStyle(cs);

        c = row2.createCell(j+1);
            c.setCellValue(expense.getAmount());
            //c.setCellStyle(cs);

            c = row2.createCell(j+2);
            c.setCellValue(expense.getCategory());
           // c.setCellStyle(cs);

        c = row2.createCell(j+3);
            c.setCellValue(expense.getDate());
            //c.setCellStyle(cs);

            c = row2.createCell(j+4);
            c.setCellValue(expense.getTime());
            //c.setCellStyle(cs);

            c = row2.createCell(j+5);
            c.setCellValue(expense.getComment());
           // c.setCellStyle(cs);

        row2 = sheet1.createRow(k++);


    }

    sheet1.setColumnWidth(0, (15 * 500));
    sheet1.setColumnWidth(1, (15 * 500));
    sheet1.setColumnWidth(2, (15 * 500));

    // Create a path where we will place our List of objects on external storage
    File file = new File(context.getExternalFilesDir(null), fileName);
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(file);
        wb.write(os);
        Log.w("FileUtils", "Writing file" + file);
        Toast.makeText(MainActivity.this, "Writing file", Toast.LENGTH_LONG).show();
        success = true;
    } catch (IOException e) {
        Log.w("FileUtils", "Error writing " + file, e);
        Toast.makeText(MainActivity.this, "Error writing", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Log.w("FileUtils", "Failed to save file", e);
        Toast.makeText(MainActivity.this, "Error writing", Toast.LENGTH_LONG).show();
    } finally {
        try {
            if (null != os)
                os.close();
        } catch (Exception ex) {
        }
    }
    return success;
}

最佳答案

intent.setDataAndType(uri, "Budget.xls");

Budget.xls 不是有效的 MIME type 。使用 application/vnd.ms-excel 作为 Excel 电子表格的 MIME 类型。

另外,替换:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
                + "/Budget.xls/");

与:

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "Budget.xls"));

这在一定程度上消除了无效的尾随 /。在某种程度上,这添加了您的方法所缺少的 file 方案。

关于java - 在 Android 中使用 POI 创建 Excel 文档后立即打开它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37306037/

相关文章:

java - 基本网络应用程序不适用于 Jersey

java - Elasticsearch无法创建Java虚拟机

java - Android根据计算显示图像

android - 如何为 Android 安装 VirtualBox Guest Addition?

java - AlertDialog 中性按钮关闭对话框

java - Firebase - 从键包含 VAR 的子项检索所有数据

java - 使用 Apache TIKA 获取页面的内容、关键字、标题

android - 如何使用 gomobile 将多个包绑定(bind)到单个 Java AAR 文件中?

android - 如何在Google Play控制台中回滚到以前的版本(从工作版到Beta版)?

java - 如何扩展字节缓冲区的分配内存