簡體   English   中英

在vb.net中將數據導出到Excel

[英]Exporting data to excel in vb.net

我無法將數據導出到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?

您可以使用像我可以推薦的EPPlus (GPL)這樣的ExcelLibrary

然后,從DataTable創建Excel文件並將其寫入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())

這是另一個示例: http : //epplus.codeplex.com/wikipage?title=WebapplicationExample

一旦在這里有了數據表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 ;-)應該很好。

暫無
暫無

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

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