aspose.cell 合并内容一样的单元格
/// <summary> /// 数据合并 /// </summary> /// <param name="mySheet"></param> /// <param name="columnIndex">合并的列下标</param> /// <param name="dataIndex">从哪一行开始</param> /// <param name="col"></param> private static void MergeCell_Second(Worksheet mySheet, int columnIndex,int dataIndex, string col) { Dictionary<string, Tuple<int, int, int, int>> tp = new Dictionary<string, Tuple<int, int, int, int>>(); string val = val = mySheet.Cells[dataIndex, columnIndex].Value.ToString().Trim();//数据行 int rowNum = mySheet.Cells.MaxDataRow;//最大行数 int mergeRow = 0; int startIndex = dataIndex; List<string> k = new List<string>(); bool isall = true; k.Add(val); for (int i= dataIndex; i< rowNum; i++) { val = mySheet.Cells[i, columnIndex].Value.ToString().Trim(); if (k.Contains(val)) { mergeRow++; k.Add(val); } else { isall = false; tp.Add(k[0], new Tuple<int, int, int, int>(startIndex, 0, mergeRow, 1)); k.Clear(); k.Add(val); mergeRow = 1; startIndex = i; } } if (isall) { tp.Add(k[0], new Tuple<int, int, int, int>(dataIndex, 0, rowNum, 1)); } if (!tp.ContainsKey(k[0])) { tp.Add(k[0], new Tuple<int, int, int, int>(startIndex, 0, mergeRow, 1)); } foreach(var item in tp) { mySheet.Cells.Merge(item.Value.Item1, columnIndex, item.Value.Item3, 1); } }