欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年07月27日 16:36:21   作者:xi陽(yáng)  
這篇文章主要為大家詳細(xì)介紹了Android將數(shù)據(jù)寫(xiě)入Excel格式導(dǎo)出U盤(pán)、發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android向Excel寫(xiě)入數(shù)據(jù)導(dǎo)出并發(fā)送郵件的具體代碼,供大家參考,具體內(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ā)送軟件"));
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論