aspose合并单元格

aspose合并单元格工作中遇到处理 word 模板用到了 aspose 插件 其中遇到了合并单元格 其中分为上下单元格合并 以及左右单元格合并 1 一行中相邻单元格合并 合并所有左右相邻单元格一致的行 publicstatic HashMap String Object m1 Stringfilepa throwsExcept Maprs getcf m1 Iteratoriter rs keySet itera String Object

工作中遇到处理word模板用到了aspose插件,其中遇到了合并单元格,其中分为上下单元格合并,以及左右单元格合并

1.一行中相邻单元格合并

 //合并所有左右相邻单元格一致的行 public static void hbCells( HashMap 
  
    m1,String filepath ) throws Exception{ Map rs=getcf(m1); Iterator iterator = rs.keySet().iterator(); Map rr = new HashMap(); //获取重复数据的合并行的坐标 while (iterator.hasNext()){ Object value = iterator.next(); List 
   
     result = (List) rs.get(value); //System.out.println(result); List list = new ArrayList(); List 
    
      rowlist = new ArrayList 
     
       (); List 
      
        celllist = new ArrayList 
       
         (); for( String js:result){ String jj= getTextCellRow(filepath,js); int row=Integer.parseInt(jj.split(",")[0]); int cell=Integer.parseInt(jj.split(",")[1]); rowlist.add(row); celllist.add(cell); } list=getCellQJ(rowlist,celllist); if(list.size()>0){ rr.put(value, list); } } //2处理合并 Iterator ite = rr.keySet().iterator(); while (ite.hasNext()){ Object value = ite.next(); List 
        
          result = (List) rr.get(value); hbCell(filepath,result,filepath); } } public static List 
         
           getCellQJ(List 
          
            rowList,List 
           
             cellList) throws Exception{ List 
            
              list = new ArrayList 
             
               (); int maxrow= Collections.max(rowList); int maxcell= Collections.max(cellList); int minrow= Collections.min(rowList); int mincell= Collections.min(cellList); if(maxrow==minrow){ int size=rowList.size(); int fw=maxcell-mincell+1; //连续的单元格 if(size==fw){ list.add(mincell+","+minrow); list.add(maxcell+","+minrow); } } return list; } //合并左右单元 public static void hbCell( String filepath, List 
              
                result,String savefile) throws Exception{ int row=Integer.parseInt(result.get(0).split(",")[1]); int mincell=Integer.parseInt(result.get(0).split(",")[0]); int maxCell=Integer.parseInt(result.get(1).split(",")[0]); Document doc = new Document(filepath); Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); //第1个表格 // We want to merge the range of cells found in between these two cells. Cell cellStartRange = table.getRows().get(row).getCells().get(mincell); //第2行第1列 Cell cellEndRange = table.getRows().get(row).getCells().get(maxCell); //第3行第1列 // Merge all the cells between the two specified cells into one. mergeCellshx(cellStartRange, cellEndRange); doc.save(savefile); } //合并一行的相邻单元格 public static void mergeCellshx(Cell startCell, Cell endCell) { Table parentTable = startCell.getParentRow().getParentTable(); double Cellwidth=0;; //Table parentTable2 = endCell.getParentRow().getParentTable(); // Find the row and cell indices for the start and end cell. Point startCellPos = new Point(startCell.getParentRow().indexOf(startCell), parentTable.indexOf(startCell.getParentRow())); Point endCellPos = new Point(endCell.getParentRow().indexOf(endCell), parentTable.indexOf(endCell.getParentRow())); // Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell. Rectangle mergeRange = new Rectangle( Math.min(endCellPos.x, startCellPos.x), Math.min(endCellPos.y, endCellPos.y), Math.abs(endCellPos.x-startCellPos.x)+1 , 1);//1,2,0,18 h,w,x,y for (Row row : parentTable.getRows()) { for (Cell cell : row.getCells()) { Point currentPos = new Point(row.indexOf(cell), parentTable.indexOf(row)); // Check if the current cell is inside our merge range then merge it.0,18 1,18 if (mergeRange.contains(currentPos)) { Cellwidth+= cell.getCellFormat().getWidth(); if (currentPos.y == mergeRange.y) cell.getCellFormat().setVerticalMerge(CellMerge.FIRST); else cell.getCellFormat().setVerticalMerge(CellMerge.NONE); if (currentPos.x==mergeRange.x) cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST); else cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS); } } } startCell.getCellFormat().setWidth(Cellwidth); } 
               
              
             
            
           
          
         
        
       
      
     
    
  

2.处理合并上下单元格

//合并所有上下单元格一致的行 public static void hbRows( HashMap 
  
    m1,String filepath ) throws Exception{ Map rs=getcf(m1); Iterator iterator = rs.keySet().iterator(); Map rr = new HashMap(); Map cr = new HashMap(); //获取重复数据的合并行的坐标 while (iterator.hasNext()){ Object value = iterator.next(); List 
   
     result = (List) rs.get(value); //System.out.println(result); List listr = new ArrayList(); List listc = new ArrayList(); List 
    
      rowlist = new ArrayList 
     
       (); List 
      
        celllist = new ArrayList 
       
         (); for( String js:result){ String jj= getTextCellRow(filepath,js); if(StringUtils.isNotEmpty(jj)){ int row=Integer.parseInt(jj.split(",")[0]); int cell=Integer.parseInt(jj.split(",")[1]); rowlist.add(row); celllist.add(cell); } } if(rowlist.size()>0){ listr=getRowQJ(rowlist,celllist); if(listr.size()>0){ rr.put(value, listr); } listc=getCellQJ(rowlist,celllist); if(listc.size()>0){ cr.put(value, listc); } } } //2处理合并 //1.合并上下相同的单元格 Iterator ite = rr.keySet().iterator(); while (ite.hasNext()){ Object value = ite.next(); List 
        
          result = (List) rr.get(value); hbRow(filepath,result,filepath); } //2.合并左右相同的单元格 Iterator itc = cr.keySet().iterator(); while (itc.hasNext()){ Object value = itc.next(); List 
         
           result = (List) cr.get(value); hbCell(filepath,result,filepath); } } //合并上下单元 public static void hbRow( String filepath, List 
          
            result,String savefile) throws Exception{ int minrow=Integer.parseInt(result.get(0).split(",")[0]); int cell=Integer.parseInt(result.get(0).split(",")[1]); int maxrow=Integer.parseInt(result.get(1).split(",")[0]); Document doc = new Document(filepath); Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); //第1个表格 // We want to merge the range of cells found in between these two cells. Cell cellStartRange = table.getRows().get(minrow).getCells().get(cell); //第2行第1列 Cell cellEndRange = table.getRows().get(maxrow).getCells().get(cell); //第3行第1列 // Merge all the cells between the two specified cells into one. mergeCells(cellStartRange, cellEndRange); doc.save(savefile); } //获取区间row大小必须列一致 public static List 
           
             getRowQJ(List 
            
              rowList,List 
             
               cellList) throws Exception{ List 
              
                list = new ArrayList 
               
                 (); int maxrow= Collections.max(rowList); int maxcell= Collections.max(cellList); int minrow= Collections.min(rowList); int mincell= Collections.min(cellList); if(maxcell==mincell){ int size=rowList.size(); int fw=maxrow-minrow+1; //连续的单元格 if(size==fw){ //只有第一列 if(mincell==0){ list.add(minrow+","+mincell); list.add(maxrow+","+mincell); } } } return list; } //获取相同value的key public static Map getcf(Map 
                
                  map){ Map rs = new HashMap(); Map values = new HashMap(); List list = new ArrayList(); Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = map.get(key); if (map.containsValue(value)) { if (values.containsKey(value)) { list = (List) values.get(value); } else { list = new ArrayList(); } list.add(key); values.put(value, list); } } iterator = values.keySet().iterator(); while (iterator.hasNext()) { Object value = iterator.next(); List result = (List) values.get(value); if (result.size() > 1) { rs.put(value, result); // System.out.println("value :" + value + " -> keys:" + result.toString()); } } return rs; } //获取内容的行列 public static String getTextCellRow(String FilePath,String data) throws Exception{ Document doc; String tt=""; String s = " 
                  
                   
                    
                    
                      Aspose.Total for Java 
                     
                    
                      Aspose.Words for Java 
                     
                    
                   
                     Enterprise 
                    
                    
                    
                    
                    
                   
                     8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7 
                     
                  
                    sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= 
                   
                 "; ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes()); License license = new License(); license.setLicense(is); doc = new Document(FilePath); //替换表格 //获取第一个表格 Table table = (Table)doc.getChild(NodeType.TABLE, 0, true); if(table!=null){ DocumentBuilder builder = new DocumentBuilder(doc); RowCollection rows = table.getRows(); // System.out.println("rows==="+rows.getCount()); for (int i = 0; i < rows.getCount(); i++) { Row row = rows.get(i); for(int j=0; j 
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
  

3测试方法

合并map中value值相同的相邻单元格

4.文件的表格样式

aspose合并单元格

5.合并之后

 aspose合并单元格

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217517.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午9:24
下一篇 2026年3月18日 上午9:24


相关推荐

  • centOs使用yum安装nginx「建议收藏」

    centOs使用yum安装nginx「建议收藏」1.安装nginxyuminstallnginx2.找到配置文件find/-namenginx.conf3.添加配置vinginx.conf4.基本命令nginx#启动nginx-sstop#停止nginxnginx-sreload#重启nginxnginx-c/etc/nginx/nginx.conf#使用指定配置文件启动nginxnginx-t#检测配置文件是否有错误nginx-v#查看版本信息..

    2022年6月4日
    32
  • 初级程序员面试题总结(一):

    本人将这几天面试的题目总结一些,如果出现错误请指正,谢谢。1,谈一谈spring。答:spring是为java程序开发提供的综合性的基础java开发平台,它提供了从表现层SpringMVC到业务层Spring再到持久层springData的一套完整的解决方案。spring的核心有两大块,第一块是AOP,面向切面编程,它将程序与业务分离,集中来解决一些公共问题。第二块是IOC,控制反转,由容…

    2022年4月8日
    59
  • python之 python 起源、语言特点[通俗易懂]

    python之 python 起源、语言特点[通俗易懂]一、1.1什么是PythonPython是一门优雅而健壮的编程语言,它继承了传统编译语言的强大性和通用性,同时也借鉴了简单脚本和解释语言的易用性。它可以帮你完成工作,而且一段时间以后,你还能看

    2022年7月5日
    24
  • Linux CentOS 7安装Oracle11g超完美教程[通俗易懂]

    Linux CentOS 7安装Oracle11g超完美教程[通俗易懂]Oracle部署文章目录Oracle部署1基本环境介绍2检测是否安装了Oracle3卸载Oracle3.1重新做一次虚拟机3.2卸载Oracle4安装准备4.1建立oracle用户和用户组4.2为Oracle的安装创建相关目录4.3优化OS内核参数4.4限制oracle用户的shell权限4.5为Oracle用户添加Oracle环境变量4.6配置hostname(本机IP映射)4.7安装VNC&Oracle相关依赖4.7.1配置yum源4.7.2安装依赖4.7.3检

    2022年7月15日
    19
  • C#数组–(一维数组,二维数组的声明,使用及遍历)

    C#数组–(一维数组,二维数组的声明,使用及遍历)数组:是具有相同数据类型的一组数据的集合。数组的每一个的变量称为数组的元素,数组能够容纳元素的数称为数组的长度。一维数组:以线性方式存储固定数目的数组元素,它只需要1个索引值即可标识任意1个数组元素

    2022年7月2日
    25
  • 创建阿里云docker镜像仓库,并把java项目打包为镜像上传到阿里云镜像仓库

    创建阿里云docker镜像仓库,并把java项目打包为镜像上传到阿里云镜像仓库创建阿里云 docker 镜像仓库 并把 java 项目打包为镜像上传到阿里云镜像仓库

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号