Wijmo 更优美的jQuery UI部件集:导出Wijmo的GridView到Excel
来源:互联网 发布:js手机验证正则表达式 编辑:程序博客网 时间:2024/06/04 18:13
Wijmo GridView 控件不提供导出Excel文件的方法。本篇博客介绍一种将Wijmo的GridView控件保存到Excel的简单方法。你可以使用同样的方法在C1 WebUI GridView上。
步骤1 : 将C1GridView绑定至数据源
第一步是将C1GridView绑定到数据源。为了简单起见,我们将其绑定到C1Nwind.mdb的Customers表。
步骤2 : 导出C1GridView 至Excel
导出到Excel需要分成两步。第一步是将GridView保存至一个HTML字符串。
Web控件有一个RenderControl()方法可以将服务器端控件的内容输出到指定的HtmlTextWriter对象。如果启用了Tracing,该方法还将存储控件的Trace信息。然后该HtmlTextWriter对象输出到一个StringWriter对象。
下面的方法被用来创建一个字符串:
Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String '创建一个stringwriter Dim stringWrite As New System.IO.StringWriter() '创建一个使用该stringwriter的htmltextwriter Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) Dim dg As C1.Web.Wijmo.Controls.C1GridView.C1GridView 'just set the input datagrid = to the new dg grid dg = dgExport '将header的字体加粗 dg.HeaderStyle.Font.Bold = True '如果需要,这里是在组件级别改变颜色/格式 dg.HeaderStyle.ForeColor = System.Drawing.Color.Black dg.RowStyle.ForeColor = System.Drawing.Color.Black '绑定修改后的datagrid '告诉datagrid将自己呈现到我们提供的htmltextwriter dg.AllowSorting = False dg.AllowPaging = False dg.AllowCustomPaging = False '新的代码 Dim parent As Control = dg.Parent parent.Controls.Remove(dg) dg.RenderControl(htmlWrite) '新的代码 parent.Controls.Add(dg) '输出HTML Return stringWrite.ToString()End Function
下一步,我们将在一个Button Click事件中调用这个DownloadToExcel 方法从保存的字符串创建一个excel文件。
Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse)'清理 response.objectresponse.Clear()response.Buffer = Trueresponse.Charset = ""'设置响应的MIME类型为excelresponse.ContentType = "application/vnd.ms-excel"response.ContentEncoding = New System.Text.UTF8Encoding()response.Write(content)response.End()End Sub
实现时的问题
在相当多的情况下,你会在导出时遇到一些错误。你可能会收到一条错误信息:“RegisterForEventValidation 只能在Render()过程中被调用;”。在这种情况下,请尝试以下方法:
1. 你可以向下面的文章描述的那样,重载VerifyRenderingInServerForm 方法:
http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
2. 为了避免收到“RegisterForEventValidation 只能在Render()过程中被调用;”异常,可以关闭Page.EnableEventValidation 或者将RenderControl方法调用放置在一个try-catch块中。
此外,如果gridview包含一个复选框或者一个模板列,你会收到上面的错误。目前已发现微软发布的GridView会发生同样的错误。由于C1GridView继承自微软发布的GridView,所以它是C1GridView的已知设计问题。
下载示例
Wijmo下载,请进入Studio for ASP.NET Wijmo 2012 v1正式发布(2012.03.22更新)!
- Wijmo 更优美的jQuery UI部件集:导出Wijmo的GridView到Excel
- Wijmo 更优美的jQuery UI部件集:发现 Wijmo
- Wijmo 更优美的jQuery UI部件集:运行时处理Wijmo GridView数据操作
- Wijmo 更优美的jQuery UI部件集:爱上 ThemeRoller
- Wijmo 更优美的jQuery UI部件集:自定义 C1WijMenu
- Wijmo 更优美的jQuery UI部件集:爱上 ThemeRoller
- Wijmo 更优美的jQuery UI部件集:在对Wijmo GridView进行排序或者过滤时保留选择
- Wijmo 更优美的jQuery UI部件集:通过jsFiddle测试Wijmo Gauges
- Wijmo 更优美的jQuery UI部件集:在安全站点使用Wijmo控件
- Wijmo 更优美的jQuery UI部件集:C1 Wijmo Grids 更多惊喜
- Wijmo 更优美的jQuery UI部件集:从wijwizard和wijpager开始
- Wijmo 更优美的jQuery UI部件集:服务器端Grid魔法
- Wijmo 更优美的jQuery UI部件集:复合图表(CompositeChart)
- Wijmo 更优美的jQuery UI部件集:活动日历控件(Event Calendar)
- Wijmo 更优美的jQuery UI部件集:客户端更改C1GridView数据源
- Wijmo 更优美的jQuery UI部件集:复合图表(CompositeChart)
- 用于 Windows8 的 Wijmo Charts 图表控件
- 新一代 的HTML5开发工具Wijmo
- 让cygwin启动时加载.bashrc
- hdu 1698 Just a Hook(线段树成)
- Android PinyinIME 源码笔记 -- 0. 简介
- Matlab增加块注释
- 其他命令M
- Wijmo 更优美的jQuery UI部件集:导出Wijmo的GridView到Excel
- php截取中文字符串函数及测试
- 设计模式非常有用的概念
- CentOS rpm yum 安装与卸载
- 非变动性算法
- struts第六篇:struts核心类ActionForm
- SQLite的编译指令Pragma
- 其他命令N
- JS变量的未定义和未赋值