Zhang Yanru
Zhang Yanru

Reputation: 61

How to export excel in Android?

I want to export string or object to excel in Android. And I don't have a good idea. What should I do? Please give me some suggestions about android export excel.

Upvotes: 4

Views: 8347

Answers (2)

lidox
lidox

Reputation: 1971

  1. Add gradle imports

compile group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6'

  1. Add permissions in the AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  1. Create new Class ExcelExporter:

    import android.os.Environment;
    
    import java.io.File;
    import java.util.Locale;
    
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    
    public class ExcelExporter {
    
        public static void export() {
            File sd = Environment.getExternalStorageDirectory();
            String csvFile = "yourFile.xls";
    
            File directory = new File(sd.getAbsolutePath());
    
            //create directory if not exist
            if (!directory.isDirectory()) {
                directory.mkdirs();
            }
            try {
    
                //file path
                File file = new File(directory, csvFile);
                WorkbookSettings wbSettings = new WorkbookSettings();
                wbSettings.setLocale(new Locale(Locale.GERMAN.getLanguage(), Locale.GERMAN.getCountry()));
                WritableWorkbook workbook;
                workbook = Workbook.createWorkbook(file, wbSettings);
    
                //Excel sheetA first sheetA
                WritableSheet sheetA = workbook.createSheet("sheet A", 0);
    
                // column and row titles
                sheetA.addCell(new Label(0, 0, "sheet A 1"));
                sheetA.addCell(new Label(1, 0, "sheet A 2"));
                sheetA.addCell(new Label(0, 1, "sheet A 3"));
                sheetA.addCell(new Label(1, 1, "sheet A 4"));
    
                //Excel sheetB represents second sheet
                WritableSheet sheetB = workbook.createSheet("sheet B", 1);
    
                // column and row titles
                sheetB.addCell(new Label(0, 0, "sheet B 1"));
                sheetB.addCell(new Label(1, 0, "sheet B 2"));
                sheetB.addCell(new Label(0, 1, "sheet B 3"));
                sheetB.addCell(new Label(1, 1, "sheet B 4"));
    
                // close workbook
                workbook.write();
                workbook.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  2. In your MainActivity add following method:

private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(StartMenu.this, permission) 
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                StartMenu.this, permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(StartMenu.this, 
                    new String[]{permission}, requestCode);

        } else {
            ActivityCompat.requestPermissions(StartMenu.this, 
                    new String[]{permission}, requestCode);
        }
    } else {
        Toast.makeText(this, permission + " is already granted.", 
                Toast.LENGTH_SHORT).show();
    }
}
  1. Find a place to execute the export, for example in:
@Override
protected void onResume() {
    super.onResume();
    askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, READ_EXST);
    askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXST);
    ExcelExporter.export();
}

Upvotes: 6

Maher Abuthraa
Maher Abuthraa

Reputation: 17813

Try this simple-excel:https://github.com/centic9/poi-on-android

Or android5xlsx: https://github.com/andruhon/android5xlsx

They are trials to wrap and repack Apache-POI on Android. This'd be good starting if you are already familiar with Apache-POI.

I hope that may help,'.

Upvotes: 0

Related Questions