Android writes data to Excel exports U disk and sends mail

  • 2021-09-24 23:34:46
  • OfStack

This article example for everyone to share Android to Excel write data export and send mail specific code, for your reference, the specific content is as follows

Create Execl and write Excel format


public WriteExcel(Context mContext){
 this.mContext = mContext;
}

//  Create excel Table 
public void createExcel(File file) {
 deleteExcel(file);
 WritableSheet ws = null;
 try {
 if (!file.exists()) {
  wwb = Workbook.createWorkbook(file);// Create a table 
  ws = wwb.createSheet("sheet1", 0);// Table name   Number of pages 

  //  Inserts data in the specified cell 
  Label lbl1 = new Label(0, 0, " Label 1");
  Label lbl2 = new Label(1, 0, " Label 2");
  Label lbl3 = new Label(2, 0, " Label 3");
  Label lbl4 = new Label(3, 0, " Label 4");

  ws.addCell(lbl1);
  ws.addCell(lbl2);
  ws.addCell(lbl3);
  ws.addCell(lbl4);

  //  Write to a file from memory 
  wwb.write();
  wwb.close();
 }

 } catch (Exception e) {
 e.printStackTrace();
 }
}

/** Toward Execl Write data 
* @Param ls List<map> Data 
* @Param emeailPath
* @Param file
*/
public void writeToExcel(List<Map<String,Object>> ls,String emeailPath,File file) {

 try {
 Workbook oldWwb = Workbook.getWorkbook(file);
 wwb = Workbook.createWorkbook(file, oldWwb);
 WritableSheet ws = wwb.getSheet(0);
 //  Current number of lines 
 for (int i = 0; i < ls.size(); i++) {
  int row = ws.getRows();
  Label lab1 = new Label(0, row, ls.get(i).get(" Data 1") + "");
  Label lab2 = new Label(1, row, ls.get(i).get(" Data 2") + "");
  Label lab3 = new Label(2, row, ls.get(i).get(" Data 3") + "");
  Label lab4 = new Label(3, row, ls.get(i).get(" Data 4") + "");
  ws.addCell(lab1);
  ws.addCell(lab2);
  ws.addCell(lab3);
  ws.addCell(lab4);
 }
 //  Write to a file from memory , Brush only 1 Times 
  wwb.write();
  wwb.close();
  if (emeailPath != null) {
  postEmail(emeailPath);
  }else{
  final ProgressDialog precentDialog=new ProgressDialog(mContext);
  precentDialog.setMessage(" Export U Disk ...");
  precentDialog.setMax(100);
  precentDialog.setCanceledOnTouchOutside(false);
  precentDialog.show();
  new Thread(){
   public void run() {
   // Wait for progress bar 
   for (int i = 0; i < 100; i++) {
    try {
    long l= (long) (Math.random()*200);
    Thread.sleep(l);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    precentDialog.setProgress(i);
   }
   precentDialog.dismiss();
   handler.sendEmptyMessage(1);
   };
  }.start();
  }
 }catch(Exception e){
  e.printStackTrace();
 }
}

@SuppressLint("HandlerLeak")
private Handler handler = new android.os.Handler() {
 @Override
 public void handleMessage(Message msg) {
 // TODO Auto-generated method stub
 super.handleMessage(msg);
 Toast.makeText(mContext," Import U Disk complete! ",Toast.LENGTH_SHORT).show();
 }
};

// Delete Folder 
private void deleteExcel(File file){
 if(file.exists()){
  file.delete();
 }
}

Test U disk and make Excel form


private void postEmail(String emailPath){

    SimpleDateFormat fmat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time=fmat.format(new Date(System.currentTimeMillis()));

    String path=getExcelDir()+ File.separator+"IdCardInfo.xls";
    File file = new File(path);
    if(file.exists()){
     Intent email = new Intent(android.content.Intent.ACTION_SEND);
     email.setType("application/octet-stream");
      // Message recipients (array, which can be multiple recipients) 
      String[] emailReciver = new String[]{emailPath};

      String emailTitle = " Information _"+time;
      String emailContent = " Verification information ";
      // Set email address 
      email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
      // Set the message header 
      email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
      // Set what to send 
      email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
      // Annex 
      email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
     // Call the mail system of the system 
      mContext.startActivity(Intent.createChooser(email, " Please select mail sending software "));
    }
}

Related articles: