zoukankan      html  css  js  c++  java
  • java POI导出excel,合并单元格边框消失

    业务是导出一个报表,要求有一个跨多列的表头,肯定要用到合并单元格,但合并后边框消失。网上的一些解决办法是重写合并单元格方法,但弄清楚原因后,其实没必要。

    原来是这样的:

    合并后就第一个有边框,其余全成空白了,上代码:

    HSSFCellStyle style = wb.createCellStyle(); 创建样式
      style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); //字体右对齐
      style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
      style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
      style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
      style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
    HSSFRow row = sheet.createRow(1);
    HSSFCell cel = row.createCell(0);
      cel.setCellValue("编制人:" + "admin" + "         日期:" + sdf.format(new Date()));
      cel.setCellStyle(style);
      for (int j = 1; j <= 11; j++) {
        cel = row.createCell(j);
        cel.setCellStyle(style); //style为带边框的样式 上面有定义
        cel.setCellValue("");
      }
    

    运行后: 

    哈哈!完美的解决了

    分析:创建的行的的时候只row.createCell(0),当然只有第一列有边框,其余没边框了,然后一合并,结果就显而易见出现图一的情况了。但要是创建等多的单元格,每个单元格都加上边框,再合并就没事了。

    还是搞清原理比较好。还有一个根据单元格内容自适应高宽的问题,官方给的有问题,还没解决。>_<

  • 相关阅读:
    笔记-归并排序
    Repeated Substring Pattern
    Assign Cookies
    Number of Boomerangs
    Paint Fence
    Path Sum III
    Valid Word Square
    Sum of Two Integers
    Find All Numbers Disappeared in an Array
    First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/yangchas/p/8311656.html
Copyright © 2011-2022 走看看