//
you're reading...
ASP.NET, featured, Programmazione

ASP.NET: esportare il contenuto di una Gridview in un file leggibile da Excel

Snippet VB.NET (ma adattabile rapidamente anche in C#) rapido-rapido: salviamo il contenuto del controllo GridView1in un file html (leggibile quindi con MS Excel), mantenendone le formattazioni:


Dim SB As New StringBuilder()
Dim SW As New System.IO.StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)

Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New HtmlTextWriter(stringWrite)

Dim frm As New HtmlForm()
PlanningGridView.Parent.Controls.Add(frm)
frm.Attributes("runat") = "server"
frm.Controls.Add(<strong>GridView1</strong>)

frm.RenderControl(htmlWrite)
Response.ContentType = "application/csv"
Response.AddHeader("Content-Disposition", "attachment;filename=Export.xls;")
Response.Charset = ""
Response.Clear()
Response.Write(stringWrite.ToString())
Response.End()

Discussione

Non c'è ancora nessun commento.

Lascia un Commento

Fill in your details below or click an icon to log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Log Out / Modifica )

Foto Twitter

You are commenting using your Twitter account. Log Out / Modifica )

Foto di Facebook

You are commenting using your Facebook account. Log Out / Modifica )

Connecting to %s

Archivio

Argomenti

Follow

Get every new post delivered to your Inbox.

Join 325 other followers