[英]Export Datatable to Excel File (.XLSX)
I use this code to export Datatable to excel file (.xlsx) 我使用此代码将数据表导出到Excel文件(.xlsx)
Dim attachment As String = "attachment; filename=Excel.xlsx"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dc As DataColumn In dt.Columns
Response.Write(tab + dc.ColumnName)
tab = vbTab
Next
Response.Write(vbLf)
Dim i As Integer
For Each dr As DataRow In dt.Rows
tab = ""
For i = 0 To dt.Columns.Count - 1
Response.Write(tab & dr(i).ToString())
tab = vbTab
Next
Response.Write(vbLf)
Next
Response.End()
When I download the file I get This message : 当我下载文件时,我收到此消息:
"Excel cannot open the file 'Excel.xlsx' because the file format or file extension is not valid " “ Excel无法打开文件'Excel.xlsx',因为文件格式或文件扩展名无效”
I use Excel 2010 我使用Excel 2010
Any Ideas why ?! 任何想法为什么?!
Your are creating a TSV - tab separated value. 您正在创建TSV-制表符分隔的值。 Instead .xlsx try using .tsv
而是.xlsx尝试使用.tsv
如果要导出为excel,也许可以通过第三方lib NPOI来制作excel对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.