Пример #1
0
        /// <summary>
        /// 填充数据
        /// </summary>
        public override void Fill()
        {
            if (Data != null && Data.Rows.Count > 0)
            {
                //导入初始行  0 位第一行标题
                var start = 1;
                // 表格值
                string colsValue = null;
                ICellStyle cellStyle = null;
                foreach (DataRow dr in Data.Rows)
                {
                    IRow _row = SheetThis.CreateRow(start);
                    for (int i = 0; i < this.Columns.Length; i++)
                    {
                        var cell = _row.CreateCell(i);
                        // get cell value
                        colsValue = dr[this.Columns[i].ColName] + "";

                        // format cell value
                        this.Columns[i].FormattedValue(cell, dr, start, ref colsValue);

                        // 获取单元格样式 默认从字典中读取
                        cellStyle = GetCellStyle(this.Columns[i].DataType);
                        // 设置单元格数据
                        this.Columns[i].DataType.SetCellValue(cell, colsValue, cellStyle);
                    }
                    start++;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 创建标题
        /// </summary>
        public virtual void CreateTitle()
        {
            IRow row = SheetThis.CreateRow(0);
            var  i   = 0;

            if (Columns == null || Columns.Length == 0)
            {
                this.InitDefaultColumns();
            }
            if (Columns != null)
            {
                foreach (var col in Columns)
                {
                    ICell cell = row.CreateCell(i++);
                    cell.SetCellValue(col.ColTitleName);
                    cell.CellStyle = GetTitleStyle(WorkBook);
                }
            }
        }
Пример #3
0
 /// <summary>
 /// 创建表
 /// </summary>
 public void CreateSheet()
 {
     if (string.IsNullOrWhiteSpace(this.SheetName))
     {
         this.SheetName = "默认名称";
     }
     CheckAndReBuildSheetName();
     SheetThis = WorkBook.CreateSheet(SheetName);
     SheetThis.DefaultColumnWidth       = 20;
     SheetThis.DefaultRowHeightInPoints = 20;
     if (ShowTitle)
     {
         //调用创建标题的方法
         CreateTitle();
         if (FreezePane)
         {
             //冻结首行
             SheetThis.CreateFreezePane(0, 1);
         }
     }
 }
Пример #4
0
        /// <summary>
        /// 填充数据
        /// </summary>
        public override void Fill()
        {
            if (Data != null && Data.Count > 0)
            {
                // 导入初始行  0 位第一行标题
                var start = 1;
                // 表格值
                string     colsValue = null;
                ICellStyle cellStyle = null;
                var        clazz     = GetClazz();
                foreach (T t in Data)
                {
                    IRow _row = SheetThis.CreateRow(start);
                    for (int i = 0; i < this.Columns.Length; i++)
                    {
                        var cell = _row.CreateCell(i);
                        // get cell value
                        try
                        {
                            colsValue = clazz.GetProperty(this.Columns[i].ColName).GetValue(t) + "";
                        }
                        catch (Exception)
                        {
                            //未找到该属性
                        }

                        // format cell value
                        this.Columns[i].FormattedValue(cell, t, start, ref colsValue);

                        // 获取单元格样式 默认从字典中读取
                        cellStyle = GetCellStyle(this.Columns[i].DataType);
                        // 设置单元格数据
                        this.Columns[i].DataType.SetCellValue(cell, colsValue, cellStyle);
                    }
                    start++;
                }
            }
        }