博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
excel 合并单元格
阅读量:4616 次
发布时间:2019-06-09

本文共 2397 字,大约阅读时间需要 7 分钟。

最近用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[
1
as 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 + 
1
1];
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/archive/2012/05/11/2496428.html

你可能感兴趣的文章
SpringBoot------ActiveMQ安装
查看>>
详细了解 int? 类型
查看>>
纯js事件注册方法(解决兼容性)
查看>>
Ultimate Facebook Messenger for Business Guide (Feb 2019)
查看>>
博客开通第77天
查看>>
[git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF
查看>>
mysql索引详解
查看>>
Log4j maven依赖配置
查看>>
HDU-4472 Count 递推
查看>>
活用这25种图表效果,你的数据可视化也能变得高级酷炫
查看>>
Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM
查看>>
[New Portal]Windows Azure Virtual Machine (16) 使用Azure PowerShell创建Azure Virtual Machine
查看>>
GMM模型
查看>>
unity3d log管理
查看>>
scp与rsync限速
查看>>
实验2-1-5 将x的平方赋值给y
查看>>
利用spring boot+vue做的一个博客项目
查看>>
595. 大的国家
查看>>
【原创】JQWidgets-TreeGrid 1、快速入门
查看>>
高精度库(支持小数、负数、整数、判断质数、阶乘、孪生质数等)
查看>>