【问题标题】:Exporting data to excel in vb.net在 vb.net 中将数据导出到 excel
【发布时间】:2011-09-28 21:22:24
【问题描述】:

我无法将我的数据导出到 Excel。
我已经尝试过关于 S/O 的建议,但没有任何运气。

        Dim sqlString As String = "spExportRateProfile" & Session("OfficeNumber") & "," & Session("SalesRepID")
        Dim conn As SqlConnection = New SqlConnection(Utils.GetConfigKey("ConnectionStringVimas"))
        conn.Open()
        Dim dt As DataTable = New DataTable()
        Dim da As SqlDataAdapter = New SqlDataAdapter(sqlString, conn)
        da.Fill(dt)

        Response.AddHeader("content-disposition", "attachment;filename=ReportExport.xlsx")
        Response.ContentType = "application/vnd.ms-excel"  

在此之后我需要做什么才能将我的数据导出到 Excel?

【问题讨论】:

标签: asp.net vb.net export-to-excel


【解决方案1】:

您可以使用像 EPPlus(GPL) 这样的 ExcelLibrary,我强烈推荐。

那么从 DataTable 创建 Excel 文件并将其写入响应就这么简单:

Dim pck = New ExcelPackage()
Dim ws = pck.Workbook.Worksheets.Add("Worksheet-Name")
ws.Cells("A1").LoadFromDataTable(dt, True, OfficeOpenXml.Table.TableStyles.Medium1)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;  filename=ExcelFileName.xlsx")
Response.BinaryWrite(pck.GetAsByteArray())

这是另一个例子:http://epplus.codeplex.com/wikipage?title=WebapplicationExample

【讨论】:

    【解决方案2】:

    一旦你有你的数据表dt在这里你应该这样做(C# - 刚刚从互联网上复制)

    ...
    da.Fill(dt);
    
    Response.ContentType = "Application/x-msexcel";
    Response.AddHeader("content-disposition", "attachment; filename=\"test.csv\"");
    Response.Write((new ExportXMLCSV()).ToCSV(dt));
    Response.End();
    

    这里是ExportXMLCSV类的方法ToCSV(C# - 刚刚从网上复制)

    public string ToCSV(DataTable dataTable)
        {
            //create the stringbuilder that would hold our data
            StringBuilder sb = new StringBuilder();
            //check if there are columns in our datatable
            if (dataTable.Columns.Count != 0)
            {
                //loop thru each of the columns so that we could build the headers
                //for each field in our datatable
                foreach (DataColumn column in dataTable.Columns)
                {
                    //append the column name followed by our separator
                    sb.Append(column.ColumnName + ',');
                }
                //append a carriage return
                sb.Append("\r\n");
                //loop thru each row of our datatable
                foreach (DataRow row in dataTable.Rows)
                {
                    //loop thru each column in our datatable
                    foreach (DataColumn column in dataTable.Columns)
                    {
                        //get the value for tht row on the specified column
                        // and append our separator
                        sb.Append(row[column].ToString() + ',');
                    }
                    //append a carriage return
                    sb.Append("\r\n");
                }
            }
            //return our values
            return sb.ToString();
        }
    

    刚刚从这里复制的所有内容:http://forums.asp.net/t/1115305.aspx 你应该很好地进行一些转换 C# -> VB.NET 的练习;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2014-03-02
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多