Android developmentAndroid tutorial

How to create an excel file in android?

How To Create Excel File In Android
How To Create Excel File In Android

Hi in this Android article I am sharing How to create an excel file in android?. We are sharing an easy way to create an excel file on Android. You can add customer Heading and your List of data to add in your excel file. we are using writableworkbook  to write data in your excel file close workbook.

In Android create an Xls file with the JXL library and writableworkbook Android. First of all, you can download it  Download jxl-2.6.jar    and add these to your project libs folder. and compile in your App module Gradle file.

Step 1:  Download jxl latest version and extract your zip file.

firstly you can download the latest version of jxl library and extract it. Copy your JXL jar file and Past it into the Project base libe folder.

Step 2: Add your jar file in your module base Gradle file.

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

  add library and sync your project after successfully syncing your project. open your java file. and implement a writable workbook for creating Xls file.

Step 3: Create a Data Module Class for set and get data.

package com.codeplayon.genreate_xlsfile;

class Bean {
    String initial, firstName, middleName, lastName;

    Bean(String initial, String firstName, String middleName, String lastName) {
        this.initial = initial;
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }

    public String getInitial() {
        return initial;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public String getLastName() {
        return lastName;
    }

}

Step 4: In Your Activity Java file you can use the following code.

public class MainActivity extends AppCompatActivity {

    TextView Daonloaad;
    WritableWorkbook workbook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Daonloaad = findViewById(R.id.Daonloaad);
        Daonloaad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                        checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
                else
                {
                    //your code
                    createExcelSheet();
                }

            }
        });
    }


    void createExcelSheet() {
        //File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

        String csvFile = "ExcelsheetName.xls";
        java.io.File futureStudioIconFile = new java.io.File(Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                + "/" + csvFile);
        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));
        try {
            workbook = Workbook.createWorkbook(futureStudioIconFile, wbSettings);
            createFirstSheet();
//            createSecondSheet();
            //closing cursor
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void createFirstSheet() {
        try {
            List<Bean> listdata = new ArrayList<>();

            listdata.add(new Bean("mr","firstName1","middleName1","lastName1"));
            listdata.add(new Bean("mr","firstName1","middleName1","lastName1"));
            listdata.add(new Bean("mr","firstName1","middleName1","lastName1"));
            //Excel sheet name. 0 (number)represents first sheet
            WritableSheet sheet = workbook.createSheet("sheet1", 0);
            // column and row title
            sheet.addCell(new Label(0, 0, "NameInitial"));
            sheet.addCell(new Label(1, 0, "firstName"));
            sheet.addCell(new Label(2, 0, "middleName"));
            sheet.addCell(new Label(3, 0, "lastName"));

            for (int i = 0; i < listdata.size(); i++) {
                sheet.addCell(new Label(0, i + 1, listdata.get(i).getInitial()));
                sheet.addCell(new Label(1, i + 1, listdata.get(i).getFirstName()));
                sheet.addCell(new Label(2, i + 1, listdata.get(i).getMiddleName()));
                sheet.addCell(new Label(3, i + 1, listdata.get(i).getLastName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}