将SQLite数据库的数据写到excel表里。
1、用java代码操作excel表需要用poi,poi导包有很多问题,不能有多余的包,可能会有重复内容;其他附带的包也不能少,会报错。
2、代码:
1 public void makeExecl() 2 { 3 sqLhelper=new SQLhelper(getActivity(),"Temperature",null,1); 4 SQLiteDatabase RsqliteDatabase=sqLhelper.getReadableDatabase(); 5 String time=new SimpleDateFormat("yyyy/MM/dd").format(new Date()); 6 Cursor c=RsqliteDatabase.rawQuery("select * from lastuser", null); 7 c.moveToFirst(); 8 String lastname=c.getString(c.getColumnIndex("name")).toString(); 9 String lastphone=c.getString(c.getColumnIndex("phone")).toString(); 10 String lastnum=c.getString(c.getColumnIndex("num")).toString(); 11 c=RsqliteDatabase.rawQuery("select * from tempera where name='"+lastname+"' order by time desc", null); 12 c.moveToFirst(); 13 try { 14 verifyStoragePermissions(getActivity()); 15 InputStream inputStream = new FileInputStream("data/data/com.example.seanfu/14天健康登记样表.xls"); 16 if(inputStream==null) 17 { 18 Toast.makeText(getActivity(),"aaa",Toast.LENGTH_SHORT).show(); 19 }else{ 20 HSSFWorkbook hssfWorkbook= new HSSFWorkbook(inputStream); 21 HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0); 22 HSSFRow hssfRow2 = hssfSheet.getRow(1); 23 HSSFRow hssfRow3 = hssfSheet.getRow(2); 24 HSSFRow hssfRow4 = hssfSheet.getRow(3); 25 HSSFRow hssfRow21 = hssfSheet.getRow(21); 26 HSSFCell school = hssfRow2.getCell(1); 27 HSSFCell timecell = hssfRow2.getCell(5); 28 HSSFCell namecell = hssfRow3.getCell(1); 29 HSSFCell numcell = hssfRow3.getCell(5); 30 HSSFCell healthcell = hssfRow4.getCell(1); 31 HSSFCell phonecell = hssfRow4.getCell(5); 32 HSSFCell signcell = hssfRow21.getCell(2); 33 HSSFCell signtimecell = hssfRow21.getCell(5); 34 school.setCellValue("石家庄铁道大学"); 35 timecell.setCellValue(time); 36 namecell.setCellValue(lastname); 37 numcell.setCellValue(lastnum); 38 phonecell.setCellValue(lastphone); 39 healthcell.setCellValue(gethealth(lastnum,time)); 40 signcell.setCellValue(lastname); 41 signtimecell.setCellValue(time); 42 for(int i=19, t=0;i>5;i--,t++) 43 { 44 HSSFRow hssfRow = hssfSheet.getRow(i); 45 HSSFCell cell = hssfRow.getCell(0); 46 HSSFCell cell1 = hssfRow.getCell(1); 47 HSSFCell cell2 = hssfRow.getCell(2); 48 HSSFCell cell3 = hssfRow.getCell(3); 49 HSSFCell cell4 = hssfRow.getCell(5); 50 int month=Integer.valueOf(time.split("/")[1]); 51 int day=Integer.valueOf(time.split("/")[2])-t; 52 if(day<=0) 53 { 54 month--; 55 day+=28; 56 } 57 String time_m=""; 58 String time_d=""; 59 time_m=String.valueOf(month); 60 time_d=String.valueOf(day); 61 cell.setCellValue(time_m+"月"+time_d+"日"); 62 String m=String.valueOf(month);String d=String.valueOf(day); 63 if(m.length()==1) m="0"+m;if(d.length()==1) d="0"+d; 64 String Temp=time.split("/")[0]+"/"+m+"/"+d; 65 Cursor q = RsqliteDatabase.rawQuery("select * from tempera where num='" + lastnum + "' and time1='" + Temp + "' order by time desc",null); 66 q.moveToFirst(); 67 if(q.getCount()!=0) 68 { 69 cell1.setCellValue(q.getString(q.getColumnIndex("temperature")).toString()); 70 cell2.setCellValue(gethealth(Float.parseFloat(q.getString(q.getColumnIndex("temperature")).toString()))); 71 cell3.setCellValue(q.getString(q.getColumnIndex("location")).toString()); 72 cell4.setCellValue(q.getString(q.getColumnIndex("info")).toString()); 73 }else{ 74 cell1.setCellValue("无记录"); 75 cell2.setCellValue("无记录"); 76 cell3.setCellValue("无记录"); 77 cell4.setCellValue("无记录"); 78 } 79 } 80 OutputStream fos = new FileOutputStream("data/data/com.example.seanfu/14天健康登记样表.xls"); 81 hssfWorkbook.write(fos); 82 fos.flush(); 83 fos.close(); 84 Toast.makeText(getActivity(),"生成成功:"+"\"data/data/com.example.seanfu/14天健康登记样表.xls",Toast.LENGTH_SHORT).show(); 85 } 86 } catch (FileNotFoundException e) { 87 e.printStackTrace(); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 }