Пример #1
0
        /// <summary>
        /// 将 exportable 中的数据连接到当前 sheet 中
        /// 调用顺序: NewExcel() -&gt; NewSheet() -&gt; Union() -&gt; WriteTo() -&gt; CloseExcel().
        /// </summary>
        /// <param name="header">导出对象头.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>
        /// this, 以支持链式操作.
        /// </returns>
        public Exporter UnionHeader(IExportHeader header, ExportCallback callback)
        {
            //ExportCallback exportCallback = callback;
            var context = this._context;

            if (context == null)
            {
                throw new Exception("请先调用 NewExcel()");
            }
            var sheet = context.Sheet;

            if (sheet == null)
            {
                throw new Exception("请先调用 NewSheet()");
            }

            var  y = context.StartRowIndex;
            IRow row = null;
            int  columnIndex = 0, x = 0;

            context.RowIndex = 0;
            context.CellType = CellType.HeaderCell;
            while (true)
            {
                context.Reset();
                context.ColumnIndex = columnIndex;

                if (header.NextHeaderCell(columnIndex, context))
                {
                    if (callback != null)
                    {
                        callback(context);
                    }
                    if (context.IsValid)
                    {
                        // 创建单元格
                        if (row == null)
                        {
                            row = sheet.CreateRow(y++);
                        }
                        var cell = row.CreateCell(x++);

                        // 设置单元格
                        SetCell(cell, context);
                    }

                    ++columnIndex;
                }
                else
                {
                    break;
                }
            }

            context.StartRowIndex = y;

            return(this);
        }
Пример #2
0
        ///// <summary>
        ///// 将 exportable 中的数据连接到当前 sheet 中
        ///// 调用顺序: NewExcel() -&gt; NewSheet() -&gt; Union() -&gt; WriteTo() -&gt; CloseExcel().
        ///// </summary>
        ///// <param name="exportable">可导出对象.</param>
        ///// <param name="callback">导出回调.</param>
        ///// <returns>this, 以支持链式操作.</returns>
        //public Exporter Union(Exportable exportable, ExportCallback callback)
        //{
        //    return this
        //        .UnionHeader((IExportHeader)exportable, callback)
        //        .Union((IExportable)exportable, callback);
        //}

        /// <summary>
        /// 将 exportable 中的数据连接到当前 sheet 中
        /// 调用顺序: NewExcel() -&gt; NewSheet() -&gt; Union() -&gt; WriteTo() -&gt; CloseExcel().
        /// </summary>
        /// <param name="exportable">可导出对象.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter Union(IExportable exportable, ExportCallback callback)
        {
            //ExportContext context = this._context;
            //if (context == null) throw new Exception("请先调用 NewExcel()");
            //ISheet sheet = context.Sheet;
            //if (sheet == null) throw new Exception("请先调用 NewSheet()");

            if (exportable.NextRow(0))
            {
                return(this.DoUnion(exportable, callback));
            }

            return(this);
        }
Пример #3
0
        /// <summary>
        /// 分页导出到 Excel.
        /// </summary>
        /// <param name="paged">分页的 Exportable.</param>
        /// <param name="name">导出目标 sheet 的名称.</param>
        /// <param name="zip">导出目标流.</param>
        /// <param name="partNameFormat">分页文件命名格式 {0} 代表编号,必须至少包含一个 {0},否则会出现命名冲突..</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter ExportToExcelByPage(PagedExportable paged, string name, ZipOutputStream zip, string partNameFormat, ExportCallback callback)
        {
            var pageIndex = 0;

            while (paged.NextRow(0))
            {
                var fileName = string.Format(partNameFormat, ++pageIndex);
                var entry    = new ZipEntry(fileName);
                entry.DateTime = DateTime.Now;
                zip.PutNextEntry(entry);

                try
                {
                    this.NewExcel()
                    .NewSheet(name)
                    .UnionHeader(paged, callback)
                    .DoUnion(paged, callback)
                    .WriteTo(zip);
                }
                finally
                {
                    this.CloseExcel();
                }

                paged.NextPage(pageIndex);
            }

            return(this);
        }
Пример #4
0
 /// <summary>
 /// 分页导出到 Excel.
 /// </summary>
 /// <param name="exportable">可导出对象.</param>
 /// <param name="name">导出目标 sheet 的名称.</param>
 /// <param name="pageSize">每页的大小.</param>
 /// <param name="fileName">导出目标流.</param>
 /// <param name="partNameFormat">分页文件命名格式 {0} 代表编号,必须至少包含一个 {0},否则会出现命名冲突..</param>
 /// <param name="callback">导出回调.</param>
 /// <returns>this, 以支持链式操作.</returns>
 public Exporter ExportToExcelByPage(IExportable exportable, string name, int pageSize, string fileName, string partNameFormat, ExportCallback callback)
 {
     using (var zos = new ZipOutputStream(new FileStream(fileName, FileMode.OpenOrCreate)))
     {
         return(this.ExportToExcelByPage(exportable, name, pageSize, zos, partNameFormat, callback));
     }
 }
