简体   繁体   English

使用保存对话框将数据表导出到Excel文件

[英]Exporting datatable to excel file with save dialog

I have a datatable being created with various inputs. 我有一个由各种输入创建的数据表。 Sometimes the resulting table is 35000+ rows. 有时结果表是35000+行。 Currently, the datatable gets displayed onto a gridview. 当前,数据表已显示在gridview上。 It loads fine after a couple minutes. 几分钟后加载正常。 Then, theres an option to export the gridview to an excel file. 然后,有一个选项可以将gridview导出到excel文件。 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. 我的目标是绕过gridview步骤并获取格式化的表格,并将其直接放入Excel文件中。 Could also be a csv file if thats faster to write/load, as long as the data table is similar to the gridview output. 如果数据写入/加载与gridview输出类似,则如果写入/加载速度更快,也可以是csv文件。

I tried the following code here Export DataTable to Excel File . 我在这里尝试了以下代码, 将DataTable导出到Excel File I did my best to convert it to vb, here... 我尽力将其转换为vb,在这里...

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. CreateDataSource()是在内存中创建的表。 Then theres other buttons that call it to fill the gridview. 然后还有其他按钮可以调用它来填充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. 当我同时尝试xls和csv文件时,会发生这种情况。 Something is not getting translated right. 某些内容未正确翻译。 Any solutions? 有什么办法吗?

(Written with help from Google) Create an export using the StringWriter class : (在Google的帮助下编写)使用StringWriter类创建导出:

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. 您只需要传递函数DataSet和File Name。 If you do not want to edit your CreateDataSource() function, you can merge it into a DataSet first like so: 如果您不想编辑CreateDataSource()函数,则可以先将其合并到DataSet中,如下所示:

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. 根据Microsoft,当您打开“忽略使用动态数据交换(DDE)的其他应用程序”设置时,会发生这种情况。 (See here ). (请参阅此处 )。 The link includes instructions to change the setting. 该链接包含更改设置的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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