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

时间:2016-05-18 17:06:28

标签: 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;
}

1 个答案:

答案 0 :(得分:1)

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方案。