Example #1
0
 public override void Format(SheetFormatterContext context)
 {
     context.ClearRowContent(_templateRowIndex); //清除模板行单元格内容
     if (null == _columnInfoList || _columnInfoList.Count <= 0 || null == _dataSource)
     {
         return;
     }
     foreach (TSource rowSource in _dataSource)
     {
         var row = context.Sheet.GetRow(context.GetCurrentRowIndex(_templateRowIndex));
         foreach (TableColumnInfo <TSource> colInfo in _columnInfoList)
         {
             var cell = row.GetCell(colInfo.ColumnIndex);
             SetCellValue(cell, colInfo.DgSetValue(rowSource));
         }
         context.InsertEmptyRow(_templateRowIndex); //追加空行
     }
     context.RemoveRow(_templateRowIndex);          //删除空行
 }
        /// 格式化操作
        /// <param name="context"></param>
        public override void Format(SheetFormatterContext context)
        {
            context.ClearRowContent(TemplateRowIndex); //清除模板行单元格内容
            if (null == ColumnInfoList || ColumnInfoList.Count <= 0 || null == DataSource)
            {
                return;
            }
            var itemCount = 0;

            foreach (TSource rowSource in DataSource)
            {
                if (itemCount++ > 0)
                {
                    context.InsertEmptyRow(TemplateRowIndex);  //追加空行
                }
                var row = context.Sheet.GetRow(context.GetCurrentRowIndex(TemplateRowIndex));
                foreach (TableColumnInfo <TSource> colInfo in ColumnInfoList)
                {
                    var cell = row.GetCell(colInfo.ColumnIndex);
                    SetCellValue(cell, colInfo.DgSetValue(rowSource));
                }
            }
        }
Example #3
0
        /// 格式化操作
        public override void Format(SheetFormatterContext context)
        {
            context.ClearRowContent(TemplateRowIndex); //清除模板行单元格内容
            if (null == ColumnInfoList || ColumnInfoList.Count <= 0 || null == DataSource)
            {
                return;
            }
            var itemCount = 0;
            var sheet     = context.Sheet;
            int rowIndex  = 0;
            Stack <LevelInfo> levelStack = new Stack <LevelInfo>();


            foreach (TSource rowSource in DataSource)
            {
                if (itemCount++ > 0)
                {
                    //如果不是第一行时执行的操作
                    context.InsertEmptyRow(TemplateRowIndex);  //追加空行
                }

                //当前行索引
                rowIndex = context.GetCurrentRowIndex(TemplateRowIndex);
                //当前行的层级
                int level = GetLevel(rowSource);
                //当前行
                var row = sheet.GetRow(rowIndex);

                if (levelStack.Count > 0)
                {
                    LevelInfo top = levelStack.Peek();
                    while (level < top.Level)
                    {
                        sheet.GroupRow(top.RowIndex, rowIndex - 1);
                        levelStack.Pop();
                        top = levelStack.Peek();
                    }
                    if (level > top.Level)
                    {
                        levelStack.Push(new LevelInfo(rowIndex, level));
                    }
                }
                else
                {
                    levelStack.Push(new LevelInfo(rowIndex, level));
                }

                foreach (TableColumnInfo <TSource> colInfo in ColumnInfoList)
                {
                    var    cell  = row.GetCell(colInfo.ColumnIndex);
                    object value = colInfo.DgSetValue(rowSource);
                    if (colInfo.ColumnIndex == _levelColumnIndex)
                    {
                        //处理层级列,如果是层级列,则增加缩进
                        value = new string(' ', level * 2) + value;
                    }
                    SetCellValue(cell, value);
                }
            }

            while (levelStack.Count > 0)
            {
                LevelInfo top = levelStack.Pop();
                if (0 < top.Level)
                {
                    sheet.GroupRow(top.RowIndex, rowIndex);
                }
            }
        }