2016-12-07 52 views

回答

1
  1. 的gradle添加进口

编译组: 'net.sourceforge.jexcelapi',名称: 'JXL',版本: '2.6'

  • 在AndroidManifest.xml文件中添加权限
  • <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    
  • 创建新类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(); 
         } 
        } 
    } 
    
  • 在你MainActivity添加下面的方法:

  • 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. 查找执行出口的地方,例如:
    @Override 
    protected void onResume() { 
        super.onResume(); 
        askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE, READ_EXST); 
        askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXST); 
        ExcelExporter.export(); 
    }