POI 合并单元格 及 注意事项

POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet。

跨第1行第1个到第2个单元格的操作为
sheet.addMergedRegion(new Region(0,(short)0,0,(short)1));

跨第1行第1个到第2行第1个单元格的操作为
sheet.addMergedRegion(new Region(0,(short)0,1,(short)0));


重点注意事项:
1.单元格CELL和ROW对象下标都是从0开始的。
2.单元格合并时Region(1,2,3,4)第1个值的行号必须要比3位置的行号小,如果大于3就不能正常合并单元格
3.合并单元格的时候要合并的单单元格必须先创建,这样方便后面再次获取这个单元格来填充数据,主要就是因为合并时不能由后向前进行合并引起的。



完整的例子程序包含了输出

Java代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <jsp:directive.page import="com.yuantiao.ipub.ytcms.service.ArticleService"/>  
  3. <jsp:directive.page import="com.yuantiao.ipub.ytcms.stat.bean.ArticleInfo"/>  
  4. <jsp:directive.page import="org.apache.poi.hssf.usermodel.*"/>  
  5. <jsp:directive.page import="org.apache.poi.hssf.util.HSSFColor"/>  
  6. <jsp:directive.page import="org.apache.poi.hssf.util.Region"/>  
  7. <jsp:directive.page import="java.text.SimpleDateFormat"/>  
  8. <%!//自定义的方法,主要用于插入中文的字符格式  
  9.   private void setGB2312String(HSSFCell Cell, String Value) throws  
  10.       Exception {  
  11.     Cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
  12.     Cell.setCellValue(Value);  
  13.   }   
  14. %>  
  15.   
  16.   
  17. <%  
  18. ArticleService articleService = new ArticleService();  
  19. ArticleInfo articleInfo;  
  20.   
  21. String beginTime = request.getParameter("beginTime");  
  22. String endTime = request.getParameter("endTime");  
  23. String stateType = request.getParameter("stateType");  
  24.   
  25. if(beginTime==null)  
  26. {  
  27.     beginTime = "";  
  28. }  
  29. if(endTime == null)  
  30. {  
  31.     endTime = "";  
  32. }  
  33. int statetype = stateType==null||stateType.equals("")?1:Integer.parseInt(stateType);  
  34. //System.out.println(beginTime+","+endTime+","+stateType);  
  35. ArrayList list = articleService.getArticleInfoList(statetype,beginTime,endTime);  
  36.   
  37. if(list.size() == 0 ){  
  38. %>  
  39.     <script>  
  40.         alert("无满足要求的信息.");  
  41.         window.close();  
  42.     </script>  
  43. <%  
  44.     return;  
  45. }  
  46. java.sql.Date bb=new java.sql.Date(System.currentTimeMillis());    
  47. SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义日期用格式化工具  
  48.   
  49. int index = 0;//行标记,记录创建到第几行了  
  50.   
  51. HSSFWorkbook wb = new HSSFWorkbook();   
  52. HSSFSheet s = wb.createSheet();  
  53. wb.setSheetName(0"MySheet");  
  54.   
  55. HSSFHeader header = s.getHeader();  
  56.   
  57. HSSFRow trow=null;  
  58. HSSFCell tcell=null;  
  59. HSSFCell cell = null;  
  60. HSSFFont font = wb.createFont();  
  61. HSSFFont font2 = wb.createFont();  
  62. HSSFCellStyle cellStyle = wb.createCellStyle();  
  63. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体加粗                                  
  64. font.setFontHeight((short380);        // 设置字体大小  
  65. font.setFontName("宋体");      // 设置单元格字体     
  66.   
  67. HSSFCellStyle cellStyle2 = wb.createCellStyle();  
  68. font2.setColor(HSSFFont.COLOR_RED); // 设置单元格字体的颜色.  
  69.   
  70.   
  71. HSSFCellStyle cellStyle1_2 = wb.createCellStyle();  
  72.   
  73. cellStyle1_2.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);  
  74. cellStyle1_2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  75. cellStyle1_2.setBorderBottom((short)1);  
  76. cellStyle1_2.setBorderLeft((short)1);  
  77. cellStyle1_2.setBorderRight((short)1);  
  78. cellStyle1_2.setBorderTop((short)1);  
  79. cellStyle1_2.setBottomBorderColor(HSSFColor.BLACK.index);  
  80.   
  81. HSSFCellStyle cellStyle1_3 = wb.createCellStyle();  
  82.   
  83. cellStyle1_3.setBorderBottom((short)1);  
  84. cellStyle1_3.setBorderLeft((short)1);  
  85. cellStyle1_3.setBorderRight((short)1);  
  86. cellStyle1_3.setBorderTop((short)1);  
  87. cellStyle1_3.setBottomBorderColor(HSSFColor.BLACK.index);  
  88.   
  89. HSSFCellStyle cellStyle1_4 = wb.createCellStyle();  
  90. cellStyle1_4.setBorderBottom((short)1);  
  91. cellStyle1_4.setBorderLeft((short)1);  
  92. cellStyle1_4.setBorderRight((short)1);  
  93. cellStyle1_4.setBorderTop((short)1);  
  94. cellStyle1_4.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
  95. cellStyle1_4.setBottomBorderColor(HSSFColor.BLACK.index);  
  96.    
  97. //创建第一行和第二行的文字信息  
  98. /**********************标题第一行***************************/  
  99. //创建第一行的标题  
  100. int cellIndex = 0;//CELL的位置标记  
  101.   
  102. cellIndex = 0;//CELL的位置标记  
  103. trow=s.createRow(index++);//创建行  
  104. trow.setHeight((short)400);  
  105. tcell=trow.createCell((short)cellIndex++);  
  106. tcell.setCellStyle(cellStyle1_2);  
  107. setGB2312String(tcell,"序号");  
  108.   
  109. tcell=trow.createCell((short)cellIndex++);  
  110. tcell.setCellStyle(cellStyle1_2);  
  111. setGB2312String(tcell,Integer.parseInt(stateType)==1?"作者":"出处");  
  112.   
  113. tcell=trow.createCell((short)cellIndex++);  
  114. tcell.setCellStyle(cellStyle1_2);  
  115. setGB2312String(tcell,"标题");  
  116.   
  117. tcell=trow.createCell((short)cellIndex++);  
  118. tcell.setCellStyle(cellStyle1_2);  
  119. setGB2312String(tcell,"主栏目");  
  120.   
  121. tcell=trow.createCell((short)cellIndex++);  
  122. tcell.setCellStyle(cellStyle1_2);  
  123. setGB2312String(tcell,"录入时间");  
  124.   
  125. tcell=trow.createCell((short)cellIndex++);  
  126. tcell.setCellStyle(cellStyle1_2);  
  127. setGB2312String(tcell,"文章分值");  
  128.   
  129. tcell=trow.createCell((short)cellIndex++);  
  130. tcell.setCellStyle(cellStyle1_2);  
  131. setGB2312String(tcell,"分值合计1");  
  132.   
  133.    
  134.               
  135. String pName = "";  
  136. String equalStr = "";//主判断  
  137. int totalScore = 0,loopQuantity = 0;  
  138. boolean flag = false;//判断标记  
  139. ArticleInfo tempArticleInfo = null;  
  140.   
  141. for(int i=0;i<list.size();i++)  
  142. {  
  143.     articleInfo = (ArticleInfo)list.get(i);   
  144.       
  145.       
  146.     //如果对比变量为空时进行初始化  
  147.     if("".equals(equalStr)){  
  148.     //如果类型为1时按作者进行统计和判断  
  149.         if(statetype == 1){  
  150.                 equalStr = articleInfo.getAuthoer();  
  151.         }else{  
  152.             equalStr = articleInfo.getQuote();  
  153.         }  
  154.     }  
  155.       
  156.     //开始循环生成EXCEL行数据  
  157.     cellIndex = 0;//初始化CELL下标  
  158.     trow=s.createRow(index++);//创建行  
  159.     trow.setHeight((short)400);  
  160.     tcell=trow.createCell((short)cellIndex++);  
  161.     tcell.setCellStyle(cellStyle1_3);  
  162.     setGB2312String(tcell,""+(i+1));  
  163.       
  164.     tcell=trow.createCell((short)cellIndex++);  
  165.     tcell.setCellStyle(cellStyle1_3);  
  166.     setGB2312String(tcell,statetype == 1? articleInfo.getAuthoer() : articleInfo.getQuote());//判断是显示作者信息还是出处信息  
  167.       
  168.     tcell=trow.createCell((short)cellIndex++);  
  169.     tcell.setCellStyle(cellStyle1_3);  
  170.     setGB2312String(tcell,articleInfo.getTitle());  
  171.       
  172.     tcell=trow.createCell((short)cellIndex++);  
  173.     tcell.setCellStyle(cellStyle1_3);  
  174.     setGB2312String(tcell,articleInfo.getSiteName());  
  175.       
  176.     tcell=trow.createCell((short)cellIndex++);  
  177.     tcell.setCellStyle(cellStyle1_3);  
  178.     setGB2312String(tcell,dateFormat.format(articleInfo.getRecordDate()));  
  179.       
  180.     tcell=trow.createCell((short)cellIndex++);  
  181.     tcell.setCellStyle(cellStyle1_3);  
  182.     setGB2312String(tcell,""+articleInfo.getScore());  
  183.       
  184.     tcell=trow.createCell((short)cellIndex++);  
  185.     tcell.setCellStyle(cellStyle1_3);  
  186.     setGB2312String(tcell,"");  
  187.       
  188.           
  189.           
  190.         tempArticleInfo = null//每次循环进行初始化,以便知道什么时候到达最后条记录  
  191.         flag = false;  
  192.         if(i+1 != list.size()){  
  193.                 tempArticleInfo = (ArticleInfo)list.get(i+1);     
  194.         }  
  195.           
  196.           
  197.         //进行统一判断,方便后面使用,简化代码  
  198.         if(tempArticleInfo != null){  
  199.             if(statetype == 1){  
  200.                     flag = equalStr.equals(tempArticleInfo.getAuthoer());  
  201.             }else{  
  202.                     flag = equalStr.equals(tempArticleInfo.getQuote());  
  203.             }  
  204.         }  
  205.   
  206.         //下一条记录不等于当前的记录对比值时进行相应的处理  
  207.         if(!flag || tempArticleInfo == null){  
  208.                 totalScore += articleInfo.getScore();  
  209.             loopQuantity ++;  
  210.                   
  211.                 //进行跨列处理  
  212.                 //#########注意:跨行操作时Region(1,2,3,4)第1个值的行号必须要比3位置的行号小,如果大于3就不能正常合并单元格  
  213.                  s.addMergedRegion(new Region(index-loopQuantity,(short)(cellIndex-1),index-1,(short)(cellIndex-1)));  
  214.                       
  215.                     //获取最先需要跨的CELL,然后把值加入  
  216.                  tcell = s.getRow(index-loopQuantity).getCell((short)(cellIndex-1));  
  217.                     setGB2312String(tcell,""+totalScore);  
  218.                       
  219.                   
  220.                 //清空统计数据  
  221.                 totalScore = 0;  
  222.                 loopQuantity = 0;  
  223.         }else{  
  224.             totalScore += articleInfo.getScore();  
  225.             loopQuantity ++;  
  226.         }  
  227.       
  228.   
  229.       
  230.       
  231. }  
  232.   
  233.     //s.addMergedRegion(new Region(1,(short)(0),0,(short)(0)));  
  234.   
  235.     pName="栏目统计表";  
  236.     response.reset();  
  237.     response.setContentType("application/x-msdownload");  
  238.     response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");  
  239.     ServletOutputStream outStream=null;  
  240.   
  241.     try{  
  242.         outStream = response.getOutputStream();  
  243.         wb.write(outStream);  
  244.     }catch(Exception e)  
  245.     {  
  246.      e.printStackTrace();  
  247.     }finally{  
  248.         outStream.close();  
  249.     }  
  250. %>  
new Region 这个已经过时了,现在是用addMergedRegion(new CellRangeAddress(row,row,col,col);

 

你可能感兴趣的:(poi,合并单元格,注意事项,及)