首页 > 解决方案 > 在手机上打开 Excel 文件 (.xls) 时遇到问题

问题描述

我正在 Web 表单页面中创建一个 excel 文档,当我加载该页面时,系统会提示我下载一个可以在我的计算机上正常打开的 excel 文件。但是,如果我将相同的文件作为附件发送并尝试在手机上查看它,我会收到一条错误消息,指出文件格式不受支持。

这是我下面的代码..希望有人可以阐明....

我在手机上遇到的错误说:

文件读取错误。文件类型不受支持或文件已损坏。

excelling.aspx 代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="excelling.aspx.cs" Inherits="app_Files_excelling" %>

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <p runat="server" id="p1">Hello world.</p>
                <table>
            <thead>
                <tr>
                    <th>column 1 header</th>
                    <th>column 2 header</th>
                </tr>
            </thead>

            <tbody>
                <tr>
                    <td>column 1 value</td>
                    <td>column 2 value</td>
                </tr>
            </tbody>

            <tfoot>
                <tr>
                    <td>Footer 1</td>
                    <td>Footer 2</td>

                </tr>

            </tfoot>
        </table>
</body>
</html>

excelling.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class app_Files_excelling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.ContentType = "application/vnd.ms-excel";

        Response.AddHeader("Content-Disposition", "attachment;filename=NCR.xls");

    }
}

标签: c#asp.netexcel

解决方案


就像那些评论过的人一样,您需要在通过浏览器提供之前生成一个真正的 XSLX 文件。您可以使用许多库轻松实现这一目标。

ClosedXML 示例

通过 ASP.NET 提供生成的文件


推荐阅读