excel 合并单元格

最近用devexpress做excel export,发现devexpress对cell merge的case支持不是很好,没办法,只要调用excel api自己去做merge了。devexpress的mege就是对某列按顺序对相同的值进行merge。写了一个方法,对导出的excel进行merge,如下:

 1 private void mergeCell2(string fileName, int rowNumber, List<int> columnIndex)
 2         {
 3             Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
 4 
 5             try
 6             {
 7                 _excelApp.DisplayAlerts = false;
 8                 _excelApp.ScreenUpdating = true;
 9 
10 
11                 Workbook workBook = _excelApp.Workbooks.Open(fileName,
12                     XlUpdateLinks.xlUpdateLinksAlways, Type.Missing, Type.Missing, Type.Missing,
13                     Type.Missing, Type.Missing, Type.Missing, Type.Missing,
14                     Type.Missing, Type.Missing, Type.Missing, Type.Missing,
15                     Type.Missing, Type.Missing);
16 
17                 Worksheet sheet1 = workBook.Worksheets[1as Worksheet;
18 
19                 for (int loopClmIndx = 0; loopClmIndx < columnIndex.Count; loopClmIndx++)
20                 {
21                     char columnName = Convert.ToChar(Convert.ToInt32('A') + columnIndex[loopClmIndx]);
22 
23                     Range c1 = sheet1.get_Range(string.Format("{0}{1}", columnName, 2),
24                         string.Format("{0}{1}", columnName, rowNumber + 1));
25 
26                     object[,] vv = c1.Cells.Value2 as object[,];
27                     if (vv != null)
28                     {
29                         int mergeStartIndx = -1;
30 
31                         for (int rowIndx = 1; rowIndx <= vv.Length;rowIndx++)
32                         {
33                             mergeStartIndx = rowIndx;
34                             object tempValue = vv[rowIndx, 1];
35                             string strTempValue = tempValue == null ? string.Empty : tempValue.ToString();
36 
37                             while (rowIndx < vv.Length)
38                             {
39                                 object tempNextValue = vv[rowIndx + 11];
40                                 string strTempNextValue = tempNextValue == null ? string.Empty : tempNextValue.ToString();
41 
42                                 if (strTempNextValue.Equals(strTempValue))
43                                 {
44                                     rowIndx++;
45                                 }
46                                 else
47                                 {
48                                     break;
49                                 }
50                             }
51 
52                             if (mergeStartIndx != rowIndx)
53                             {
54                                 Range rg = sheet1.get_Range(string.Format("{0}{1}", columnName, mergeStartIndx+1),
55                                      string.Format("{0}{1}", columnName, rowIndx+1));
56 
57                                 if (rg != null)
58                                 {
59                                     rg.Merge(false);
60                                 }
61 
62                                 Marshal.ReleaseComObject(rg);
63                             }
64                         }                       
65                     }
66                 }
67 
68                 workBook.Save();
69                 workBook.Close(true, Type.Missing, Type.Missing);
70 
71                 Marshal.ReleaseComObject(sheet1);
72                 Marshal.ReleaseComObject(workBook);
73             }
74             finally
75             {
76                 if (_excelApp != null)
77                 {
78                     _excelApp.Quit();
79                     _excelApp = null;
80                     GC.SuppressFinalize(this);
81                 }
82             }
83         }

 注意,如果excel的cell没有值,没有值的相邻的格子也必须合并。

原文地址:https://www.cnblogs.com/fgynew/p/2496428.html