0

I am having one listview, I want to generate excel file of that listview and share it by mail. is it possible with android?

2
  • 1
    you need to use java excel library to generate excel sheet in android Commented Apr 20, 2017 at 4:50
  • 1
    you can add the data in the db and use stacktips.com/tutorials/android/… Commented Apr 20, 2017 at 4:51

1 Answer 1

1

First you need to download java excel jar, use below link

Java Excel Jar Link

After downloaded jar , add in your build.gradle file,

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

Now you can create excel file using jar, In Java class,

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.