将Gridview导出到Excel,但导出整个aspx页面 [英] Export Gridview to Excel but the whole aspx page exported instead
本文介绍了将Gridview导出到Excel,但导出整个aspx页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Public Shared Sub DataGridToExcelSchs(ByVal dgExport As DataGrid, ByVal response As HttpResponse)
'clean up the response.object
response.Clear()
response.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter()
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New DataGrid()
' just set the input datagrid = to the new dg grid
dg = dgExport
' I want to make sure there are no annoying gridlines
dg.GridLines = GridLines.Vertical
' Make the header text bold
dg.HeaderStyle.Font.Bold = True
' If needed, here's how to change colors/formatting at the component level
dg.HeaderStyle.ForeColor = System.Drawing.Color.White
dg.HeaderStyle.BackColor = System.Drawing.Color.Red
dg.HeaderStyle.BorderStyle = BorderStyle.Solid
dg.HeaderStyle.BorderWidth = New Unit(1)
dg.HeaderStyle.BorderColor = System.Drawing.Color.WhiteSmoke
'dg.ItemStyle .ForeColor = System.Drawing.Color.Black
'bind the modified datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'output the html
response.Write(stringWrite.ToString)
response.End()
End Sub
我试过以上代码。请帮忙。
I am tried to above code . Please help for same.
推荐答案
请按照这个例子。这有vb.net代码
http://aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx [ ^ ]
Please follow this example. This has vb.net code
http://aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx[^]
这篇关于将Gridview导出到Excel,但导出整个aspx页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文