从android listview生成excel

时间:2017-04-20 04:46:57

标签: android excel listview

我有一个列表视图,我想生成该列表视图的excel文件并通过邮件分享。这可能与android?

1 个答案:

答案 0 :(得分:0)

首先你需要下载java excel jar,使用下面的链接

Java Excel Jar Link

下载jar后,添加build.gradle文件,

 dependencies {
    compile files('libs/jxl-2.6.12.jar')
}

现在您可以使用jar创建excel文件,在Java类中,

private void createExcelSheet()
{
    String Fnamexls="excelSheet"+System.currentTimeMillis()+ ".xls";
    File sdCard = Environment.getExternalStorageDirectory();
    File directory = new File (sdCard.getAbsolutePath() + "/newfolder");
    directory.mkdirs();
    File file = new File(directory, Fnamexls);

    WorkbookSettings wbSettings = new WorkbookSettings();

    wbSettings.setLocale(new Locale("en", "EN"));

    WritableWorkbook workbook;
    try {
        int a = 1;
        workbook = Workbook.createWorkbook(file, wbSettings);
        //workbook.createSheet("Report", 0);
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
        Label label = new Label(0, 2, "SECOND");
        Label label1 = new Label(0,1,"first");
        Label label0 = new Label(0,0,"HEADING");
        Label label3 = new Label(1,0,"Heading2");
        Label label4 = new Label(1,1,String.valueOf(a));
        try {
            sheet.addCell(label);
            sheet.addCell(label1);
            sheet.addCell(label0);
            sheet.addCell(label4);
            sheet.addCell(label3);
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        workbook.write();
        try {
            workbook.close();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //createExcel(excelSheet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}