在Excel VB中打开DataTable

时间:2015-03-16 22:44:20

标签: asp.net vb.net excel datatable rendercontrol

好的,这是一个有趣的问题。我的任务是修改现有的VB项目。目前,用户从一系列下拉列表中选择一个sql查询,然后运行该查询。因此,用户选择和环境下拉列表,该下拉列表的结果填充类别下拉列表。选择类别后,他们会获得可用查询的下拉列表。一旦他们选择了一个查询并点击“运行”按钮,他们就会得到一个包含查询结果的网格视图。一些查询结果很大。我作为测试运行的查询有40列和20,000条记录。查询在不到5秒的时间内运行,但渲染gridview需要一分多钟。网格视图完成渲染后,用户可以选择将结果导出到Excel。通过这个,我的意思是代码通过gridview.RenderControl打开一个Excel实例,并在Excel中显示结果。用户不想保存excel文件,然后导航到该文件,他们希望它从他们正在使用的webform中打开,这正是代码当前所做的。

但是,用户并不关心gridview。如果他们完全看到它们就不在乎。他们想要打开Excel。因此,我想打开Excel并使用内存中的DataTable(或DataSet)填充它,而不是使用gridview.RenderControl。有关最佳方法的任何想法吗?

以下是他们当前填充gridview的方式:             Dim MyConnection作为SqlConnection             Dim MyCommand As SqlCommand             Dim MyDataTable As DataTable             Dim MyReader As SqlDataReader

        MyConnection = New SqlConnection()
        MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings(Connection).ConnectionString

        MyCommand = New SqlCommand()
        MyCommand.CommandText = Sqlquery
        MyCommand.CommandType = CommandType.Text
        MyCommand.Connection = MyConnection

        MyCommand.Connection.Open()
        MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

        MyDataTable = New DataTable()
        MyDataTable.Load(MyReader)

        If (MyDataTable.Rows.Count > 0) Then
            QueryresultPanel.Visible = True
            gvLineItems.DataSource = MyDataTable
            gvLineItems.DataBind()
        End If

        MyDataTable.Dispose()
        MyCommand.Dispose()
        MyConnection.Dispose()

以下是他们如何打开和填充Excel实例:

     Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
        Response.Clear()
        Response.Buffer = True
        '
        ' Set the content type to Excel.
        ' 
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        ' 
        ' Turn off the view state.
        ' 
        Me.EnableViewState = False
        Dim oStringWriter As New System.IO.StringWriter()
        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
        ' 
        ' Get the HTML for the control.
        '
        gvLineItems.RenderControl(oHtmlTextWriter)
        ' 
        ' Write the HTML back to the browser.
        '
        Response.Write(oStringWriter.ToString())
        Response.[End]()
    End Sub

显然,DataTable或DataSet没有RenderControl,也无法弄清楚如何在Excel实例中渲染此记录集而不将其保存到文件中。

1 个答案:

答案 0 :(得分:0)

好的,这是我找到的解决方案(如果有人有兴趣的话)。实际上它非常简单。我只是通过数据表循环并使用了StringWriter。

    Protected Sub WriteToExcelFile(dt As DataTable)
    Dim sw As StringWriter

    For Each datacol As DataColumn In dt.Columns
        sw.Write(datacol.ColumnName + vbTab)
    Next

    Dim row As DataRow
    For Each row In dt.Rows
        sw.Write(vbNewLine)
        Dim column As DataColumn
        For Each column In dt.Columns
            If Not row(column.ColumnName) Is Nothing Then
                sw.Write(row(column).ToString() + vbTab)
            Else
                sw.Write(String.Empty + vbTab)
            End If
        Next column
    Next row

    Response.Clear()
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader("Content-Disposition", "attachment;filename=DataTable.xls")
    Response.Output.Write(sw.ToString())
    Response.Flush()
    System.Web.HttpContext.Current.Response.Flush()
    System.Web.HttpContext.Current.Response.SuppressContent = True
    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub