private void RefreshLineTplRanges(TplBlock block, int colCount) { foreach (var line in block.TplLineList) { line.TplRange = RangeHelper.GetRange(line.TplRange.Worksheet, line.TplRange.Column, line.TplRange.Row, line.TplCellCount + InsertCount, colCount); line.TplCellCount += colCount; } }
/// <summary> /// 单元格有公式时更新行数据. /// </summary> /// <param name="holder"></param> /// <param name="currentRowIndex"></param> /// <param name="table"></param> /// <param name="valueRowIndex"></param> public void UpdateRowData(GroupDataHolder holder, int currentRowIndex, DataTable table, int valueRowIndex) { var lastColIndex = CellList[0].LastColIndex; foreach (var cell in CellList) { if (cell.Formula != null) { var currentCellRange = RangeHelper.GetCell(TplRange.Worksheet, lastColIndex, currentRowIndex); cell.WriteCell(SheetTemplate, holder, currentCellRange, table, valueRowIndex); } lastColIndex++; } }
/// <summary> /// 获取单元格显示值 优先级:公式->水平分组列添加值->数据源值 /// </summary> /// <param name="holder"></param> /// <param name="table"></param> /// <param name="rowIndex"></param> /// <returns></returns> private object GetValue(GroupDataHolder holder, DataTable table, int rowIndex) { if (Formula != null) { return(Formula.GetValue(holder, table, rowIndex)); } if (HgOption != InsertOption.Never) { return(TplTextContent); //插入动态列时更新赋值 } var tableValue = RangeHelper.GetTableValue(table, rowIndex, TplValueColName); return(tableValue ?? TplTextContent); }
private int IsNeedNewCol(DataTable table, int valueIndex) { var objArray = new object[GroupColList.Count]; for (var i = 0; i < GroupColList.Count; i++) { var colName = GroupColList[i]; objArray[i] = RangeHelper.GetTableValue(table, valueIndex, colName); } if (LastValueMap.Any(objArray2 => CompareArray(objArray, objArray2))) { return(-1); } LastValueMap.Add(objArray); return(0); }
private void MergeHGroupCells() { for (var i = 0; i < TplLineList.Count; i++) { var line = TplLineList[i]; if (line.ContainsHGroup) { foreach (var startRow in line.InsertedRowList) { object objA = null; var startColIndex = DynamicColumn.StartColIndex; for (var k = 0; k < DynamicColumn.InsertCount; k++) { var currentCellRange = RangeHelper.GetCell(TplRange.Worksheet, startColIndex, startRow); var objB = currentCellRange.Value2; if ((objA != null) && Equals(objA, objB)) { if ((i == (TplLineList.Count - 1)) || TplLineList[i + 1].ContainsHGroup) { RangeHelper.MergeRanges(currentCellRange, MergeOption.Left); } else if ((k % DynamicColumn.GroupColumnCount) > 0) { RangeHelper.MergeRanges(currentCellRange, MergeOption.Left); } } objA = objB; startColIndex++; } var num6 = DynamicColumn.StartCellIndex + DynamicColumn.InsertCount; for (var m = num6; m < line.CellList.Count; m++) { var cell = line.CellList[m]; if (cell.MergeOption == MergeOption.Left) { RangeHelper.MergeRanges( RangeHelper.GetCell(TplRange.Worksheet, startColIndex, startRow), MergeOption.Left); } startColIndex++; } } } } }
/// <summary> /// 复制填充行(非分组更新单元格值) /// </summary> /// <param name="holder"></param> /// <param name="currentRowIndex"></param> /// <param name="table"></param> /// <param name="valueRowIndex"></param> /// <returns></returns> public bool FillLine(GroupDataHolder holder, int currentRowIndex, DataTable table, int valueRowIndex) { var startCellIndex = IsNeedNewLine(holder, currentRowIndex, table, valueRowIndex); if (startCellIndex < 0) { return(false); } RangeHelper.InsertCopyRange(TplRange.Worksheet, TplRange, StartColumnIndex, currentRowIndex, CellList.Count, 1, InsertRangeDirection.Down); InsertedRowList.Add(currentRowIndex); UpdateLine(holder, currentRowIndex, startCellIndex, table, valueRowIndex, MergeOption.Left, false); UpdateLine(holder, currentRowIndex, startCellIndex, table, valueRowIndex, MergeOption.Up, true); return(true); }
/// <summary> /// 写Sheet表单元格 /// </summary> /// <param name="tpl"></param> /// <param name="holder"></param> /// <param name="currentCellRange"></param> /// <param name="table"></param> /// <param name="valueRowIndex"></param> public void WriteCell(ReportSheetTemplate tpl, GroupDataHolder holder, CellRange currentCellRange, DataTable table, int valueRowIndex) { if (UseR1C1Formula) { currentCellRange.FormulaR1C1 = "=" + TplTextContent; } else { var cellValue = GetValue(holder, table, valueRowIndex); if ((cellValue == null || cellValue == string.Empty) || (cellValue == DBNull.Value)) { cellValue = TplDefaultContent; } RangeHelper.UpdateCellValue(tpl, currentCellRange, cellValue, TplFormat); if (GroupAlign == GroupAlign.Vertical) { //纵向分组才更新最后分组值,水平分组在判断是否新增单元格时更新(before情况) LastGroupedValue = GetGroupValue(holder, table, valueRowIndex); } } }
private TplCloumn FindDynamicColumn() { var column = new TplCloumn { GroupColumnCount = 0 }; //遍历模版行,以包含包含横向分组的第一行为基准,查找动态列数量 foreach (var line in TplLineList) { for (var k = 0; k < line.CellList.Count; k++) { var cell = line.CellList[k]; if (cell.GroupAlign == GroupAlign.Horizontal) { if (column.GroupColumnCount <= 0) { column.StartCellIndex = k; column.StartColIndex = cell.LastColIndex; } column.GroupColumnCount++; } } if (column.GroupColumnCount > 0) { break; } } if (column.GroupColumnCount <= 0) { return(null); } column.TplLastColumnIndex = (TplColumCount - column.StartCellIndex) - column.GroupColumnCount; if (column.TplLastColumnIndex <= 0) { column.TplLastColumnIndex = 1; } column.TplRange = RangeHelper.GetRange(TplRange.Worksheet, column.StartColIndex, StartParseRowIndex, column.GroupColumnCount, TplLineList.Count); //添加横向分组单元格(包含公式) foreach (var line in TplLineList) { foreach (var cell in line.CellList) { if (cell.GroupAlign == GroupAlign.Horizontal) { var item = cell.Copy(); column.CellList.Add(item); if (!column.GroupColList.Contains(cell.TplGroupColumnName)) { column.GroupColList.Add(cell.TplGroupColumnName); } } } } return(column); }
public static ReportSheetTemplate ParseSheetTemplate(Worksheet sheet) { var template = new ReportSheetTemplate { Sheet = sheet }; var sheetIndex = sheet.Index + 1; for (var i = 1; i < TemplateFlags.IndexTemplateEndRow; i++) { var str = (string)RangeHelper.GetRange(sheet, 1, i, 1, 1).Value2; if (!string.IsNullOrEmpty(str)) { if (str.Equals(TemplateFlags.EmptyFields, StringComparison.CurrentCultureIgnoreCase)) { var emptyFieldStr = RangeHelper.GetRange(sheet, 2, i, 1, 1).Value2 as string; if (!string.IsNullOrEmpty(emptyFieldStr)) { template.EmptyFieldsDict = ParseKeyValuePair(emptyFieldStr); } } else { var dict = ParseKeyValuePair(str); template.AutoFit = dict.ContainsKey(TemplateFlags.BlockAutoFit) && dict[TemplateFlags.BlockAutoFit] == "true"; var namePrefix = string.Concat("S", sheetIndex, "."); //Todo:JoinAt 动态列合并 int joinat; var block = new TplBlock { StartParseColumnIndex = 2, StartParseRowIndex = i, ColumnsCount = int.Parse(dict[TemplateFlags.BlockColumnCount]), TplColumCount = int.Parse(dict[TemplateFlags.BlockColumnCount]), TplRowCount = int.Parse(dict[TemplateFlags.BlockRowCount]), Name = dict.ContainsKey(TemplateFlags.BlockName) ? dict[TemplateFlags.BlockName] : string.Concat(namePrefix + "block", template.BlockList.Count + 1), DataTableIndex = dict.ContainsKey(TemplateFlags.BlockTable) ? int.Parse(dict[TemplateFlags.BlockTable]) : -1, TplColumnTableIndex = dict.ContainsKey(TemplateFlags.BlockColumnTalbe) ? int.Parse(dict[TemplateFlags.BlockColumnTalbe]) : -1, CopyOnly = dict.ContainsKey(TemplateFlags.BlockCopyOnly) && dict[TemplateFlags.BlockCopyOnly] == "true", UpdateAllRow = dict.ContainsKey(TemplateFlags.BlockUpdateAllRow) && dict[TemplateFlags.BlockUpdateAllRow] == "true", Joinat = dict.ContainsKey(TemplateFlags.BlockJoinAt) && int.TryParse(dict[TemplateFlags.BlockJoinAt], out joinat) ? joinat : -1 }; block.TplRange = RangeHelper.GetRange(sheet, block.StartParseColumnIndex, block.StartParseRowIndex, block.ColumnsCount, block.TplRowCount); if (block.CopyOnly) { template.BlockList.Add(block); } else { for (var j = 0; j < block.TplRowCount; j++) { var startColumn = TemplateFlags.IndexLineContendStartColumn; var line = ParseLine(sheet, block, startColumn, j + block.StartParseRowIndex); line.SheetTemplate = template; line.StartColumnIndex = startColumn; line.InsertOption = GetLineInsertOption( RangeHelper.GetCell(sheet, TemplateFlags.IndexLineInsertOptionColumn, j + block.StartParseRowIndex).Value2 as string); line.TplCellCount = block.ColumnsCount; block.TplLineList.Add(line); } block.InitDynamicColumn(template); template.BlockList.Add(block); } } } } return(template); }
public bool DoMerge(CellRange currentCellRange) { return(currentCellRange != null && RangeHelper.MergeRanges(currentCellRange, MergeOption)); }
public void InsertOneColumn(TplBlock block, int colIndex, GroupDataHolder holder, DataTable columnTalbe, int valueIndex, bool hasData) { if (hasData) { var range = RangeHelper.GetRange(TplRange.Worksheet, StartColIndex + colIndex, block.StartParseRowIndex, 1, block.RowsCount); RangeHelper.InsertCopyRange(TplRange.Worksheet, range, (StartColIndex + GroupColumnCount) + InsertCount, block.StartParseRowIndex, 1, block.RowsCount, InsertRangeDirection.Right, TplLastColumnIndex); } var orign = RangeHelper.GetRange(TplRange.Worksheet, StartColIndex + colIndex, block.TplRange.Row, 1, block.TplRowCount); RangeHelper.InsertCopyRange(TplRange.Worksheet, orign, (StartColIndex + GroupColumnCount) + InsertCount, orign.Row, 1, block.TplRowCount, InsertRangeDirection.Right, TplLastColumnIndex); RefreshLineTplRanges(block, 1); block.TplColumCount++; block.ColumnsCount++; for (var i = 0; i < block.TplLineList.Count; i++) { var line = block.TplLineList[i]; var num2 = StartCellIndex + colIndex; var tplCell = line.CellList[num2].Copy(); tplCell.LastColIndex++; line.CellList.Insert((StartCellIndex + GroupColumnCount) + InsertCount, tplCell); if (tplCell.Formula != null) { foreach (var groupKey in tplCell.Formula.KeyList) { if (groupKey.ReusedKey == null) { groupKey.ReusedKey = SearchKey.FindReusedKey(groupKey.ValueColName); } for (var key3 = groupKey.SearchKey; key3 != null; key3 = key3.NextKey) { if (IsGroupedColumn(key3.KeyName)) { key3.KeyValue = RangeHelper.GetTableValue(columnTalbe, valueIndex, key3.KeyName); key3.IsFixedValue = true; } } block.GroupKeyList.Add(groupKey.Copy()); if (groupKey.SearchKey != null) { groupKey.SearchKey.FillKey(columnTalbe, valueIndex); } block.Holder.AddValue(block.CountedMap, groupKey, columnTalbe, valueIndex); } } tplCell.GroupAlign = GroupAlign.None; Console.WriteLine(string.Concat("Inserted hg [", i.ToString().PadLeft(3), "][", num2.ToString().PadLeft(3), "] = ", tplCell.Formula)); if (i < block.RowsCount) { var currentCellRange = RangeHelper.GetCell(TplRange.Worksheet, (StartColIndex + GroupColumnCount) + InsertCount, block.StartParseRowIndex + i); tplCell.WriteCell(Tpl, holder, currentCellRange, columnTalbe, valueIndex); } } InsertCount++; }
public void InsertColumn(TplBlock block, GroupDataHolder holder, DataTable dataTable, int valueIndex, bool hasData) { if (InsertCount > 0) { if (hasData) { var range = RangeHelper.GetRange(TplRange.Worksheet, (StartColIndex + InsertCount) - GroupColumnCount, block.StartParseRowIndex, GroupColumnCount, block.RowsCount); //Console.WriteLine(string.Format( // "orign1:[StartColIndex-{4},InsertCount-{0},groupColumnCount-{1},StartRowIndex-{2},RowCount{3},tplLastColumnIndex{5}]", // InsertCount, groupColumnCount, block.StartRowIndex, block.RowCount, StartCellIndex, tplLastColumnIndex)); RangeHelper.InsertCopyRange(TplRange.Worksheet, range, StartColIndex + InsertCount, block.StartParseRowIndex, GroupColumnCount, block.RowsCount, InsertRangeDirection.Right, TplLastColumnIndex); } var orign = RangeHelper.GetRange(TplRange.Worksheet, (StartColIndex + InsertCount) - GroupColumnCount, block.TplRange.Row, GroupColumnCount, block.TplRowCount); RangeHelper.InsertCopyRange(TplRange.Worksheet, orign, StartColIndex + InsertCount, orign.Row, GroupColumnCount, block.TplRowCount, InsertRangeDirection.Right, TplLastColumnIndex); RefreshLineTplRanges(block, GroupColumnCount); block.TplColumCount += GroupColumnCount; block.ColumnsCount += GroupColumnCount; } for (var i = 0; i < block.TplLineList.Count; i++) { var line = block.TplLineList[i]; for (var j = 0; j < GroupColumnCount; j++) { var num3 = (StartCellIndex + ((InsertCount > 0) ? (InsertCount - GroupColumnCount) : 0)) + j; var tplCell = line.CellList[num3]; if (InsertCount > 0) { tplCell = tplCell.Copy(); tplCell.LastColIndex += GroupColumnCount; line.CellList.Insert(num3 + GroupColumnCount, tplCell); } if (tplCell.Formula != null) { foreach (var groupKey in tplCell.Formula.KeyList) { for (var searchKey = groupKey.SearchKey; searchKey != null; searchKey = searchKey.NextKey) { if (IsGroupedColumn(searchKey.KeyName)) { searchKey.KeyValue = RangeHelper.GetTableValue(dataTable, valueIndex, searchKey.KeyName); searchKey.IsFixedValue = true; } } block.GroupKeyList.Add(groupKey.Copy()); if (groupKey.SearchKey != null) { groupKey.SearchKey.FillKey(dataTable, valueIndex); } block.Holder.AddValue(block.CountedMap, groupKey, dataTable, valueIndex); } } else if (tplCell.HgOption != InsertOption.Never) { tplCell.TplTextContent = Convert.ToString( RangeHelper.GetTableValue(dataTable, valueIndex, tplCell.TplValueColName)); } tplCell.GroupAlign = GroupAlign.None; Console.WriteLine(string.Concat("Inserted hg [", i.ToString().PadLeft(3), "][", num3.ToString().PadLeft(3), "] = ", tplCell.Formula)); if (i < block.RowsCount) { var currentCellRange = RangeHelper.GetCell(TplRange.Worksheet, (StartColIndex + InsertCount) + j, block.StartParseRowIndex + i); tplCell.WriteCell(Tpl, holder, currentCellRange, dataTable, valueIndex); } } } InsertCount += GroupColumnCount; }
public void FillTemplate() { foreach (var block in BlockList) { if (block.CopyOnly) { #region Copy 传入参数部分 var rangeCells = RangeHelper.GetRangeCells( RangeHelper.InsertCopyRange(Sheet, block.TplRange, block.StartParseColumnIndex, TemplateFlags.IndexTemplateEndRow + AlreadyWriteRows, block.TplColumCount, block.TplRowCount, InsertRangeDirection.Down)); while (rangeCells.MoveNext()) { var currentCell = (CellRange)rangeCells.Current; if (!currentCell.HasMerged) { var cellValue = currentCell.Value2 as string; if (((cellValue != null) && cellValue.StartsWith("#")) && ((cellValue.Length > 1) && (ParamMap != null))) { var strArray = cellValue.Substring(1) .Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); object paramValue; ParamMap.TryGetValue(strArray[0], out paramValue); var format = ""; if (strArray.Length > 1) { format = strArray[1].ToLower(); } RangeHelper.UpdateCellValue(this, currentCell, paramValue, format); } //模版单元格为合并单元格时进行合并 var templateCell = RangeHelper.GetRange(Sheet, currentCell.Column, ((currentCell.Row - TemplateFlags.IndexTemplateEndRow) - AlreadyWriteRows) + block.StartParseRowIndex, 1, 1); if (templateCell.HasMerged) { var startColumn = templateCell.MergeArea.Column; var startRow = ((templateCell.MergeArea.Row + TemplateFlags.IndexTemplateEndRow) + AlreadyWriteRows) - block.StartParseRowIndex; var mergeColumnCount = templateCell.MergeArea.ColumnCount; var mergeRowCount = templateCell.MergeArea.RowCount; var needMergeArea = RangeHelper.GetRange(Sheet, startColumn, startRow, mergeColumnCount, mergeRowCount); if (!needMergeArea.HasMerged) { needMergeArea.Merge(); } } } } AlreadyWriteRows += block.TplRowCount; #endregion Copy 传入参数部分 } else { block.StartParseRowIndex = TemplateFlags.IndexTemplateEndRow + AlreadyWriteRows; if (block.TplColumnTableIndex >= 0 && DataSource.Tables.Count > block.TplColumnTableIndex) { //动态填充模版列 block.CreateDynamicColumn(DataSource.Tables[block.TplColumnTableIndex]); } if (block.DataTableIndex < 0 || DataSource.Tables.Count <= block.DataTableIndex) { throw new ArgumentException(string.Format( "DataTable [{0}] of Block [{1}] not found in DataSet!", block.DataTableIndex, block.Name)); } if ((DataSource.Tables[block.DataTableIndex].Rows.Count <= 0)) { var emptyTable = new DataTable(); #region 数据源无数据时填充EmptyTable var stringType = Type.GetType("System.String"); if (stringType != null) { if (EmptyFieldsDict != null && EmptyFieldsDict.Count > 0) { foreach (var key in EmptyFieldsDict.Keys) { emptyTable.Columns.Add(new DataColumn(key, stringType)); } var row = emptyTable.NewRow(); foreach (DataColumn column in emptyTable.Columns) { var field = EmptyFieldsDict[column.ColumnName]; if (field.StartsWith("#") && ParamMap != null) { object paramValue; if (ParamMap.TryGetValue(field.Substring(1), out paramValue)) { row[column.ColumnName] = paramValue.ToString(); } else { row[column.ColumnName] = field; } } else { row[column.ColumnName] = field; } } emptyTable.Rows.Add(row); } else { emptyTable.Columns.Add(new DataColumn("column1", stringType)); emptyTable.Columns.Add(new DataColumn("column2", stringType)); emptyTable.Rows.Add("0", "0"); } } #endregion 数据源无数据时填充EmptyTable AlreadyWriteRows += block.FillBlock(emptyTable); } else { AlreadyWriteRows += block.FillBlock(DataSource.Tables[block.DataTableIndex]); } } } JoinTable(); }
private void JoinTable() { if (BlockList.Count >= 2) { TplBlock block = BlockList[1]; //要合并的第一个Block int startRowIndex = block.StartParseRowIndex; int startColumn = block.StartParseColumnIndex + block.ColumnsCount; int num3 = 0; for (int i = 2; i < BlockList.Count; i++) //以第一个block为基准,合并其他指定joinat的block { TplBlock block2 = BlockList[i]; if ((block2.Joinat >= 0) && (block2.RowsCount > 0)) { //获取joinat之后的区域 CellRange range = RangeHelper.GetRange(Sheet, block2.StartParseColumnIndex + block2.Joinat + 1, block2.StartParseRowIndex - num3, block2.ColumnsCount, block2.RowsCount); CellRange destRange = RangeHelper.GetRange(Sheet, startColumn + 1, block.StartParseRowIndex, block2.ColumnsCount, block2.RowsCount); range.Copy(destRange);//block2区域复制到block1右边 Sheet.DeleteRow(range.Row, range.RowCount); num3 += block2.RowsCount; if ((block2.DynamicColumn != null) && (block2.DynamicColumn.StartCellIndex == block2.Joinat)) { foreach (var line in block2.TplLineList) { if (line.ContainsHGroup) //block2中包含横向分组的行hg { bool flag = false; foreach (var rowIndex in line.InsertedRowList) { int startRow = (rowIndex - block2.StartParseRowIndex) + startRowIndex; //动态添加移动的起始行 CellRange range2 = RangeHelper.GetRange(Sheet, startColumn, startRow, 1, 1); //相同行区域的block1最后单元格? if (range2.MergeArea != null) { range2 = RangeHelper.GetRange(Sheet, range2.MergeArea.Column, //?range2合并区域的起始单元格 range2.MergeArea.Row, 1, 1); } CellRange range3 = RangeHelper.GetRange(Sheet, startColumn + 1, startRow, 1, 1); //block2的第一个单元格 if (range3.MergeArea != null) { range3 = RangeHelper.GetRange(Sheet, range3.MergeArea.Column, //第一个单元格的起始区域 range3.MergeArea.Row, 1, 1); } if (range2.Text.Equals(range3.Text)) { RangeHelper.GetRange(Sheet, range2.Column, range2.Row, (range3.Column + range3.ColumnCount) - range2.Column, Math.Min(range3.RowCount, range2.RowCount)).Merge(true); flag = true; } } if (!flag) { break; } } } } startColumn += block2.ColumnsCount - block2.Joinat; //合并后起始列更新 } } } }