如何使用NPOI的XWPFTable實現合並行的功能


NPOI word 中的單元格合並。網上有不少教程將單元格合並的,但是基本都是在創建的單元的時候就開始合並了。

現在我們來看下,如何在創建好的表格上再做合並動作。

NPOI 的XWPFTable的row提供了MergeCells這個功能,該功能可以實現單行的已存在的單元格的合並,和set gridspan值不一樣的是,它不會創建出新的單元格。

但是XWPFTable沒有合並多行的方法,這時候我們需要借助CT_Tc 也就是NPOI中表格單元格的另一種表示形式,這種形式可以讓你設置單元格的屬性(通過將docx解壓開,在docx.xml中可以看到,這里不詳說了)。

此時,我們給單元格加上Vmerge的屬性就可以了。第一個單元格是restart,后面的是continue。

通過先合並多列,再合並行,我們就能達到合並一個區域的效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public  static  XWPFTableCell MYMergeCells(XWPFTable table,  int  fromCol,  int  toCol,  int  fromRow,  int  toRow)
         {
             for  ( int  rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
             {
                 if  (fromCol < toCol)
                 {
                     table.GetRow(rowIndex).MergeCells(fromCol, toCol);
                 }
                 XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(fromCol);
                 CT_Tc cttc = rowcell.GetCTTc();
                 if  (cttc.tcPr ==  null )
                 {
                     cttc.AddNewTcPr();
                 }
                 if  (rowIndex == fromRow)
                 {
                     // The first merged cell is set with RESTART merge value 
                     rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart;
                 }
                 else
                 {
                     // Cells which join (merge) the first one, are set with CONTINUE 
                     rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@ continue ;
                 }
             }
 

這里對原方法進行了一些修改:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <summary>
       /// 跨行合並
       /// </summary>
       /// <param name="table"></param>
       /// <param name="col">合並的列</param>
       /// <param name="fromRow">起始行</param>
       /// <param name="toRow">終止行</param>
 
       public  static  void  mergeCellsVertically(XWPFTable table,  int  col,  int  fromRow,  int  toRow)
       {
 
           for  ( int  rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
           {
               XWPFTableCell cell = table.GetRow(rowIndex).GetCell(col);
 
               CT_Tc cttc = cell.GetCTTc();
               if  (cttc.tcPr ==  null )
               {
                   cttc.AddNewTcPr();
               }
 
 
               //第一個合並單元格用重啟合並值設置
               if  (rowIndex == fromRow)
               {
                   cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.restart;
 
               }
               else
               {
                   //合並第一個單元格的單元被設置為“繼續”
                   cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.@ continue ;
 
               }
 
           }
 
       }
 

 

這樣調用:

粘貼圖片

 

實現效果:

粘貼圖片

 

參考網址: https://blog.csdn.net/weixin_43483847/article/details/87695799


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM