Gridview到.xlsx

时间:2014-02-26 14:28:51

标签: asp.net excel gridview

想知道是否有人可以给我回答,如果可能将grindview导出到.xlsx(2007版本的excel),可能会给我一些提示。

我已设法将grindview转移到.xls(2003 excel)但不转移到xlsx。

我的代码是:

此致 Lime3003

Protected Sub ExportToExcel(sender As Object, e As EventArgs)
                    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=" + selReportName + " .xls")
    Response.Charset = ""
    Response.ContentType = "Application / vnd.openxmlformats - officedocument.spreadsheetml.sheet"
    'Response.ContentType = "application/vnd.ms-excel"

    Using sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)

        gwResult.GridLines = GridLines.Both
        gwResult.RenderControl(hw)
        'style to format numbers to string
        Dim style As String = "<style> .textmode { } </style>"
        Response.Write(style)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.[End]()
    End Using
End Sub

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered 
End Sub

1 个答案:

答案 0 :(得分:0)

正如Tim Schmelter说我使用EPPlus导出数据视图但我需要将其更改为数据表。

    Protected Sub ExportToExcel(sender As Object, e As EventArgs)

    Dim dt As DataTable

    dt = dvData.ToTable

    'MsgBox(gwResult.HeaderRow.Cells.Count)
    Try
        Dim pck = New OfficeOpenXml.ExcelPackage()
        Dim ws = pck.Workbook.Worksheets.Add(selReportName)
        ws.Cells("A2").LoadFromDataTable(dt, True)

        Response.Clear()
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=" + selReportName + ".xlsx")
        Response.BinaryWrite(pck.GetAsByteArray())
        'log error
    Catch ex As Exception
    End Try
    Response.[End]()

End Sub