大二下学期每日总结之第一次个人作业(第二阶段:生成excel)
今日实现了第一次个作业的生成个人14天健康表的功能,要生成excel所以需要相关的工具类,通过jxl.jar实现:
首先实现个人14天体温登记健康表要有14天的数据,数据的获取比较简单,获取学生id,通过id查询数据库获取数据。
生成十四天的数据,所以我们只要查询的数据按倒序排列,然后去前14个即可。下面为查询代码:
public List<WenDate> queryDataFor(String querystr, String str) { String [] strs={str}; Cursor cursor=sqldb.rawQuery("select * from wendudate where stuid=? order by msec desc",strs); if(cursor.moveToFirst()) { do{ String name=cursor.getString(cursor.getColumnIndex("name")); String stuid=cursor.getString(cursor.getColumnIndex("stuid")); String address=cursor.getString(cursor.getColumnIndex("address")); Float wendu=cursor.getFloat(cursor.getColumnIndex("wendu")); String dateandtime=cursor.getString(cursor.getColumnIndex("dateandtime")); String special=cursor.getString(cursor.getColumnIndex("special")); String stuclass=cursor.getString(cursor.getColumnIndex("stuclass")); Long msec=cursor.getLong(cursor.getColumnIndex("msec")); System.out.println("wendudate:::"+name+stuid+address+wendu+dateandtime+special+stuclass+msec); wenDate =new WenDate(dateandtime,address,wendu,special,stuid,name,stuclass,msec); listWenDate.add(wenDate); }while(cursor.moveToNext()); }
数据有了现在需要将数据写入excel, 需要引入jxl.jar包。
步骤也很简单,首先打开一个已存在的excel:
Workbook wb=Workbook.getWorkbook(new File("文件地址"));
WritableWorkbook wbook = wb.createWorkbook(new File("文件地址"),wb);
获取工作区:WritableSheet wsheet = wbook.getSheet(0);序号从0开始。
设置你要插入的行与列,以及内容:Label time = new Label(5, 1, str)//行与列也是从0开始,第一个参数为行。
插入数据:wsheet.addCell(time);最后写入并关闭wbook.write();wbook.close();下面为代码:
try { Workbook wb=Workbook.getWorkbook(new File("/storage/emulated/0/$MuMu共享文件夹/excel1.xls")); WritableWorkbook wbook = wb.createWorkbook(new File("/storage/emulated/0/$MuMu共享文件夹/excel1.xls"),wb); // 创建一个工作表 第一个工作区 WritableSheet wsheet = wbook.getSheet(0); Label time = new Label(5, 1, wenDate.getDateandtime());wsheet.addCell(time); Label name = new Label(1, 2, stuDate.getStuName());wsheet.addCell(name ); Label stuId = new Label(5, 2, stuDate.getStuID());wsheet.addCell(stuId); if(wenDate.getWendu()<37){ Label wendusitu = new Label(1, 3, "良好");wsheet.addCell(wendusitu); }else { Label wendusitu = new Label(1, 3, "发热");wsheet.addCell(wendusitu); } Label phone = new Label(5, 3, stuDate.getStuPhone());wsheet.addCell(phone); if(listwendate.size()<14){ for(int i=0;i<listwendate.size();i++){ Label Time=new Label(0,6+i,listwendate.get(i).getDateandtime());wsheet.addCell(Time); Label wendu = new Label(1, 6+i, listwendate.get(i).getWendu().toString());wsheet.addCell(wendu); if(listwendate.get(i).getWendu()<37){ Label situation = new Label(2, 6+i,"良好");wsheet.addCell(situation); }else{ Label situation = new Label(2, 6+i,"发热");wsheet.addCell(situation); } Label address = new Label(3, 6+i,listwendate.get(i).getAddress());wsheet.addCell(address); Label special = new Label(5, 6+i,listwendate.get(i).getSpecial());wsheet.addCell(special); } }else { for(int i=0;i<14;i++){ Label Time=new Label(0,6+i,listwendate.get(i).getDateandtime());wsheet.addCell(Time); Label wendu = new Label(1, 6+i, listwendate.get(i).getWendu().toString());wsheet.addCell(wendu); if(listwendate.get(i).getWendu()<37){ Label situation = new Label(2, 6+i,"良好");wsheet.addCell(situation); }else{ Label situation = new Label(2, 6+i,"发热");wsheet.addCell(situation); } Label address = new Label(3, 6+i,listwendate.get(i).getAddress());wsheet.addCell(address); Label special = new Label(5, 6+i,listwendate.get(i).getSpecial());wsheet.addCell(special); } } // 把值加到工作表中 // 写入文件 wbook.write(); wbook.close(); System.out.println("创建成功!"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }