Example #1
0
 public int CreateDynamicColumn(DataTable table)
 {
     if (DynamicColumn == null)
     {
         return(0);
     }
     ColumnsCount -= DynamicColumn.GroupColumnCount;
     if ((table == null) || (table.Rows.Count <= 0))
     {
         return(0);
     }
     for (var i = 0; i < table.Rows.Count; i++)
     {
         DynamicColumn.CheckEachColumn(this, Holder, 0, table, i);
     }
     IsDynamicCloumnsCreated = true;
     RangeHelper.GetRange(TplRange.Worksheet, DynamicColumn.StartColIndex + DynamicColumn.GroupColumnCount,
                          TplRange.Row,
                          ((TplColumCount - DynamicColumn.StartCellIndex) - DynamicColumn.GroupColumnCount) - 1, TplRowCount)
     .Copy(
         RangeHelper.GetRange(TplRange.Worksheet, DynamicColumn.StartColIndex, TplRange.Row,
                              ((TplColumCount - DynamicColumn.StartCellIndex) - DynamicColumn.GroupColumnCount) - 1,
                              TplRowCount), true, true);
     TplColumCount -= DynamicColumn.GroupColumnCount;
     foreach (var line in TplLineList)
     {
         line.CellList.RemoveRange(DynamicColumn.StartCellIndex, DynamicColumn.GroupColumnCount);
         line.TplCellCount -= DynamicColumn.GroupColumnCount;
         line.TplRange      = RangeHelper.GetRange(TplRange.Worksheet, 3, line.TplRange.Row, TplColumCount, 1);
     }
     return(1);
 }
Example #2
0
        private static TplLine ParseLine(Worksheet sheet, TplBlock block, int startCol, int startRow)
        {
            var colCount = block.ColumnsCount;
            var line     = new TplLine
            {
                TplRange = RangeHelper.GetRange(sheet, startCol, startRow, colCount, 1)
            };

            for (var i = 0; i < colCount; i++)
            {
                var range = RangeHelper.GetCell(sheet, startCol + i, startRow);
                var cell  = new TplCell
                {
                    TplRange     = range,
                    LastColIndex = i + startCol
                };
                var str = range.Value2 as string;
                if (!string.IsNullOrEmpty(str))
                {
                    ParseCell(block, line, cell, str.Trim());
                }
                line.CellList.Add(cell);
            }
            return(line);
        }
Example #3
0
 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;
     }
 }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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++;
        }
Example #7
0
        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; //合并后起始列更新
                    }
                }
            }
        }