簡體   English   中英

在Excel VB中打開數據表

[英]Open DataTable in Excel VB

好的,這是一個有趣的問題。 我的任務是修改現有的VB項目。 當前,用戶從一系列下拉菜單中選擇一個sql查詢,然后運行該查詢。 因此,用戶選擇“環境”下拉列表,該下拉列表的結果將填充類別下拉列表。 選擇類別后,他們將獲得可用查詢的下拉列表。 選擇查詢並單擊“運行”按鈕后,他們將獲得帶有查詢結果的表格視圖。 一些查詢結果是巨大的。 我正在作為測試運行的查詢具有40列和20,000條記錄。 查詢運行時間不到5秒,但是渲染網格視圖需要一分鍾以上的時間。 完成gridview的渲染后,用戶可以選擇將結果導出到Excel。 通過這個,我的意思是代碼通過gridview.RenderControl打開Excel的實例,並在Excel中顯示結果。 用戶不想保存excel文件,然后導航到該文件,而是希望它從所使用的Web表單中直接打開,這是代碼當前正在執行的操作。

但是,用戶並不關心gridview。 他們根本不在乎是否看到它。 他們只想打開Excel。 因此,我不想使用gridview.RenderControl,而是要打開Excel並在內存中填充DataTable(或DataSet)。 關於最佳方法的想法嗎?

它們當前如何填充gridview:Dim MyConnection為SqlConnection Dim MyCommand為SqlCommand Dim MyDataTable為DataTable Dim MyReader為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實例中呈現此記錄集。

好的,這是我找到的解決方案(以防萬一有人感興趣)。 實際上,這非常簡單。 我只是遍歷數據表並使用了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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM