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