創建Execl、寫入Execl數據、導入U盤 發送帶附件的郵件 ...
創建Execl、寫入Execl數據、導入U盤
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<map>數據 * @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); // 當前行數 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(); } }
發送帶附件的郵件
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+"CardInfo.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, "請選擇郵件發送軟體")); } }