日期:2014-05-19 浏览次数:20838 次
package cn.edu.cumt.print; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.List; import cn.edu.cumt.print.ExcelService; import cn.edu.cumt.UserDao.StuDetailedInfoDao; import cn.edu.cumt.realclass.simpleInfoBean; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelService { public static boolean writeExcel() throws SQLException, IOException,BiffException { try { File file = new File("result.xls"); if(file.exists()) { return false; } else { WritableWorkbook wb = Workbook.createWorkbook(new File("D:/result.xls")); WritableSheet ws = wb.createSheet("sheet1", 0); StuDetailedInfoDao sdid = new StuDetailedInfoDao(); List<simpleInfoBean> list = sdid.selectAll(); Label la = new Label(0, 0, "序号"); Label lb = new Label(1, 0, "学生姓名"); Label lc = new Label(2, 0, "性别"); Label ld = new Label(3, 0, "考试级别"); Label le = new Label(4, 0, "考试代码"); Label lee = new Label(5,0, "联系电话"); Label lf = new Label(6, 0, "身份证号"); Label lg = new Label(7, 0, "民族代码"); Label lh = new Label(8, 0, "是否补考"); Label li = new Label(8, 0, "远准考证号"); ws.addCell(la); ws.addCell(lb); ws.addCell(lc); ws.addCell(ld); ws.addCell(le); ws.addCell(lee); ws.addCell(lf);ws.addCell(lg); ws.addCell(lh); ws.addCell(li); if (null != list) { for (int i = 0; i < list.size(); i++) { simpleInfoBean ub = list.get(i); la = new Label(0, i + 1, ub.getStuName()); lb = new Label(1, i + 1, ub.getSex()); lc = new Label(2, i + 1, ub.getExamGrade()); ld = new Label(3, i + 1, ub.getCode()); le = new Label(4, i + 1, ub.getConNum()); lee = new Label(5, i + 1,ub.getIDcard()); lf = new Label(6, i + 1, ub.getNationalityCode()); lg = new Label(7, i + 1, ub.getNationalityCode()); lh = new Label(8, i + 1, ub.getMakeUpJob()); li = new Label(9, i + 1, ub.getAdmissionCardNum()); ws.addCell(la); ws.addCell(lb); ws.addCell(lc); ws.addCell(ld); ws.addCell(le); ws.addCell(lee); ws.addCell(lf); ws.addCell(lg); ws.addCell(lh); ws.addCell(li); } } wb.write(); wb.close(); return true; } } catch (Exception e) { e.printStackTrace(); return false; // TODO: handle exception } } } } //exportXLS.jsp <body> <% if(ExcelService.writeExcel()) { %> <p>成功生成终表,现在<a href="D:/result.xls">下载</a></p> <% } else { %> <p>插入失败,请去E盘查看文件“result.xls”是否已经存在</p> <% } %>