• POI Excel表格合并,边框设置


    Java代码  收藏代码
    1. RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);  
    2. RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);  
    3. RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);  
    4. RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);  
     
    上面是加边框的核心代码,为单元格的上,下左,右四边加边框!我测试在单个合并单元格的时候,是没有什么问题的!但是当我用下面代码的时候会出现有的边框没有
    Java代码  收藏代码
    1. Row row=sheet.createRow((short)0);  
    2.       ImageUtil.createCell("排序", 0, sctext, row);  
    3.       CellRangeAddress CellRangeAddress1 = new CellRangeAddress(0,2,0,0);  
    4.       sheet.addMergedRegion(CellRangeAddress1);  
    5.   
    6.       RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);  
    7.       RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);  
    8.       RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);  
    9.       RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);  
    10.       ImageUtil.createCell("项目名称", 1, sctext, row);  
    11.       CellRangeAddress CellRangeAddress2 = new CellRangeAddress(0,2,1,1);  
    12.       sheet.addMergedRegion(CellRangeAddress2);  
    13.         
    14.       ImageUtil.createCell("姓名", 2, sctext, row);  
    15.       CellRangeAddress CellRangeAddress3 = new CellRangeAddress(0,2,2,2);  
    16.       sheet.addMergedRegion(CellRangeAddress3);     
    17.          
    18.       ImageUtil.createCell("各指标完成情况总数据", 3, sctext, row);  
    19.       CellRangeAddress CellRangeAddress4 = new CellRangeAddress(0,0,3,8);  
    20.       sheet.addMergedRegion(new CellRangeAddress(0,0,3,8));     
    21.        
    22.       ImageUtil.createCell("各指标完成率", 9, sctext, row);  
    23.       CellRangeAddress CellRangeAddress5 = new CellRangeAddress(0,0,9,14);  
    24.       sheet.addMergedRegion(CellRangeAddress5);    
    25.       
    26.       Row row2=sheet.createRow((short)1);  
    27.       ImageUtil.createCell("覆盖面", 3, sctext, row2);  
    28.       CellRangeAddress CellRangeAddress6 = new CellRangeAddress(1,1,3,4);  
    29.       sheet.addMergedRegion(CellRangeAddress6);  
    30.         
    31.       ImageUtil.createCell("频次", 5, sctext, row2);  
    32.       CellRangeAddress CellRangeAddress7 = new CellRangeAddress(1,1,5,6);  
    33.       sheet.addMergedRegion(CellRangeAddress7);  

     结果如下图:

    最后是顺序问题导致的.其原因是因为在编辑单元格的时候,后者覆盖了前者的style.

    解决办法:

    封装了一个方法:

    Java代码  收藏代码
    1. public void setBorder(CellRangeAddress cellRangeAddress, Sheet sheet,  
    2.             Workbook wb) throws Exception {  
    3.         RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);  
    4.         RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);  
    5.         RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);  
    6.         RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);  
    7.           
    8. }  
     然后将调用该方法的步骤放到最后,记得是最后,就是全部单元格合并之后,否则会覆盖的,如下:
    Java代码  收藏代码
    1. super.setBorder(CellRangeAddress1, sheet, book);  
    2. super.setBorder(CellRangeAddress2, sheet, book);  
    3. super.setBorder(CellRangeAddress3, sheet, book);  
    4. super.setBorder(CellRangeAddress4, sheet, book);  
    5. super.setBorder(CellRangeAddress5, sheet, book);  
    6. super.setBorder(CellRangeAddress6, sheet, book);  
    7. super.setBorder(CellRangeAddress7, sheet, book);  
    8. super.setBorder(CellRangeAddress8, sheet, book);  
    9. super.setBorder(CellRangeAddress9, sheet, book);  
    10. super.setBorder(CellRangeAddress10, sheet, book);  
    11. super.setBorder(CellRangeAddress11, sheet, book);  
    12. super.setBorder(CellRangeAddress12, sheet, book);  
    13. super.setBorder(CellRangeAddress13, sheet, book);  
    14. super.setBorder(CellRangeAddress14, sheet, book);  
    15. super.setBorder(CellRangeAddress15, sheet, book);  
    16. super.setBorder(CellRangeAddress16, sheet, book);  
     
  • 相关阅读:
    为什么要用设计模式?先看看6大原则(一)
    git版本库的创建和yaf框架环境的部署
    laravel日常小问题
    Session store not set on request.
    phpstudy集成环境安装lavarel
    html中提交表单并实现不跳转页面处理返回值
    document load 与document ready的区别
    定时器优化
    放大镜
    子组件调用父组件的方法并传递数据
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/7911521.html
Copyright © 2020-2023  润新知