使用EPPlus将GridView导出到Excel

时间:2016-10-19 20:41:07

标签: asp.net vb.net epplus

我正在尝试将gridview导出为ex​​cel,当我将Gridview转换为datatable时。我在数据表中没有任何值。以下是我的代码:

Private Sub ExportToExcel()
    FormatUtility.DateFormat(Date.Today.Month, dateMM)
    FormatUtility.DateFormat(Date.Today.Day, dateDD)
    ' Dim dvExWrd As New DataView
    Response.Clear()
    Response.Charset = ""
    Response.ContentEncoding = System.Text.Encoding.UTF8
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Response.AddHeader("content-disposition", "attachment;filename=GridData.xlsx")
    FormatExcelTransfer()
    Dim GV As GridView = CType(Session("GridView"), GridView) ' I checked, I have the value in GV
    Dim dt As DataTable = TryParse(GV.DataSource, DataTable) 'I have a value of nothing in dt

    Using pck As New ExcelPackage()

        Dim wsDt As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet1")
        wsDt.Cells("A1").LoadFromDataTable(dt, True, TableStyles.None)
        wsDt.Cells(wsDt.Dimension.Address).AutoFitColumns()

        Response.BinaryWrite(pck.GetAsByteArray())
    End Using

    Response.Flush()
    Response.End()
End Sub

Dim GV As GridView = CType(Session(“GridView”),GridView)'我查了一下,我在GV中有价值          Dim dt As DataTable = TryParse(GV.DataSource,DataTable)'我在dt中没有任何值

我不确定我做错了什么或者是否有其他方法将Gridview转换为DataTable的方式,我使用的是错误的

0 个答案:

没有答案