我知道这已经在此处和网络上的一千个其他站点上得到了回答,但是我尝试过的每个站点都无法按我的意愿工作。我只想导出一个数据表,而不是从中运行代码的页面,并且我想这样做而无需下载第 3 方 dll。我尝试过的每一段代码最终都会导出我正在运行它的页面。这是我正在使用的当前迭代......
Private Sub ExporttoExcel(table As DataTable)
Dim attachment As String = "attachment; filename=file.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dc As DataColumn In table.Columns
Response.Write(tab + dc.ColumnName)
tab = vbTab
Next
Response.Write(vbLf)
Dim i As Integer
For Each dr As DataRow In table.Rows
tab = ""
For i = 0 To table.Columns.Count - 1
Response.Write(tab & dr(i).ToString())
tab = vbTab
Next
Response.Write(vbLf)
Next
Response.End()
End Sub
谁能解释为什么这不起作用或如何仅将数据表导出到 Excel 中?