• 【原创】[C#]WinForm中DataGrid扩展 快速导出Excel文件 (1)(续)


    [C#]WinForm中DataGrid扩展 - 快速导出Excel文件 (1)(续)

    经过对Excel深入了解,采用数据写入到range的方法,效率更高,更明显;与常用的逐单元格写有所不同,可查看[C#]WinForm中DataGrid扩展 - 导出Excel文件 (1)

    本例的实现方式以[C#]WinForm中DataGrid扩展 - 导出Excel文件 (1)相似。

     1public bool ExportExcel(string p_ReportName)
     2        {
     3            if ( this.TableStyles.Count == 0 ) return false;
     4            DataGridTableStyle ts = this.TableStyles[0];
     5
     6            // 创建Excel对象                    --LeeWenjie    2006-11-29
     7            Excel.Application xlApp = new Excel.ApplicationClass();
     8            if ( xlApp == null )
     9            {
    10                MessageBox.Show("Excel无法启动");
    11                return false;
    12            }

    13            // 创建Excel工作薄
    14            Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
    15            Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
    16            
    17            // 设置标题
    18            Excel.Range range = xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ts.GridColumnStyles.Count]);
    19            range.MergeCells = true;
    20            xlApp.ActiveCell.FormulaR1C1 = p_ReportName;
    21            xlApp.ActiveCell.Font.Size = 20;
    22            xlApp.ActiveCell.Font.Bold = true;
    23            xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
    24
    25            // 列索引,行索引,总列数,总行数
    26            int colIndex = 0;
    27            int RowIndex = 0;
    28            int colCount = ts.GridColumnStyles.Count;
    29            int RowCount = this.BindingContext[this.DataSource,this.DataMember].Count;
    30
    31            // 创建缓存数据
    32            object[,] objData = new object[RowCount + 1, colCount ];
    33            // 获取列标题
    34            foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
    35            {
    36                objData[RowIndex,colIndex++= cs.HeaderText;    
    37            }
                
    38            // 获取数据
    39            for(RowIndex =1;RowIndex< RowCount;RowIndex++)
    40            {
    41                for(colIndex=0;colIndex < colCount;colIndex++)
    42                {
    43                    objData[RowIndex,colIndex] = this[RowIndex-1,colIndex];
    44                }

    45                Application.DoEvents();
    46            }

    47            // 写入Excel
    48            range = xlSheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);            
    49            range.Value2 = objData;
    50
    51            // 保存
    52            try
    53            {
    54                xlBook.Saved  = true;
    55                xlBook.SaveCopyAs("D:\\Fly" + DateTime.Now.ToString("yyyyMMdd"+ ".xls");
    56            }

    57            catch
    58            {
    59                MessageBox.Show("保存出错,请检查!");
    60                return false;
    61            }

    62            finally
    63            {
    64                xlApp.Quit();
    65                GC.Collect();
    66            }

    67            return true;
    68        }

    开发环境:
    VS.Net 2003

    改进后,效率提高N倍,8000条数据大约需要2秒。

    **************************************
    本系列相关文章,敬请关注
    完整的DataGridEx原代码,正在整理中,有需要请留言)。
    ------------------------------------------------------
    [C#]WinForm中DataGrid扩展 - 导出Excel文件 (1)
    [C#]WinForm中DataGrid扩展 - 快速导出Excel文件 (1)(续)
    [C#]WinForm中DataGrid扩展 - 列样式扩展(2)
    [C#]WinForm中DataGrid扩展 - 自定义行颜色(3)
    [C#]WinForm中DataGrid扩展 - 多列排序(4)
    [C#]WinForm中DataGrid扩展 - 自动生成列样式(5)

  • 相关阅读:
    ckeditor 实现图片上传以及预览(亲测有效)
    学习OpenStack之 (4): Linux 磁盘、分区、挂载、逻辑卷管理 (Logical Volume Manager)
    学习OpenStack之 (1):安装devstack
    学习OpenStack之 (0):基础知识
    Aho-Corasick 多模式匹配算法、AC自动机详解
    标准C++中的string类的用法总结
    STL队列 之FIFO队列(queue)、优先队列(priority_queue)、双端队列(deque)
    linux下文件合并、分割、去重
    设计模式之观察者模式(Observable与Observer)
    访问者模式讨论篇:java的动态绑定与双分派
  • 原文地址:https://www.cnblogs.com/LeeWenjie/p/576062.html
Copyright © 2020-2023  润新知