java 打印表格文件

jar 包在资源中 jxl.jar

 

 

public String printInfo(){
HttpServletResponse response=getResponse();
  
  jxl.write.Label label;   
        WritableWorkbook workbook;
        jxl.write.WritableWorkbook wwb = null;
        OutputStream out=null;

  try {
   out = response.getOutputStream();
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
        try {

                 //设置文件名称
          response.setHeader("Content-disposition","attachment; filename=" + System.currentTimeMillis()+".xls");  

         //设置字符集     
       response.setContentType("application/msexcel;charset=UTF-8");
       wwb = Workbook.createWorkbook(out);

          //创建样式对象
       WritableFont titleFont=new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD);
       WritableCellFormat titleFormat= new WritableCellFormat();
    WritableCellFormat contentFormat=new WritableCellFormat();
       titleFormat.setFont(titleFont);
       titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
     
    WritableFont contentFont=new WritableFont(WritableFont.TIMES,10);
    contentFormat.setFont(contentFont);
    contentFormat.setAlignment(jxl.format.Alignment.CENTRE);
    
    titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);   
    contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

    jxl.write.WritableSheet ws = wwb.createSheet("综合查询 ", 1);
       jxl.write.Label labelName = new jxl.write.Label(0, 0, "综合查询 ",titleFormat);
    ws.mergeCells(0, 0, 14, 0);
    ws.addCell(labelName);
   
    ws.setColumnView(0, 7);
    ws.setColumnView(1, 8);
    ws.setColumnView(2, 10);
    ws.setColumnView(3, 8);
    ws.setColumnView(4, 7);
    ws.setColumnView(5, 7);
    ws.setColumnView(6, 10);
    ws.setColumnView(7, 11);
    ws.setColumnView(8, 12);
    ws.setColumnView(9, 12);
    ws.setColumnView(10, 9);
    ws.setColumnView(11, 8);
    ws.setColumnView(12, 8);
    ws.setColumnView(13, 8);
    ws.setColumnView(14, 17);
    jxl.write.Label column1 = new jxl.write.Label(0, 1, "序号",contentFormat);
    jxl.write.Label column2 = new jxl.write.Label(1, 1, "年级",contentFormat);
    jxl.write.Label column3 = new jxl.write.Label(2, 1, "班级",contentFormat);
    jxl.write.Label column4 = new jxl.write.Label(3, 1, "姓名",contentFormat);
    jxl.write.Label column5 = new jxl.write.Label(4, 1, "性别",contentFormat);
    jxl.write.Label column6 = new jxl.write.Label(5, 1, "考号",contentFormat);
    jxl.write.Label column7 = new jxl.write.Label(6, 1, "条码",contentFormat);
    jxl.write.Label column8 = new jxl.write.Label(7, 1, "家长手机",contentFormat);
    jxl.write.Label column9 = new jxl.write.Label(8, 1, "家庭住址",contentFormat);
    jxl.write.Label column10 = new jxl.write.Label(9, 1, "毕业学校",contentFormat);
    jxl.write.Label column11 = new jxl.write.Label(10, 1, "考试成绩",contentFormat);
    jxl.write.Label column12 = new jxl.write.Label(11, 1, "交服务费",contentFormat);
    jxl.write.Label column13 = new jxl.write.Label(12, 1, "交学费",contentFormat);
    jxl.write.Label column14 = new jxl.write.Label(13, 1, "校服尺寸",contentFormat);
    jxl.write.Label column15 = new jxl.write.Label(14, 1, "添加时间",contentFormat);
    ws.addCell(column1);
    ws.addCell(column2);
    ws.addCell(column3);
    ws.addCell(column4);
    ws.addCell(column5);
    ws.addCell(column6);
    ws.addCell(column7);
    ws.addCell(column8);
    ws.addCell(column9);
    ws.addCell(column10);
    ws.addCell(column11);
    ws.addCell(column12);
    ws.addCell(column13);
    ws.addCell(column14);
    ws.addCell(column15);
    PageUtil page =new PageUtil(10000,1);
    PageUtil<Object[]> pageUtil= searchStudentInfoBean.getStudentInfo(page, name, grade, type, start, end);
    if (pageUtil!=null && pageUtil.getList().size()>0) {
     int j=2;
     for (int i = 0; i < pageUtil.getList().size(); i++) {
      Object[] ob = pageUtil.getList().get(i);
      if (ob[0]==null) {
       ob[0]="";
      }
      if (ob[1]==null) {
       ob[1]="";
      }
      if (ob[2]==null) {
       ob[2]="";
      }
      if (ob[3]==null) {
       ob[3]="";
      }
      if (ob[4]==null) {
       ob[4]="";
      }
      if (ob[5]==null) {
       ob[5]="";
      }
      if (ob[6]==null) {
       ob[6]="";
      }
      if (ob[7]==null) {
       ob[7]="";
      }
      if (ob[8]==null) {
       ob[8]="";
      }
      if (ob[9]==null) {
       ob[9]="";
      }
      if (ob[10]==null) {
       ob[10]="";
      }
      if (ob[11]==null) {
       ob[11]="";
      }
      if (ob[12]==null) {
       ob[12]="";
      }
      if (ob[13]==null) {
       ob[13]="";
      }
      jxl.write.Label column20 = new jxl.write.Label(0, j, i+1+"",contentFormat);
      jxl.write.Label column21 = new jxl.write.Label(1, j, ob[0]+"",contentFormat);
      jxl.write.Label column22 = new jxl.write.Label(2, j, ob[1]+"",contentFormat);
      jxl.write.Label column23 = new jxl.write.Label(3, j, ob[2]+"",contentFormat);
      jxl.write.Label column24 = new jxl.write.Label(4, j, ob[3]+"",contentFormat);
      jxl.write.Label column25 = new jxl.write.Label(5, j, ob[4]+"",contentFormat);
      jxl.write.Label column26 = new jxl.write.Label(6, j, ob[5]+"",contentFormat);
      jxl.write.Label column27 = new jxl.write.Label(7, j, ob[6]+"",contentFormat);
      jxl.write.Label column28 = new jxl.write.Label(8, j, ob[7]+"",contentFormat);
      jxl.write.Label column29 = new jxl.write.Label(9, j, ob[8]+"",contentFormat);
      jxl.write.Label column30 = new jxl.write.Label(10, j, ob[9]+"",contentFormat);
      jxl.write.Label column31 = new jxl.write.Label(11, j, ob[10]+"",contentFormat);
      jxl.write.Label column32 = new jxl.write.Label(12, j, ob[11]+"",contentFormat);
      jxl.write.Label column33 = new jxl.write.Label(13, j, ob[12]+"",contentFormat);
      jxl.write.Label column34 = new jxl.write.Label(14, j, ob[13]+"",contentFormat);
      ws.addCell(column20);
      ws.addCell(column21);
      ws.addCell(column22);
      ws.addCell(column23);
      ws.addCell(column24);
      ws.addCell(column25);
      ws.addCell(column26);
      ws.addCell(column27);
      ws.addCell(column28);
      ws.addCell(column29);
      ws.addCell(column30);
      ws.addCell(column31);
      ws.addCell(column32);
      ws.addCell(column33);
      ws.addCell(column34);
      j++;
     }
     wwb.write();
    }
        }catch (Exception e) {
   // TODO: handle exception
         e.printStackTrace();
  }
        try {
   wwb.close();
   out.flush();      
         out.close(); 
  } catch (Exception e) {
   // TODO Auto-generated catch block
   log.error("出错:", e);
  }
  return null;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值