简体   繁体   中英

Exporting datatable to excel file with save dialog

I have a datatable being created with various inputs. Sometimes the resulting table is 35000+ rows. Currently, the datatable gets displayed onto a gridview. It loads fine after a couple minutes. Then, theres an option to export the gridview to an excel file. Everytime we have a large table to export, the conversion fails.

My goal is to bypass the gridview step and take the formatted table and put it directly into an excel file. Could also be a csv file if thats faster to write/load, as long as the data table is similar to the gridview output.

I tried the following code here Export DataTable to Excel File . I did my best to convert it to vb, here...

Protected Sub btnExportData_Click(sender As Object, e As EventArgs) Handles btnExportData.Click
    Dim dt As DataTable
    dt = CreateDataSource()
    Dim filename As String = "attachment; filename=DistComplain.xls"
    Response.ClearContent()
    Response.AddHeader("content-disposition", filename)
    Response.ContentType = "application/vnd.ms-excel"
    Dim tab As String = ""
    For Each dc As DataColumn In dt.Columns
        Response.Write((tab + dc.ColumnName))
        tab = "" & vbTab
    Next
    Response.Write("" & vbLf)
    Dim i As Integer
    For Each dr As DataRow In dt.Rows
        tab = ""
        i = 0
        Do While (i < dt.Columns.Count)
            Response.Write((tab + dr(i).ToString))
            tab = "" & vbTab
            i = (i + 1)
        Loop
        Response.Write("" & vbLf)
    Next
    Response.End()
End Sub

CreateDataSource() is the table that gets created in memory. Then theres other buttons that call it to fill the gridview. Right now it successfully complies and runs, and then it successfully creates the file. Although, when the file tries to open I get this error...

Excel错误

This happens when I try both xls and csv files. Something is not getting translated right. Any solutions?

(Written with help from Google) Create an export using the StringWriter class :

Public Shared Sub ExportDataSetToExcel(ds As DataSet, filename As String)
    Dim response As HttpResponse = HttpContext.Current.Response

    'Clean response object
    response.Clear()
    response.Charset = ""

    'Set response header
    response.ContentType = "application/vnd.ms-excel"
    response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")

    'Create StringWriter and use to create CSV
    Using sw As New StringWriter()
        Using htw As New HtmlTextWriter(sw)
            'Instantiate DataGrid
            Dim dg As New DataGrid()
            dg.DataSource = ds.Tables(0)
            dg.DataBind()
            dg.RenderControl(htw)
            response.Write(sw.ToString())
            response.[End]()
        End Using
    End Using
End Sub

You just need to pass the function the DataSet and the File Name. If you do not want to edit your CreateDataSource() function, you can merge it into a DataSet first like so:

Dim dt As DataTable = CreateDataSource()
Dim ds As New DataSet
ds.Merge(dt)

Your question is about why you're getting the message about being unable to open the file, correct?

According to Microsoft, this occurs when you have the "Ignore other applications that use Dynamic Data Exchange (DDE)" setting turned on. (See here ). The link includes instructions to change the setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM