码迷,mamicode.com
首页 > 其他好文 > 详细

POI Excel表格合并,边框设置

时间:2017-11-28 19:50:57      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:string   否则   new   str   oid   create   class   res   style   

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);  
 

 

POI Excel表格合并,边框设置

标签:string   否则   new   str   oid   create   class   res   style   

原文地址:http://www.cnblogs.com/mr-wuxiansheng/p/7911521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!