GridView 汇出 Excel 及 Word 文件是蛮常使用的需求,此篇文章将扩展 GridView 控件提供汇出 Excel 及 Word 文件的方法。一般在 GridView 汇出的常见下列问题也会在此一并被解决。
GridView 汇出的常见问题:
问题1. GridView 使用 RenderControl 方法产生的错误。
问题2. GridView 分页的问题。
问题3. 汇出文件名称产生乱码的问题。
问题4. 汇出内容产生乱码的问题。
解决 GridView 汇出问题
我们先针对 GridView 汇出的问题,逐一处理解决
问题1. GridView 使用 RenderControl 方法产生的错误。
此问题参考上篇「使用 BasePage 来解决 GridView 执行 RenderControl 产生的错误」来解决。
问题2. GridView 分页的问题。
在执行汇出时,若 GridView 有使用分页(AllowPaging=True),则采下列步骤处理。
Step1.取消分页,即设定 GridView.AllowPaging=False。
Step2.GridView 执行 DataBind,使其重新系结所有数据,再使用 RenderCotnrol 输出 HTML 程序代码。
Step3.还原分页,即设定 GridView.AllowPaging=True。
问题3. 汇出文件名称产生乱码的问题。
针对此问题,只需将文件名称经 UrlEncode 编码,即可解决中文文件名的问题。
HttpUtility.UrlEncode(FileName, Encoding)
问题4. 汇出内容产生乱码的问题。
使用 Response 输出 <meta http-equiv='Content-Type'; content='text/html';charset='utf-8'> 来解决内容乱码的问。
扩展 GridView 控件
我们继承 GridView 命名为 TBGridView,其中新增 Export、ExportExcel、ExportWord 等汇出方法。
1
Imports
System
2 Imports System.Collections.Generic
3 Imports System.ComponentModel
4 Imports System.Text
5 Imports System.Web
6 Imports System.Web.UI
7 Imports System.Web.UI.WebControls
8 Imports System.Drawing
9
10 Namespace WebControls Namespace WebControls
11 < _
12 Description("TBGridView 控件"), _
13 ToolboxData("<{0}:TBGridView runat=server></{0}:TBGridView>") _
14 > _
15 Public Class TBGridViewClass TBGridView
16 Inherits GridView
17
18 /**/''' <summary>
19 ''' GridView 控件汇出 Excel 文件。
20 ''' </summary>
21 Public Sub ExportExcel()Sub ExportExcel()
22 Export(Encoding.UTF8, "gridview.xls", "application/ms-excel")
23 End Sub
24
25 /**/''' <summary>
26 ''' GridView 控件汇出 Word 文件。
27 ''' </summary>
28 Public Sub ExportWord()Sub ExportWord()
29 Export(Encoding.UTF8, "gridview.doc", "application/ms-word")
30 End Sub
31
32 /**/''' <summary>
33 ''' GridView 控件汇出。
34 ''' </summary>
35 ''' <param name="Encoding">编码方式。</param>
36 ''' <param name="FileName">文件名称。</param>
37 ''' <param name="ContentType">内容类型标头。</param>
38 Public Sub Export()Sub Export(ByVal Encoding As Encoding, ByVal FileName As String, ByVal ContentType As String)
39 Dim oResponse As HttpResponse
40 Dim oStringWriter As System.IO.StringWriter
41 Dim oHtmlWriter As System.Web.UI.HtmlTextWriter
42 Dim bAllowPaging As Boolean
43 Dim sText As String
44 Dim sFileName As String
45
46 If TypeOf Me.Page Is TBBasePage Then
47 DirectCast(Me.Page, TBBasePage).IsVerifyRender = False '页面不需验证控件
48 End If
49
50 '文件名称需经 UrlEncode 编码,解决中文文件名的问题
51 sFileName = HttpUtility.UrlEncode(FileName, Encoding)
52
53 oResponse = HttpContext.Current.Response
54 oResponse.Clear()
55 sText = "<meta http-equiv='Content-Type'; content='text/html';charset='{0}'>"
56 sText = String.Format(sText, Encoding.WebName)
57 oResponse.Write(sText)
58 oResponse.AddHeader("content-disposition", "attachment;filename=" & sFileName)
59 oResponse.ContentEncoding = Encoding
60 oResponse.Charset = Encoding.WebName
61 oResponse.ContentType = "application/ms-excel"
62
63 ' If you want the option to open the Excel file without saving than
64 ' comment out the line below
65 ' oResponse.Cache.SetCacheability(HttpCacheability.NoCache)
66
67 oStringWriter = New System.IO.StringWriter()
68 oHtmlWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
69 bAllowPaging = Me.AllowPaging
70 If bAllowPaging Then
71 Me.AllowPaging = False
72 If Me.RequiresDataBinding Then
73 Me.DataBind()
74 End If
75 End If
76
77 Me.RenderControl(oHtmlWriter)
78
79 If bAllowPaging Then
80 AllowPaging = bAllowPaging
81 End If
82 oResponse.Write(oStringWriter.ToString())
83 oResponse.End()
84 End Sub
85
86 End Class
87End Namespace
2 Imports System.Collections.Generic
3 Imports System.ComponentModel
4 Imports System.Text
5 Imports System.Web
6 Imports System.Web.UI
7 Imports System.Web.UI.WebControls
8 Imports System.Drawing
9
10 Namespace WebControls Namespace WebControls
11 < _
12 Description("TBGridView 控件"), _
13 ToolboxData("<{0}:TBGridView runat=server></{0}:TBGridView>") _
14 > _
15 Public Class TBGridViewClass TBGridView
16 Inherits GridView
17
18 /**/''' <summary>
19 ''' GridView 控件汇出 Excel 文件。
20 ''' </summary>
21 Public Sub ExportExcel()Sub ExportExcel()
22 Export(Encoding.UTF8, "gridview.xls", "application/ms-excel")
23 End Sub
24
25 /**/''' <summary>
26 ''' GridView 控件汇出 Word 文件。
27 ''' </summary>
28 Public Sub ExportWord()Sub ExportWord()
29 Export(Encoding.UTF8, "gridview.doc", "application/ms-word")
30 End Sub
31
32 /**/''' <summary>
33 ''' GridView 控件汇出。
34 ''' </summary>
35 ''' <param name="Encoding">编码方式。</param>
36 ''' <param name="FileName">文件名称。</param>
37 ''' <param name="ContentType">内容类型标头。</param>
38 Public Sub Export()Sub Export(ByVal Encoding As Encoding, ByVal FileName As String, ByVal ContentType As String)
39 Dim oResponse As HttpResponse
40 Dim oStringWriter As System.IO.StringWriter
41 Dim oHtmlWriter As System.Web.UI.HtmlTextWriter
42 Dim bAllowPaging As Boolean
43 Dim sText As String
44 Dim sFileName As String
45
46 If TypeOf Me.Page Is TBBasePage Then
47 DirectCast(Me.Page, TBBasePage).IsVerifyRender = False '页面不需验证控件
48 End If
49
50 '文件名称需经 UrlEncode 编码,解决中文文件名的问题
51 sFileName = HttpUtility.UrlEncode(FileName, Encoding)
52
53 oResponse = HttpContext.Current.Response
54 oResponse.Clear()
55 sText = "<meta http-equiv='Content-Type'; content='text/html';charset='{0}'>"
56 sText = String.Format(sText, Encoding.WebName)
57 oResponse.Write(sText)
58 oResponse.AddHeader("content-disposition", "attachment;filename=" & sFileName)
59 oResponse.ContentEncoding = Encoding
60 oResponse.Charset = Encoding.WebName
61 oResponse.ContentType = "application/ms-excel"
62
63 ' If you want the option to open the Excel file without saving than
64 ' comment out the line below
65 ' oResponse.Cache.SetCacheability(HttpCacheability.NoCache)
66
67 oStringWriter = New System.IO.StringWriter()
68 oHtmlWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
69 bAllowPaging = Me.AllowPaging
70 If bAllowPaging Then
71 Me.AllowPaging = False
72 If Me.RequiresDataBinding Then
73 Me.DataBind()
74 End If
75 End If
76
77 Me.RenderControl(oHtmlWriter)
78
79 If bAllowPaging Then
80 AllowPaging = bAllowPaging
81 End If
82 oResponse.Write(oStringWriter.ToString())
83 oResponse.End()
84 End Sub
85
86 End Class
87End Namespace
测试程序
在页面放置一个 GridView 控件系结数据并设定分页,另外放置二个按钮分别做「汇出 Excel 文件」及「汇出 Word 文件」的动作。
汇出按钮撰写的程序代码如下
1
Protected
Sub btnExcportExcel_Click()
Sub btnExcportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcportExcel.Click
2 GridView1.ExportExcel()
3 End Sub
4
5 Protected Sub btnExportWord_Click() Sub btnExportWord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportWord.Click
6 GridView1.ExportWord()
7 End Sub
2 GridView1.ExportExcel()
3 End Sub
4
5 Protected Sub btnExportWord_Click() Sub btnExportWord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportWord.Click
6 GridView1.ExportWord()
7 End Sub
汇出 Excel 文件的结果如下所示
汇出 Word 文件的结果如下所示