Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用asp.net将sql表导出到excel_Asp.net_Vb.net_Export To Excel - Fatal编程技术网

是否可以使用asp.net将sql表导出到excel

是否可以使用asp.net将sql表导出到excel,asp.net,vb.net,export-to-excel,Asp.net,Vb.net,Export To Excel,我有一个带有导出按钮的网页。按下此按钮时是否可以将sql表导出到excel?我可以用gridview来做,但我只想用一个带有代码的简单按钮来做这项工作。有人能给我指一下我需要的方向吗?这里有一个实用功能,你可以使用: Public Shared Sub ExportToSpreadsheet(table As DataTable, filename As String) ' Get a hold of the HTTP context and clear it, because we a

我有一个带有导出按钮的网页。按下此按钮时是否可以将sql表导出到excel?我可以用gridview来做,但我只想用一个带有代码的简单按钮来做这项工作。有人能给我指一下我需要的方向吗?

这里有一个实用功能,你可以使用:

Public Shared Sub ExportToSpreadsheet(table As DataTable, filename As String)
    ' Get a hold of the HTTP context and clear it, because we are going to push the CSV data through the context
    Dim context = HttpContext.Current
    context.Response.Clear()

    ' Loop through each column in your data table
    For Each column As DataColumn In table.Columns
        ' Write column names
        context.Response.Write(column.ColumnName + ";")
    Next

    context.Response.Write(Environment.NewLine)

    ' Loop through each row in the data table
    For Each row As DataRow In table.Rows
        ' Loop through each column in row
        For i As Integer = 0 To table.Columns.Count - 1
            ' Write each column value
            context.Response.Write(row(i).ToString().Replace(";", [String].Empty) & ";")
        Next

        ' Write a new line between rows of data
        context.Response.Write(Environment.NewLine)
    Next

    ' Set the content type and headers
    context.Response.ContentType = "text/csv"
    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & ".csv")
    context.Response.[End]()
End Sub
然后你可以这样称呼它:

ExportToSpreadsheet(YourDataTable, "YourFileName")

注意:由于它是一个
共享
函数,因此您可以将其放入实用程序类中,而无需实例化(
新建
)类即可使用该函数。

以下是您可以使用的实用程序函数:

Public Shared Sub ExportToSpreadsheet(table As DataTable, filename As String)
    ' Get a hold of the HTTP context and clear it, because we are going to push the CSV data through the context
    Dim context = HttpContext.Current
    context.Response.Clear()

    ' Loop through each column in your data table
    For Each column As DataColumn In table.Columns
        ' Write column names
        context.Response.Write(column.ColumnName + ";")
    Next

    context.Response.Write(Environment.NewLine)

    ' Loop through each row in the data table
    For Each row As DataRow In table.Rows
        ' Loop through each column in row
        For i As Integer = 0 To table.Columns.Count - 1
            ' Write each column value
            context.Response.Write(row(i).ToString().Replace(";", [String].Empty) & ";")
        Next

        ' Write a new line between rows of data
        context.Response.Write(Environment.NewLine)
    Next

    ' Set the content type and headers
    context.Response.ContentType = "text/csv"
    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & ".csv")
    context.Response.[End]()
End Sub
然后你可以这样称呼它:

ExportToSpreadsheet(YourDataTable, "YourFileName")

注意:由于它是一个
共享的
函数,因此您可以将它放在实用程序类中,而不需要实例化(
新建
)类来使用该函数。

如果您想在excel中使用它,可以使用以下代码。选择数据并将其放在Gridview中,然后执行以下操作

Dim GridView1 As New GridView

SqlDataSource1.SelectCommand = "SELECT * FROM TableName"
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

GridView1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())
Response.End()

您还可以格式化Gridview,使其在Excel工作表上看起来很好

如果希望在excel中使用,可以使用以下代码。选择数据并将其放在Gridview中,然后执行以下操作

Dim GridView1 As New GridView

SqlDataSource1.SelectCommand = "SELECT * FROM TableName"
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

GridView1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())
Response.End()

您还可以格式化Gridview,使其在Excel工作表上看起来很好

谢谢,我把它弄好了,但是文件不是很干净,里面有很多东西;;;;;;;所有的东西都在columnA中,以使它更干净?结果是.csv而不是.xls。如果windows中的分隔符不可用,则会出现问题;但是,例如,(英语中的默认值?),我将csv改为xls,这有点帮助,但现在列名的拆分非常混乱。是的,汉克斯,我让它工作了,但是文件不是很干净,里面有一大堆垃圾;;;;;;;所有的东西都在columnA中,以使它更干净?结果是.csv而不是.xls。如果windows中的分隔符不可用,则会出现问题;但是,例如,(英语中的默认值?),我将csv改为xls,这有点帮助,但现在列名的拆分非常混乱。是的,我已经使用了该代码,但问题是它只会导出gridview中显示的内容。不是所有的行和列,因为我打开了分页,也不是所有的列都显示否。您不必在页面上使用Gridview。您可以在代码隐藏中创建Gridview并获取所有记录。。。检查上面更新的代码。我已经应用了相同的代码,但它仍然不能在我的机器上工作。是否需要更改上述代码才能在.NET 4.5版上成功运行谢谢!我在asp.net环境中使用它更新:它只做了一点修改——“我不需要在
.aspx
页面上有
datagrid
。在
运行时创建的
datagrid
,即
.aspx.cs
也可以工作。”Ps:尽管如此,还是欢迎更正。是的,我已经使用了该代码,但问题是它只会导出gridview中显示的内容。不是所有的行和列,因为我打开了分页,也不是所有的列都显示否。您不必在页面上使用Gridview。您可以在代码隐藏中创建Gridview并获取所有记录。。。检查上面更新的代码。我已经应用了相同的代码,但它仍然不能在我的机器上工作。是否需要更改上述代码才能在.NET 4.5版上成功运行谢谢!我在asp.net环境中使用它更新:它只做了一点修改——“我不需要在
.aspx
页面上有
datagrid
。在
运行时创建的
datagrid
,即
.aspx.cs
也可以工作。”Ps:尽管如此,还是欢迎更正。