将Excel导出到Asp.net,平板电脑不会打开

在我的function,我有以下指示:

grid.DataSource = dt grid.DataBind() Response.Clear() Response.AddHeader("content-disposition", "attachment; filename=Soste.xls") Response.AddHeader("Content-Type", "application/Excel") Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Response.Charset = "" Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter Dim htmlWrite As New HtmlTextWriter(stringWrite) grid.RenderControl(htmlWrite) Dim html As String = stringWrite.ToString() Dim pattern As String = "(\p{Sc})?" Dim rgx As New System.Text.RegularExpressions.Regex(pattern) html = rgx.Replace(html, "") Response.Write(html) HttpContext.Current.Response.[End]() 

网格是一个数据网格,我发布要导出的数据。

在个人电脑上它是好的,但是当我尝试导出数据到平板电脑(android或苹果),该文件无法打开。

我问:你甚至可以在手机上打开吗?

非常感谢答复。

您的问题的一个可能的解释是您的代码实际上并不创build一个真正的.xls文件。 相反,它会创build一个包含HTML表格的文本文件,并为该文件提供一个.xls扩展名:

ScreenShot.png

Excel可以像这样打开“假”.xls文件(另一个例子是重命名为.xls的.csv文件),但其他平台上的文件查看器/编辑器可能不那么容易。

如果你真的想生成一个.xls文件,那么你应该使用一个库来创build一个实际的.xls文件。 在这里有另一个问题的一些build议:

从C#创buildExcel(.XLS和.XLSX)文件