在vb.net中将数据导出到excel
我无法将数据导出到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用我强烈推荐的 ExcelLibrary,例如 EPPlus(GPL)。
然后,从数据表创建 Excel 文件并将其写入响应就如此简单:
这是另一个示例: 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:
Here is another example: http://epplus.codeplex.com/wikipage?title=WebapplicationExample
一旦您在此处有了数据表
dt
,您就应该执行此操作(C# - 刚刚从 Internet 复制),这里是类
ExportXMLCSV
的方法ToCSV
(C# - 刚刚从 Internet 复制)所有内容都从这里复制: 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)and here the method
ToCSV
of the classExportXMLCSV
(C# - just copied from the Internet)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 ;-)