在vb.net中将数据导出到excel

发布于 2024-12-07 05:36:22 字数 642 浏览 0 评论 0原文

我无法将数据导出到 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?

I am unable to export my data into excel.
I have tried the suggestions on S/O, but have not had any luck.

        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"  

What do I need do after this to export my data to excel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

口干舌燥 2024-12-14 05:36:22

您可以使用我强烈推荐的 ExcelLibrary,例如 EPPlus(GPL)。

然后,从数据表创建 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

You could use a ExcelLibrary like EPPlus(GPL) which i can warmly recommend.

Then it is as easy as this to create Excel-Files from a DataTable and write it to the Response:

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())

Here is another example: http://epplus.codeplex.com/wikipage?title=WebapplicationExample

久而酒知 2024-12-14 05:36:22

一旦您在此处有了数据表 dt,您就应该执行此操作(C# - 刚刚从 Internet 复制)

...
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# - 刚刚从 Internet 复制)

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;-)

once you have your datatable dt here you should do this (C# - just copied from the Internet)

...
da.Fill(dt);

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

and here the method ToCSV of the class ExportXMLCSV (C# - just copied from the Internet)

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();
    }

everything just copied from here: http://forums.asp.net/t/1115305.aspx you should be good to go with a little exercise of conversion C# -> VB.NET ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文