Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
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++;
        }
Ejemplo n.º 4
0
        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();
        }