domenica 11 gennaio 2015

VB.Net esportare un datatable in Excel

Qui di seguito si riporta un frammento di codice che dato un oggetto di tipo DataTable viene esportato in file Excel
Dim DttRisultato As DataTable = CreaDati()
      

        Dim attachment As String = "attachment; filename=FileName.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        Dim tab As String = ""
        For Each Colonna As DataColumn In DataTableRisultato.Columns
            Response.Write(tab + Colonna.ColumnName)
            tab = vbTab
        Next
        Response.Write(vbLf)
        Dim Conta As Integer
        For Each riga As DataRow In DataTableRisultato.Rows
            tab = ""
            For Conta  = 0 To DataTableRisultato.Columns.Count - 1
                Response.Write(tab & riga (Conta ).ToString())
                tab = vbTab
            Next
            Response.Write(vbLf)
        Next
        Response.End()

Nessun commento: