欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poi设置excel表格边框

程序员文章站 2023-12-29 11:43:46
...
RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);
RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);
RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);
RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);
 
上面是加边框的核心代码,为单元格的上,下左,右四边加边框!我测试在单个合并单元格的时候,是没有什么问题的!但是当我用下面代码的时候会出现有的边框没有
 Row row=sheet.createRow((short)0);
       ImageUtil.createCell("排序", 0, sctext, row);
       CellRangeAddress CellRangeAddress1 = new CellRangeAddress(0,2,0,0);
       sheet.addMergedRegion(CellRangeAddress1);

       RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);
       RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);
       RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);
       RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);
       ImageUtil.createCell("项目名称", 1, sctext, row);
       CellRangeAddress CellRangeAddress2 = new CellRangeAddress(0,2,1,1);
       sheet.addMergedRegion(CellRangeAddress2);
       
       ImageUtil.createCell("姓名", 2, sctext, row);
       CellRangeAddress CellRangeAddress3 = new CellRangeAddress(0,2,2,2);
       sheet.addMergedRegion(CellRangeAddress3);   
        
       ImageUtil.createCell("各指标完成情况总数据", 3, sctext, row);
       CellRangeAddress CellRangeAddress4 = new CellRangeAddress(0,0,3,8);
       sheet.addMergedRegion(new CellRangeAddress(0,0,3,8));   
      
       ImageUtil.createCell("各指标完成率", 9, sctext, row);
       CellRangeAddress CellRangeAddress5 = new CellRangeAddress(0,0,9,14);
       sheet.addMergedRegion(CellRangeAddress5);  
     
       Row row2=sheet.createRow((short)1);
       ImageUtil.createCell("覆盖面", 3, sctext, row2);
       CellRangeAddress CellRangeAddress6 = new CellRangeAddress(1,1,3,4);
       sheet.addMergedRegion(CellRangeAddress6);
       
       ImageUtil.createCell("频次", 5, sctext, row2);
       CellRangeAddress CellRangeAddress7 = new CellRangeAddress(1,1,5,6);
       sheet.addMergedRegion(CellRangeAddress7);

 结果如下图:

poi设置excel表格边框
            
    
    博客分类: 报表excelexcel报表javaexcel合并单元格 报表excel合并单元格 

 

最后在没有办法的情况下,只能一点点调试。结果确十分坑爹,折腾了我好几个小时上网看资料。最后原来是以为顺序问题导致的.

  其原因是因为在编辑单元格的时候,后者覆盖了前者的style.

解决办法:

封装了一个方法:

 

public void setBorder(CellRangeAddress cellRangeAddress, Sheet sheet,
			Workbook wb) throws Exception {
		RegionUtil.setBorderLeft(1, cellRangeAddress, sheet, wb);
		RegionUtil.setBorderBottom(1, cellRangeAddress, sheet, wb);
		RegionUtil.setBorderRight(1, cellRangeAddress, sheet, wb);
		RegionUtil.setBorderTop(1, cellRangeAddress, sheet, wb);
		
}
 然后将调用该方法的步骤放到最后,记得是最后,就是全部单元格合并之后,否则会覆盖的,如下:
super.setBorder(CellRangeAddress1, sheet, book);
super.setBorder(CellRangeAddress2, sheet, book);
super.setBorder(CellRangeAddress3, sheet, book);
super.setBorder(CellRangeAddress4, sheet, book);
super.setBorder(CellRangeAddress5, sheet, book);
super.setBorder(CellRangeAddress6, sheet, book);
super.setBorder(CellRangeAddress7, sheet, book);
super.setBorder(CellRangeAddress8, sheet, book);
super.setBorder(CellRangeAddress9, sheet, book);
super.setBorder(CellRangeAddress10, sheet, book);
super.setBorder(CellRangeAddress11, sheet, book);
super.setBorder(CellRangeAddress12, sheet, book);
super.setBorder(CellRangeAddress13, sheet, book);
super.setBorder(CellRangeAddress14, sheet, book);
super.setBorder(CellRangeAddress15, sheet, book);
super.setBorder(CellRangeAddress16, sheet, book);
 
 
另外也参考了以下朋友提供的资源,非常感谢
特别应该感谢是下面这位仁兄:

上一篇:

下一篇: