本文实例为大家分享了Android向Excel写入数据导出并发送邮件的具体代码,供大家参考,具体内容如下

创建Execl、写入Excel格式

public WriteExcel(Context mContext){ this.mContext = mContext;}// 创建excel表public void createExcel(File file) { deleteExcel(file); WritableSheet ws = null; try { if (!file.exists()) {  wwb = Workbook.createWorkbook(file);//创建表  ws = wwb.createSheet("sheet1", 0);//表名 页数  // 在指定单元格插入数据  Label lbl1 = new Label(0, 0, "标签1");  Label lbl2 = new Label(1, 0, "标签2");  Label lbl3 = new Label(2, 0, "标签3");  Label lbl4 = new Label(3, 0, "标签4");  ws.addCell(lbl1);  ws.addCell(lbl2);  ws.addCell(lbl3);  ws.addCell(lbl4);  // 从内存中写入文件中  wwb.write();  wwb.close(); } } catch (Exception e) { e.printStackTrace(); }}/**向Execl写入数据* @Param ls List数据* @Param emeailPath* @Param file*/public void writeToExcel(List> ls,String emeailPath,File file) { try { Workbook oldWwb = Workbook.getWorkbook(file); wwb = Workbook.createWorkbook(file, oldWwb); WritableSheet ws = wwb.getSheet(0); // 当前行数 for (int i = 0; i < ls.size(); i++) {  int row = ws.getRows();  Label lab1 = new Label(0, row, ls.get(i).get("数据1") + "");  Label lab2 = new Label(1, row, ls.get(i).get("数据2") + "");  Label lab3 = new Label(2, row, ls.get(i).get("数据3") + "");  Label lab4 = new Label(3, row, ls.get(i).get("数据4") + "");  ws.addCell(lab1);  ws.addCell(lab2);  ws.addCell(lab3);  ws.addCell(lab4); } // 从内存中写入文件中,只能刷一次  wwb.write();  wwb.close();  if (emeailPath != null) {  postEmail(emeailPath);  }else{  final ProgressDialog precentDialog=new ProgressDialog(mContext);  precentDialog.setMessage("导出U盘中...");  precentDialog.setMax(100);  precentDialog.setCanceledOnTouchOutside(false);  precentDialog.show();  new Thread(){   public void run() {   //等待进度条   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,"导入U盘完成!",Toast.LENGTH_SHORT).show(); }};//删除文件夹private void deleteExcel(File file){ if(file.exists()){  file.delete(); }}

检测U盘、制作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");    //邮件接收者(数组,可以是多位接收者)    String[] emailReciver = new String[]{emailPath};    String emailTitle = "信息_"+time;    String emailContent = "核验信息";    //设置邮件地址    email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);    //设置邮件标题    email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);    //设置发送的内容    email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);    //附件    email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));   //调用系统的邮件系统    mContext.startActivity(Intent.createChooser(email, "请选择邮件发送软件"));  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多相关文章

  1. 安全新手入坑——HTML标签
  2. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  3. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  4. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  5. Android(安卓)studio App开发 SQLite数据的使用
  6. Android(安卓)EventLog简介
  7. Android启动Activity的标准Action和标准Category
  8. Android网络访问与数据解析
  9. Android-Jetpack笔记-Paging结合网络数据

随机推荐

  1. android 音效处理 (2)
  2. 转:Game Engines for Android(安卓)(Andr
  3. 修改android系统和watchdog的延时
  4. Android常用控件
  5. android 下写文件性能测试
  6. android之BitMap
  7. android 拨打紧急号码,通话时开启免提功能
  8. APP开发实战94-Vector静态图的使用
  9. android 分辨率及密度详细
  10. android用户界面-组件Widget-画廊视图Gal