2016-12-07 3 views
2

Ich möchte String oder Objekt exportieren, um in Android zu übertreffen. Und ich habe keine gute Idee. Was soll ich tun? Bitte geben Sie mir einige Vorschläge über Android Export Excel.Wie excel in Android exportieren?

Antwort

1
  1. hinzufügen gradle Importe

Kompilierung Gruppe: 'net.sourceforge.jexcelapi', Name: 'jxl', Version: '2.6'

  1. Add Berechtigungen in der AndroidManifest.xml Datei
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
  1. neue Klasse ExcelExporter erstellen:

    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 Ihrem MainActivity fügen folgende Methode:

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. einen Ort in den Export, zum Beispiel in auszuführen:
@Override 
protected void onResume() { 
    super.onResume(); 
    askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, READ_EXST); 
    askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXST); 
    ExcelExporter.export(); 
}