/// <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); } }
标签:val,int,单元格,tp,cell,Add,dataIndex,new,aspose From: https://www.cnblogs.com/gxivwshjj/p/18442297