简体   繁体   中英

Export Excel to Asp.net that the Tablet will not open

In my function I have the following instructions:

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]()

Where grid is a datagrid where I post the data to export.

On the PC it's all right, but when I try to export data to a tablet (android or apple), the file does not open.

I ask: you can do it open even on the mobile?

Many thanks to the response.

One likely explanation for your issue is that your code does not actually create a real .xls file. Instead, it creates a text file containing an HTML table and gives the file an .xls extension:

ScreenShot.png

Excel is able to open "fake" .xls files like this (another example would be .csv files that have been renamed to .xls), but the file viewers/editors on other platforms may not be as accommodating.

If you really want to produce an .xls file then you should use a library that creates an actual .xls file. There are some suggestions in another question here:

Create Excel (.XLS and .XLSX) file from C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM