天易26----java导出excel表格(支持wps和office excel)
一:页面图片显示预览:
1)
2)点击导出按钮后预览:
3)最终生成的excel表格图片预览:
二:代码演示:
说明:执行操作时,请先引进导出excel表格的jar文件包,在本人的资源里面可以下载,网上搜索下载也即可,
找到导出按钮所执行的js方法,在java后天查看该方法的实现即可
1)jsp代码:
/** * 驾校合格率导出excel图表 */public void downJxhglPm(){codeList=codesService.getcode("1042");//码表查值jxhelpmList=hglTjService.getJxhglpm(tjjxkshgl);//驾校合格率排名for(int i =0;i<jxhelpmList.size();i++){TjJxkshgl tt = (TjJxkshgl)jxhelpmList.get(i); tt.setJxmc(CodeUtil.getJxjcByJxxh(codeList,tt.getJxxh()));}OutputStream os=null;WritableWorkbook wbook=null;try { os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句 //response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 String tmptitle = "驾校合格率排名"; // 标题 response.setHeader("Content-disposition", "attachment; filename="+new String(tmptitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头 response.setContentType("application/vnd.ms-excel");// 定义输出类型 wbook = Workbook.createWorkbook(os); // 建立excel文件 WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称 // 设置excel标题WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK); //设置表格样式 WritableCellFormat wcfFC = new WritableCellFormat(wfont); //wcfFC.setBackground(Colour.AQUA);Label label=new Label(0, 0, tmptitle, wcfFC); WritableCellFormat cellFormat=new WritableCellFormat(); cellFormat.setAlignment(jxl.format.Alignment.CENTRE); cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); cellFormat.setWrap(true); //cellFormat.setBackground(Colour.AQUA);cellFormat.setFont(wfont);label.setCellFormat(cellFormat); wsheet.addCell(label);//wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));wsheet.setRowView(0,500); //第一行高度wsheet.mergeCells(0, 0, 6, 1); //合并单元格(第一列的第一行和第七列的第二行合并) //wsheet.mergeCells(0, 1, 9, 1); //wsheet.mergeCells(0, 2, 0, 4); //wsheet.mergeCells(1, 2, 3, 2); //wsheet.mergeCells(4, 2, 6, 2); //wsheet.mergeCells(7, 2, 9, 2); wsheet.setColumnView(0,10); //宽度wsheet.setColumnView(1,25); //宽度wsheet.setColumnView(2,10); //宽度wsheet.setColumnView(3,10); //宽度wsheet.setColumnView(4,10); //宽度wsheet.setColumnView(5,10); //宽度// 开始生成主体内容 wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); wcfFC = new WritableCellFormat(wfont);wsheet.addCell(new Label(0, 2, "名次",wcfFC)); wsheet.addCell(new Label(1, 2, "驾校名称",wcfFC)); wsheet.addCell(new Label(2, 2, "科目一",wcfFC)); wsheet.addCell(new Label(3, 2, "科目二",wcfFC));wsheet.addCell(new Label(4, 2, "科目三",wcfFC)); wsheet.addCell(new Label(5, 2, "合格率",wcfFC)); int count=jxhelpmList.size();if(count>0){ ////判断集合是否不为0TjJxkshgl tjhgl=null;for(int i=0;i<jxhelpmList.size();i++){ tjhgl=(TjJxkshgl)jxhelpmList.get(i);wsheet.addCell(new Label(0, i+3, (i+1)+""));wsheet.addCell(new Label(1, i+3, tjhgl.getJxmc()));wsheet.addCell(new Label(2, i+3, tjhgl.getKm1hgl()));wsheet.addCell(new Label(3, i+3, tjhgl.getKm2hgl()));wsheet.addCell(new Label(4, i+3, tjhgl.getKm3hgl()));wsheet.addCell(new Label(5, i+3, tjhgl.getAvghgl()));}} wbook.write(); // 写入文件 }catch(Exception ex) { ex.printStackTrace(); }finally{if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(wbook!=null){try {wbook.close();} catch (WriteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }