赞
踩
‘=====gird导出模式
Protected Sub Btn_daochu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_daochu.Click
Dim filename As String = DateTime.Now.ToString("yyyyMMddhhmmss")
filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) '//解决导出EXCEL时乱码的问题
Response.ClearContent()
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.ContentType = "application/excel"
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls")
Response.ContentType = "application/ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
grid.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
End Sub
’====datatable导出模式
Public Shared Sub DataTable2Excel(ByVal dtData As System.Data.DataTable)
Dim dgExport As System.Web.UI.WebControls.DataGrid = Nothing
' 当前对话
Dim curContext As System.Web.HttpContext = System.Web.HttpContext.Current
' IO用于导出并返回excel文件
Dim strWriter As System.IO.StringWriter = Nothing
Dim htmlWriter As System.Web.UI.HtmlTextWriter = Nothing
If Not dtData Is Nothing Then
' 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel"
'curContext.Response.ContentEncoding = System.Text.Encoding.UTF8
curContext.Response.Charset = ""
Dim filename As String = DateTime.Now.ToString("yyyyMMddhhmmss")
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls")
' 导出excel文件
strWriter = New System.IO.StringWriter()
htmlWriter = New System.Web.UI.HtmlTextWriter(strWriter)
' 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = New System.Web.UI.WebControls.DataGrid()
dgExport.DataSource = dtData.DefaultView
dgExport.AllowPaging = False
dgExport.DataBind()
' 返回客户端
dgExport.RenderControl(htmlWriter)
curContext.Response.Write(strWriter.ToString())
curContext.Response.End()
End If
End Sub
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。