jxl 打印excel,样式设置

Jxl导出excel代码

  1. public void outputExcel(HttpServletRequest request, HttpServletResponse response,String title)throws  IOException  
  2.     {  
  3.         //获取输出流   
  4.         OutputStream os = response.getOutputStream();   
  5.         HttpSession session=request.getSession();  
  6.         String oprator=(String)session.getAttribute("yhxm");  
  7.         //设置编码    
  8.         response.setHeader("Content-disposition", "attachment; filename="+"cprdpxx"+".xls");// 设定输出文件头   
  9.         request.setCharacterEncoding("utf-8");    
  10.         //设置文件格式  
  11.         response.setContentType("application/vnd.ms-excel;charset=utf-8");  
  12.         //加标题      
  13.         //标题字体     
  14.         //设置单元格字体,位置  
  15.         jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.COURIER, 18, WritableFont.BOLD, true);    
  16.         jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);   
  17.         try {  
  18.             wcfFC.setAlignment(jxl.format.Alignment.CENTRE);  
  19.             wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);      
  20.             //字段字体     
  21.             jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(WritableFont.COURIER, 12, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);    
  22.             jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(wfc1);  
  23.             wcfFC1.setBorder(Border.NONE,BorderLineStyle.THIN,Colour.GRAY_50);  
  24.             //设置字体位置  
  25.             wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);    
  26.             wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);  
  27.             //  
  28.             //2013/7/17  
  29.             //标题字体  
  30.             jxl.write.WritableFont wfc3 = new jxl.write.WritableFont(WritableFont.COURIER, 14, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);    
  31.             jxl.write.WritableCellFormat wcfFC3 = new jxl.write.WritableCellFormat(wfc3);  
  32.             wcfFC3.setBorder(Border.NONE,BorderLineStyle.THIN,Colour.GRAY_50);  
  33.             //设置字体位置  
  34.             wcfFC3.setAlignment(jxl.format.Alignment.CENTRE);    
  35.             wcfFC3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);    
  36.               
  37.             //查询结果字体    
  38.             jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();    
  39.             wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);   
  40.             wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);     
  41.             WritableWorkbook wbook = Workbook.createWorkbook(os);    
  42.             //设置默认字体  
  43.             WritableFont font=new WritableFont(WritableFont.COURIER, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);  
  44.             WritableCellFormat cFormat = new WritableCellFormat(font);  
  45.             cFormat.setAlignment(jxl.format.Alignment.CENTRE);  
  46.             cFormat.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.GRAY_50);  
  47.             cFormat.setBackground(Colour.WHITE);  
  48.             //2013/7/17  
  49.             //设置信息头栏字体  
  50.             WritableFont font11=new WritableFont(WritableFont.COURIER, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);  
  51.             WritableCellFormat cFormat11 = new WritableCellFormat(font11);  
  52.             cFormat11.setAlignment(jxl.format.Alignment.CENTRE);  
  53.             cFormat11.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.GRAY_50);  
  54.             cFormat11.setBackground(Colour.WHITE);  
  55.               
  56.               
  57.             //写sheet名称   
  58.             WritableSheet wsheet = wbook.createSheet("长江大学职称评审投票信息统计表", 0);  
  59.             //设置单元格默认列宽  
  60.             wsheet.getSettings().setDefaultColumnWidth(10);  
  61.             wsheet.getSettings().setDefaultRowHeight(350);  
  62.             //默认为横向打印//2013/7/17  
  63.             wsheet.setPageSetup(PageOrientation.LANDSCAPE.LANDSCAPE,PaperSize.A4,0.5d,0.5d);  
  64.             /**  
  65.              * lrr 2014-05-27 BEGIN  
  66.              * 功能描述:设置页码格式:第 X 页(共 X 页)  
  67.              */  
  68. //          wsheet.setFooter("", "&P", "");//过时的方法  
  69.             ··HeaderFooter footer = new HeaderFooter();  
  70.             ··Contents contentsFooter = footer.getCentre();    
  71.             contentsFooter.setFontSize(10);    
  72.             contentsFooter.append("第 ");  
  73.             contentsFooter.appendPageNumber();  
  74.             contentsFooter.append(" 页 ( 共 ");  
  75.             contentsFooter.appendTotalPages();    
  76.             contentsFooter.append(" 页 )");  
  77.             //设置打印标题行  
  78. //          wsheet.getSettings().setVerticalFreeze(3);  
  79.             SheetSettings ss=wsheet.getSettings();  
  80.             ss.setFooter(footer);// 设置页脚  
  81.             /**  
  82.              * lrr 2014-05-27 END  
  83.              * 功能描述:设置页码格式:第 X 页(共 X 页)  
  84.              */  
  85. //          ss.setPrintTitles(0,2,0,10);  
  86.             ss.setPrintTitlesRow(0,2);//设置固定打印标题  
  87.             ss.setOrientation(PageOrientation.LANDSCAPE);  
  88.             wsheet.setPageSetup(PageOrientation.LANDSCAPE);  
  89.               
  90.             //标题     
  91.             wsheet.mergeCells(0, 0, 10, 0);  
  92.             /**  
  93.              * lrr 2014-05-24 改 BEGIN  
  94.              * 功能描述:修改投票信息统计表格式  
  95.              */  
  96.             wsheet.addCell(new jxl.write.Label(0, 0, "长江大学职称评审投票信息统计表",wcfFC3));//设置统计表标题  
  97.             WritableFont font1=new WritableFont(WritableFont.COURIER, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);  
  98.             WritableCellFormat cFormat1 = new WritableCellFormat(font1);  
  99.             cFormat1.setAlignment(jxl.format.Alignment.RIGHT);  
  100.             cFormat1.setBorder(Border.NONE,BorderLineStyle.THIN,Colour.GRAY_50);  
  101.             cFormat1.setBackground(Colour.WHITE);  
  102.             wsheet.addCell(new jxl.write.Label(0,1,"评审日期:"+(new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())) ,cFormat1));//合计列  
  103.             /**  
  104.              * lrr 2014-05-24 改 END  
  105.              * 功能描述:修改投票信息统计表格式  
  106.              */           
  107.             /**  
  108.              * 以下为从数据库中查询数据添加到wsheet中  
  109.              */   
  110.             PersistenceManagerOfZcps pmoz=new PersistenceManagerOfZcps();  
  111.             String zsql="select count(*) from view_cprdp where 1=1 "+title;//总人数  
  112.             //String xb_n_sql="select count(*) from view_cprxx where xb='1'";//性别男 人数  
  113.             String hj="";//合计  
  114.             String sql="select * from view_cprdp where 1=1 "+title;  
  115. //          String year=request.getParameter("year");  
  116.             sql+="order by year_dm desc,zcbh asc,ls,ty_num desc,cpbh";  
  117. //            //System.out.println("sql="+sql);  
  118.             ResultSet rs=pmoz.executeQuery(sql);  
  119.             List<zc_cprdpxx> cprdp_list=new ArrayList<zc_cprdpxx>();  
  120.             int zs = 0;//参评人总人数  
  121.             int ns=0;//男参评人数  
  122.             try {  
  123.                 while(rs.next())  
  124.                 {  
  125.                     zc_cprdpxx zp=new zc_cprdpxx();  
  126.                     PersistenceManagerOfZcps pm=new PersistenceManagerOfZcps();  
  127.                         zp.setYear(rs.getString("year_mc")==null?"":rs.getString("year_mc").trim());  
  128.                         zp.setCpbh(rs.getString("cpbh")==null?"":rs.getString("cpbh").trim());  
  129.                         zp.setCpxm(rs.getString("cpxm")==null?"":rs.getString("cpxm").trim());  
  130.                         zp.setLxdh(rs.getString("lxdh")==null?"":rs.getString("lxdh").trim());  
  131.                         zp.setTy_num(rs.getString("ty_num")==null?"":rs.getString("ty_num").trim());  
  132.                         zp.setBty_num(rs.getString("bty_num")==null?"":rs.getString("bty_num").trim());  
  133.                         zp.setQq_num(rs.getString("qq_num")==null?"":rs.getString("qq_num").trim());  
  134.                         zp.setSftg(rs.getString("sftg")==null?"":rs.getString("sftg").trim());  
  135.                         zp.setLs(rs.getString("ls")==null?"":rs.getString("ls").trim());  
  136.                         zp.setZwm(rs.getString("zwm_mc")==null?"":rs.getString("zwm_mc").trim());//2013/7/21  
  137.                         zp.setZcbh(pm.getPureCode_zcm(rs.getString("zcbh")==null?"":rs.getString("zcbh").trim()));    
  138.                         zp.setCprzt(pm.getPureCode("code_zczt", rs.getString("zcbh").substring(1, 2)));  
  139.                         pm.close();  
  140.                         //zp.setNl(rs.getString("nl")==null?"":rs.getString("nl").trim());  
  141.                           
  142.                         cprdp_list.add(zp);               
  143.                 }  
  144.                 rs=pmoz.executeQuery(zsql);  
  145.                 while(rs.next())  
  146.                 {  
  147.                      zs=rs.getInt(1);  
  148.                 }  
  149.                 rs.close();  
  150.                }catch (SQLException e) {  
  151.                  os.close();  
  152.                 // TODO Auto-generated catch block  
  153.                 this.cwts("导出失败,请稍后重试!", response);  
  154.                 e.printStackTrace();  
  155.             }  
  156.             finally  
  157.             {  
  158.                 pmoz.close();//关闭数据连接  
  159.             }  
  160.               
  161.               
  162.               
  163.             //hj="评委总人数为:"+String.valueOf(zs)+"     性别        男:"+String.valueOf(ns)+"   女:"+String.valueOf(zs-ns);  
  164.             wsheet.mergeCells(0, 1, 10, 1);//合并0到10单元格  
  165.             //wsheet.addCell(new jxl.write.Label(0,1,hj ,cFormat));//合计列  
  166.             // 设置固定的打印标题//2013/7/17  
  167.         wsheet.getSettings().setPrintArea(0, 2, 0,7);  
  168. //      SheetSettings setting = wsheet.getSettings();    
  169. //      setting.setPrintTitlesRow(0, 3);   
  170.           
  171.             int i = 0;  
  172.             int j=0;  
  173.             //String[] colum={"年度","参评人编号","单位名称","参评人姓名","性别","申报职称级别","申报职称名称"};  
  174.             //2013/7/17修改  
  175.             String[] colum={"年度", "参评人编号","工作单位","参评人姓名", "申报职称","申报状态","轮数","同意数","不同意数","弃权数","是否通过"};  
  176.             for (i = 0; i <11; i++) {     
  177.                 // 加入行字段名      
  178.                 wsheet.addCell(new jxl.write.Label(i, 2, colum[i],      cFormat11));  
  179.                 }   
  180.             for(i=3;i<cprdp_list.size()+3;i++)  
  181.             {  
  182.                 wsheet.addCell(new jxl.write.Label(0,i, cprdp_list.get(i-3).getYear(),      cFormat));  
  183.                 wsheet.addCell(new jxl.write.Label(1,i, cprdp_list.get(i-3).getCpbh(),      cFormat));  
  184.                 wsheet.addCell(new jxl.write.Label(2,i, cprdp_list.get(i-3).getLxdh(),      cFormat));  
  185.                 wsheet.addCell(new jxl.write.Label(3,i, cprdp_list.get(i-3).getCpxm(),      cFormat));  
  186.                 wsheet.addCell(new jxl.write.Label(4,i, cprdp_list.get(i-3).getZwm(),      cFormat));  
  187.                 wsheet.addCell(new jxl.write.Label(5,i, cprdp_list.get(i-3).getCprzt(),      cFormat));  
  188.                 wsheet.addCell(new jxl.write.Label(6,i, cprdp_list.get(i-3).getLs(),      cFormat));  
  189.                 wsheet.addCell(new jxl.write.Label(7,i, cprdp_list.get(i-3).getTy_num(),      cFormat));  
  190.                 wsheet.addCell(new jxl.write.Label(8,i, cprdp_list.get(i-3).getBty_num(),      cFormat));  
  191.                 wsheet.addCell(new jxl.write.Label(9,i, cprdp_list.get(i-3).getQq_num(),      cFormat));  
  192.                 wsheet.addCell(new jxl.write.Label(10,i, cprdp_list.get(i-3).getSftg(),      cFormat));  
  193.                   
  194.             }   
  195.               
  196.             wsheet.mergeCells(0, i, 10, i);//合并0到10单元格  
  197.             /**  
  198.              * lrr 2014-05-24 增 BEGIN  
  199.              * 功能描述:增加四行:1.监票人 2.评审委员会主任  
  200.              */  
  201.             wsheet.addCell(new jxl.write.Label(0,i,"制表人:"+oprator ,cFormat1));//合计列  
  202.             i = i+1;  
  203.             wsheet.mergeCells(0, i, 10, i+1);//合并两行并同时合并单元格  
  204.             wsheet.addCell(new jxl.write.Label(0,i,"监票人:       " ,cFormat1));//合计列  
  205.             i = i+2;  
  206.             wsheet.mergeCells(0, i, 10, i+1);//合并两行并同时合并单元格  
  207.             wsheet.addCell(new jxl.write.Label(0,i,"评审委员会主任:       " ,cFormat1));//合计列  
  208.             /**  
  209.              * lrr 2014-05-24 增 END  
  210.              * 功能描述:增加两行:1.监票人 2.评审委员会主任  
  211.              */  
  212.             wbook.write();     
  213.             wbook.close();    
  214.               
  215.         } catch (WriteException e) {  
  216.             // TODO Auto-generated catch block  
  217.             e.printStackTrace();  
  218.             //this.cwts("导出失败,请稍后重试!", response);  
  219.         } catch (IOException e1) {  
  220.             // TODO Auto-generated catch block  
  221.             e1.printStackTrace();  
  222.             //this.cwts("导出失败,请稍后重试!", response);  
  223.          }finally{    
  224.              try{  
  225.                  os.close();  
  226.                 } catch (IOException e1) {  
  227.                     // TODO Auto-generated catch block  
  228.                     e1.printStackTrace();  
  229.                     //this.cwts("导出失败,请稍后重试!", response);  
  230.                  }  
  231.         }  
  232.     }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值