/// <summary> /// 创建一个单元格位置对象 /// </summary> /// <param name="startCellocation">开始单元格</param> /// <param name="endCellocation">结束单元格</param> /// <param name="mergeType">分组内容依据</param> /// <param name="custom">自定义分组内容</param> public MergeCell(Cellocation startCellocation, Cellocation endCellocation, MergeType mergeType, string custom) { this.StartCellocation = startCellocation; this.EndCellocation = endCellocation; this.IMergeType = mergeType; this.Custom = custom; }
/// <summary> /// 创建一个单元格位置对象 /// </summary> /// <param name="startCellocation">开始单元格</param> /// <param name="endCellocation">结束单元格</param> /// <param name="mergeType">分组内容依据</param> public MergeCell(Cellocation startCellocation, Cellocation endCellocation, MergeType mergeType) { this.StartCellocation = startCellocation; this.EndCellocation = endCellocation; this.IMergeType = mergeType; }
/// <summary> /// 合并单元格 /// </summary> /// <param name="workbook">工作区域</param> /// <param name="sheet">工作簿</param> private void SetMergeCell(HSSFWorkbook workbook, ISheet sheet) { foreach (MergeCell itemCell in ListMergeCell) { Cellocation StartCellocation = itemCell.StartCellocation; Cellocation EndCellocation = itemCell.EndCellocation; if (HeaderText != null) { StartCellocation.X += 1; EndCellocation.X += 1; } if (EndCellocation.X >= StartCellocation.X && EndCellocation.Y >= StartCellocation.Y) { string GroupName = ""; if (itemCell.IMergeType == MergeType.Custom) { GroupName = itemCell.Custom; } else if (itemCell.IMergeType == MergeType.GroupByText) { GroupName = ","; for (int y1 = StartCellocation.Y; y1 <= EndCellocation.Y; y1++) { for (int x1 = StartCellocation.X; x1 <= EndCellocation.X; x1++) { string StringCellValue = sheet.GetRow(y1).GetCell(x1).StringCellValue; if (!GroupName.Contains("," + StringCellValue + ",")) { GroupName += StringCellValue + ","; } } } if (GroupName.Length != 1) { GroupName = GroupName.Substring(1, GroupName.Length - 1); GroupName = GroupName.Substring(0, GroupName.Length - 1); } } else if (itemCell.IMergeType == MergeType.MergeText) { for (int y1 = StartCellocation.Y; y1 <= EndCellocation.Y; y1++) { for (int x1 = StartCellocation.X; x1 <= EndCellocation.X; x1++) { string StringCellValue = sheet.GetRow(y1).GetCell(x1).StringCellValue; if (!GroupName.Contains("," + StringCellValue + ",")) { GroupName += StringCellValue + ","; } } } if (GroupName.Length != 0) { GroupName = GroupName.Substring(0, GroupName.Length - 1); } } else if (itemCell.IMergeType == MergeType.TakeFirstText) { GroupName = sheet.GetRow(StartCellocation.X).GetCell(StartCellocation.Y).StringCellValue; } CellRangeAddress cellRangeAddress = new CellRangeAddress(StartCellocation.X, EndCellocation.X, StartCellocation.Y, EndCellocation.Y); sheet.AddMergedRegion(cellRangeAddress); ICell newCell = sheet.GetRow(StartCellocation.X).GetCell(StartCellocation.Y); SetCellValue(newCell, "", GroupName, newCell.CellStyle, null); } } }