jxl的execl导出的相关设置(合并单元格,自动换行等)

来源:互联网 发布:网络上的博客违法吗 编辑:程序博客网 时间:2024/04/29 12:54

这里只是写了关于jxl的相关设置功能的实现功能 

if (type.equals("kehu")) {// 设置宽度
    //定义列宽
   sheet.setColumnView(0, 13);
   sheet.setColumnView(1, 7);
   sheet.setColumnView(2, 16);
   sheet.setColumnView(3, 7);
   sheet.setColumnView(4, 16);
   sheet.setColumnView(5, 7);
   sheet.setColumnView(6, 16);
   sheet.setColumnView(7, 7);
   sheet.setColumnView(8, 16);
   sheet.setColumnView(9, 7);
   sheet.setColumnView(10, 16);
   sheet.setColumnView(11, 7);
   sheet.setColumnView(12, 16);
    //定义行高 
   sheet.setRowView(0, 380);
   sheet.setRowView(1,450);
   sheet.setRowView(2,380); 
   sheet.setRowView(3,400); 
   sheet.setRowView(4,380); 
   sheet.setRowView(5,760); 
  }
  try {
   // 保存内容到execl中
   // 保存表头到cell中
   // 首行:附表
   WritableFont wtf0 = new WritableFont(WritableFont.createFont("宋体"),
     13, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt0 = new WritableCellFormat(wtf0);
   sheet.addCell(new Label(0, 0, "附表",wcfmt0));
   // 第二行
   WritableFont wtf1 = new WritableFont(WritableFont.createFont("黑体"),
     19, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt1 = new WritableCellFormat(wtf1);
   wcfmt1.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   sheet.addCell(new Label(0, 1, "《机动车销售统一发票领用存月报表(汇总)》", wcfmt1));
   sheet.mergeCells(0, 1, 12, 1); // 合并单元格(列、行)第一变量是列,第二变量是行--第三个变量是到第几列,第四变量是行
   //第三行
   WritableFont wtf2 = new WritableFont(WritableFont.createFont("宋体"),12, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt2 = new WritableCellFormat(wtf2);
   WritableCellFormat wcfmt3 = new WritableCellFormat(wtf2);
   WritableCellFormat wcfmt4 = new WritableCellFormat(wtf2);
   wcfmt2.setAlignment(jxl.format.Alignment.LEFT);
   wcfmt2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt3.setAlignment(jxl.format.Alignment.LEFT);
   wcfmt3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt4.setAlignment(jxl.format.Alignment.RIGHT);
   wcfmt4.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   sheet.addCell(new Label(0, 2, "企业名称: " + xForm.getNsrmc(), wcfmt2));
   sheet.addCell(new Label(5, 2, "纳税人识别号: " + xForm.getNsrsbh(),wcfmt3));
   String ssq=xForm.getSsq();
   String year=ssq.substring(0, 4);
   String month=ssq.substring(5, 7);
   sheet.addCell(new Label(10, 2, "所属期限:" + year+"年"+month+"月", wcfmt4));
   sheet.mergeCells(0, 2, 4, 2);
   sheet.mergeCells(5, 2, 9, 2);
   sheet.mergeCells(10, 2, 12, 2);
   // 第四行表头
   WritableFont wtf3 = new WritableFont(WritableFont.createFont("黑体"),
     12, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt5 = new WritableCellFormat(wtf3);
   wcfmt5.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt5.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt5.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   wcfmt5.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt5.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   WritableFont wtf4 = new WritableFont(WritableFont.createFont("黑体"),
     12, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt6 = new WritableCellFormat(wtf4);
   
   wcfmt6.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt6.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt6.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt6.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt6.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   sheet.addCell(new Label(0, 3, "项目", wcfmt5));
   sheet.addCell(new Label(1, 3, "期初库存", wcfmt5));
   sheet.addCell(new Label(3, 3, "本期领购", wcfmt5));
   sheet.addCell(new Label(5, 3, "本期开具", wcfmt5));
   sheet.addCell(new Label(7, 3, "本期交回未开具数量", wcfmt6));
   sheet.addCell(new Label(9, 3, "本期作废或遗失", wcfmt5));
   sheet.addCell(new Label(11, 3, "期末库存", wcfmt5));
   sheet.mergeCells(1, 3, 2, 3); // 合并单元格(列、行)
   sheet.mergeCells(3, 3, 4, 3); // 合并单元格(列、行)
   sheet.mergeCells(5, 3, 6, 3); // 合并单元格(列、行)
   sheet.mergeCells(7, 3, 8, 3); // 合并单元格(列、行)
   sheet.mergeCells(9, 3, 10, 3); // 合并单元格(列、行)
   sheet.mergeCells(11, 3, 12, 3); // 合并单元格(列、行)
   //第五行表头
   WritableFont wtf8 = new WritableFont(WritableFont.createFont("宋体"),11, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt8 = new WritableCellFormat(wtf8);
   wcfmt8.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt8.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt8.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt8.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt8.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   sheet.addCell(new Label(0, 4,"", wcfmt8));
   for(int u=1;u<13;u++){
    jxl.write.Number number = new jxl.write.Number(u,4,u);
    number.setCellFormat(wcfmt8);
    sheet.addCell(number);
   }
   //第六行表头
   WritableFont wtf9= new WritableFont(WritableFont.createFont("宋体"),11, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt9 = new WritableCellFormat(wtf9);
   wcfmt9.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt9.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt9.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt9.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt9.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   wcfmt9.setWrap(true);// 自动换行
   sheet.addCell(new Label(0, 5,"机动车销售统一发票", wcfmt9));
   for(int v=1;v<13;v++){
    if(v%2==0){
     sheet.addCell(new Label(v, 5,"起止号码", wcfmt9));
    }else{
     sheet.addCell(new Label(v, 5,"份数", wcfmt9));
    }
   }
   WritableFont wtf7 = new WritableFont(WritableFont.createFont("宋体"),
     7, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt7 = new WritableCellFormat(wtf7);
   wcfmt7.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt7.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt7.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt7.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt7.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   WritableFont wtf10 = new WritableFont(WritableFont.createFont("宋体"),
     11, WritableFont.NO_BOLD, false);
   WritableCellFormat wcfmt10 = new WritableCellFormat(wtf10);
   wcfmt10.setAlignment(jxl.format.Alignment.CENTRE);
   wcfmt10.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   wcfmt10.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
   // 设置右边框线为实线
   wcfmt10.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   wcfmt10.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
   // 设置千分隔符
    // 设置垂直对齐为居中对齐
   // wcfT.setAlignment(jxl.format.Alignment.RIGHT);
    // 设置顶部边框线为实线(默认是黑色--也可以设置其他颜色)
    // wcfT.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
    // 设置右边框线为实线
    // wcfT.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
   for(int m=0;m<str.length;m++){
    String[] str1=str[m].split(",",13);
    String ss = "";
    sheet.setRowView(m+6,380);
    for (int j = 0; j < 13; j++) {
        ss=str1[j].toString().trim();
        if(ss.indexOf("没有任何数据!")!=0){
      if(j==2||j==4||j==6||j==8||j==10||j==12){
       sheet.addCell(new Label(j, m + 6, ss+"", wcfmt7));
      }
      else{
       if(ss.equals("")||ss.equals("合计")){
        sheet.addCell(new Label(j, m + 6, ss+"", wcfmt10));
       }
       else{
        jxl.write.Number number1 = new jxl.write.Number(j,m+6,Integer.parseInt(ss.trim()));
        number1.setCellFormat(wcfmt10);
           sheet.addCell(number1);
       }
      }
        }else{
         j=12;
         m=str.length-1;
        }
    }
   }

原创粉丝点击