[索引页]
[源码下载]
扩展GridView控件(8) - 导出数据源的数据为Excel、Word或Text
作者:webabcd
/*正式版的实现 开始*/
介绍
扩展GridView控件:
导出数据源的数据为Excel、Word或Text(应保证数据源的类型为DataTable或DataSet)
使用方法:
为SmartGridView添加的方法
Export(string fileName)
Export(string fileName, ExportFormat exportFormat)
Export(string fileName, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, string[] headers, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, string[] columnNameList, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, string[] columnNameList, string[] headers, ExportFormat exportFormat, Encoding encoding)
关键代码
/*正式版的实现 结束*/
/*测试版的实现 开始*/
介绍
把GridView导出为一个Excel文件算是一个经常要用到的功能,也比较简单,我们来扩展一个GridView以实现这样的功能。
控件开发
1、新建一个继承自GridView的类。
2、重写OnRowCommand,以实现把GridView导出为Excel的功能
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,在GridView内加一个按钮,把CommandName属性设置为“ExportToExcel”,CommandArgument属性的值用“;”做分隔符分为两部分,左边的部分为导出Excel的文件名称,右边的部分为需要隐藏的列的索引(列索引用“,”分开)
ObjData.cs
Default.aspx
注:为了防止出错要在.cs代码中加上下面这句
另外,如果你的GridView中含有命令按钮的话要在.aspx页面的头部中加上下面这个属性
/*测试版的实现 结束*/
OK
[源码下载]