Пример #5
0
        /// <summary>
        /// 分页导出到 Excel.
        /// </summary>
        /// <param name="exportable">可导出对象.</param>
        /// <param name="name">导出目标 sheet 的名称.</param>
        /// <param name="pageSize">每页的大小.</param>
        /// <param name="zip">导出目标流.</param>
        /// <param name="partNameFormat">分页文件命名格式 {0} 代表编号,必须至少包含一个 {0},否则会出现命名冲突..</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter ExportToExcelByPage(IExportable exportable, string name, int pageSize, ZipOutputStream zip, string partNameFormat, ExportCallback callback)
        {
            var paged = new PagedExportable(exportable as IExportHeader, exportable, pageSize);

            return(ExportToExcelByPage(paged, name, zip, partNameFormat, callback));
        }
Пример #6
0
        /// <summary>
        /// 导出到 Excel.
        /// </summary>
        /// <param name="exportable">可导出对象.</param>
        /// <param name="name">导出目标 sheet 的名称.</param>
        /// <param name="output">输出流.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter ExportToExcel(IExportable exportable, string name, Stream output, ExportCallback callback)
        {
            try
            {
                this.NewExcel().NewSheet(name);

                var header = exportable as IExportHeader;
                if (header != null)
                {
                    this.UnionHeader(header, callback);
                }
                this.Union(exportable, callback)
                .WriteTo(output);
            }
            finally
            {
                this.CloseExcel();
            }

            return(this);
        }
Пример #7
0
 /// <summary>
 /// 导出到 Excel.
 /// </summary>
 /// <param name="exportable">可导出对象.</param>
 /// <param name="name">导出目标 sheet 的名称.</param>
 /// <param name="fileName">输出文件.</param>
 /// <param name="callback">导出回调.</param>
 /// <returns>this, 以支持链式操作.</returns>
 public Exporter ExportToExcel(IExportable exportable, string name, string fileName, ExportCallback callback)
 {
     using (var fs = new FileStream(fileName, FileMode.Create))
     {
         return(this.ExportToExcel(exportable, name, fs, callback));
     }
 }
Пример #8
0
        /// <summary>
        /// 导出到 Excel.
        /// </summary>
        /// <param name="table">是导出的 DataTable.</param>
        /// <param name="name">导出目标 sheet 的名称.</param>
        /// <param name="fileName">输出文件.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter ExportToExcel(DataTable table, string name, string fileName, ExportCallback callback)
        {
            IExportable exportable = new DataTableExportable(table);

            return(ExportToExcel(exportable, name, fileName, callback));
        }
Пример #9
0
        ///// <summary>
        ///// 导出到 Excel.
        ///// </summary>
        ///// <param name="dataGridView">是导出的 DataGridView.</param>
        ///// <param name="name">导出目标 sheet 的名称.</param>
        ///// <param name="fileName">输出文件.</param>
        ///// <param name="callback">导出回调.</param>
        ///// <returns>this, 以支持链式操作.</returns>
        //public Exporter ExportToExcel(DataGridView dataGridView, string name, string fileName, ExportCallback callback)
        //{
        //    IExportable exportable = new DataGridViewExportable(dataGridView);
        //    return ExportToExcel(exportable, name, fileName, callback);
        //}

        /// <summary>
        /// 导出到 Excel.
        /// </summary>
        /// <param name="view">是导出的 DataView.</param>
        /// <param name="name">导出目标 sheet 的名称.</param>
        /// <param name="fileName">输出文件.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter ExportToExcel(DataView view, string name, string fileName, ExportCallback callback)
        {
            IExportable exportable = new DataViewExportable(view);

            return(ExportToExcel(exportable, name, fileName, callback));
        }
Пример #10
0
        /// <summary>
        /// 将 exportable 中的数据连接到当前 sheet 中
        /// 调用顺序: NewExcel() -&gt; NewSheet() -&gt; Union() -&gt; WriteTo() -&gt; CloseExcel().
        /// </summary>
        /// <param name="exportable">可导出对象.</param>
        /// <param name="callback">导出回调.</param>
        /// <returns>this, 以支持链式操作.</returns>
        public Exporter DoUnion(IExportable exportable, ExportCallback callback)
        {
            //ExportCallback exportCallback = callback;
            var context = this._context;

            if (context == null)
            {
                throw new Exception("请先调用 NewExcel()");
            }
            var sheet = context.Sheet;

            if (sheet == null)
            {
                throw new Exception("请先调用 NewSheet()");
            }

            int rowIndex = 0, y = context.StartRowIndex;

            context.CellType = CellType.DataCell;
            do
            {
                IRow row = null;
                int  columnIndex = 0, x = 0;

                context.RowIndex = rowIndex;
                while (true)
                {
                    context.Reset();
                    context.ColumnIndex = columnIndex;

                    if (exportable.NextCell(rowIndex, columnIndex, context))
                    {
                        if (callback != null)
                        {
                            callback(context);
                        }
                        if (context.IsValid)
                        {
                            // 创建单元格
                            if (row == null)
                            {
                                row = sheet.CreateRow(y++);
                            }
                            var cell = row.CreateCell(x++);

                            // 设置单元格
                            SetCell(cell, context);
                        }

                        ++columnIndex;
                    }
                    else
                    {
                        break;
                    }
                }

                ++rowIndex;
            } while (exportable.NextRow(rowIndex));
            context.StartRowIndex = y;

            return(this);
        }