溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android如何向Excel寫(xiě)入數(shù)據(jù)導(dǎo)出U盤(pán)并發(fā)送郵件

發(fā)布時(shí)間:2021-07-26 14:28:01 來(lái)源:億速云 閱讀:128 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹Android如何向Excel寫(xiě)入數(shù)據(jù)導(dǎo)出U盤(pán)并發(fā)送郵件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體內(nèi)容如下

創(chuàng)建Execl、寫(xiě)入Excel格式

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

// 創(chuàng)建excel表
public void createExcel(File file) {
 deleteExcel(file);
 WritableSheet ws = null;
 try {
 if (!file.exists()) {
  wwb = Workbook.createWorkbook(file);//創(chuàng)建表
  ws = wwb.createSheet("sheet1", 0);//表名 頁(yè)數(shù)

  // 在指定單元格插入數(shù)據(jù)
  Label lbl1 = new Label(0, 0, "標(biāo)簽1");
  Label lbl2 = new Label(1, 0, "標(biāo)簽2");
  Label lbl3 = new Label(2, 0, "標(biāo)簽3");
  Label lbl4 = new Label(3, 0, "標(biāo)簽4");

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

  // 從內(nèi)存中寫(xiě)入文件中
  wwb.write();
  wwb.close();
 }

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

/**向Execl寫(xiě)入數(shù)據(jù)
* @Param ls List<map>數(shù)據(jù)
* @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);
 // 當(dāng)前行數(shù)
 for (int i = 0; i < ls.size(); i++) {
  int row = ws.getRows();
  Label lab1 = new Label(0, row, ls.get(i).get("數(shù)據(jù)1") + "");
  Label lab2 = new Label(1, row, ls.get(i).get("數(shù)據(jù)2") + "");
  Label lab3 = new Label(2, row, ls.get(i).get("數(shù)據(jù)3") + "");
  Label lab4 = new Label(3, row, ls.get(i).get("數(shù)據(jù)4") + "");
  ws.addCell(lab1);
  ws.addCell(lab2);
  ws.addCell(lab3);
  ws.addCell(lab4);
 }
 // 從內(nèi)存中寫(xiě)入文件中,只能刷一次
  wwb.write();
  wwb.close();
  if (emeailPath != null) {
  postEmail(emeailPath);
  }else{
  final ProgressDialog precentDialog=new ProgressDialog(mContext);
  precentDialog.setMessage("導(dǎo)出U盤(pán)中...");
  precentDialog.setMax(100);
  precentDialog.setCanceledOnTouchOutside(false);
  precentDialog.show();
  new Thread(){
   public void run() {
   //等待進(jìn)度條
   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,"導(dǎo)入U(xiǎn)盤(pán)完成!",Toast.LENGTH_SHORT).show();
 }
};

//刪除文件夾
private void deleteExcel(File file){
 if(file.exists()){
  file.delete();
 }
}

檢測(cè)U盤(pán)、制作Excel表格

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");
    //郵件接收者(數(shù)組,可以是多位接收者)
    String[] emailReciver = new String[]{emailPath};

    String emailTitle = "信息_"+time;
    String emailContent = "核驗(yàn)信息";
    //設(shè)置郵件地址
    email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
    //設(shè)置郵件標(biāo)題
    email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
    //設(shè)置發(fā)送的內(nèi)容
    email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
    //附件
    email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
   //調(diào)用系統(tǒng)的郵件系統(tǒng)
    mContext.startActivity(Intent.createChooser(email, "請(qǐng)選擇郵件發(fā)送軟件"));
  }
}

以上是“Android如何向Excel寫(xiě)入數(shù)據(jù)導(dǎo)出U盤(pán)并發(fā)送郵件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI