Excel 2008 无法解析 HTML

发布于 2024-08-23 18:11:06 字数 2121 浏览 4 评论 0原文

我需要将 gridview 导出到 Excel,我将 gridview 的返回 html 代码放入 HtmlTextWriter 并将其放入响应中。

结果文件在 Excel 中工作正常,Excel 可以解析 html 并且结果可读,在 Excel 2003 和 2007 上工作完美,但在某些装有 Excel 2008 (MACOS) 的机器中,Excel 仅显示原始 html 代码,无法处理此代码html 代码。

有什么想法来配置Excel吗?

这是要转换的代码:

public static void ToExcel(GridView gridView, string fileName)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.Buffer = true;

    fileName = fileName.Replace(".xls", string.Empty) + ".xls";

    response.AddHeader("content-disposition",
                       "attachment;filename=" + fileName);
    response.Charset = "";
    response.ContentEncoding = Encoding.Unicode;
    response.BinaryWrite(Encoding.Unicode.GetPreamble());
    response.ContentType = MimeTypes.GetContentType(fileName);

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gridView.AllowPaging = false;
    //gridView.DataBind();

    //Change the Header Row back to white color
    gridView.HeaderRow.Style.Add("background-color", "#FFFFFF");

    //Apply style to Individual Cells
    for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++)
    {
        gridView.HeaderRow.Cells[i].Style.Add("background-color", "yellow");    
    }

    for (int i = 0; i < gridView.Rows.Count; i++)
    {
        GridViewRow row = gridView.Rows[i];

        //Change Color back to white
        row.BackColor = System.Drawing.Color.White;

        //Apply text style to each Row
        row.Attributes.Add("class", "textmode");

        //Apply style to Individual Cells of Alternating Row
        if (i % 2 != 0)
        {
            for (int j = 0; j < row.Cells.Count; j++)
            {
                row.Cells[j].Style.Add("background-color", "#C2D69B");
            }
        }
    }

    gridView.RenderControl(hw);

    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    response.Write(style);
    response.Output.Write(sw.ToString());
    response.Flush();
    response.End(); 
}

I need to export a gridview to excel, I put the return html code from the gridview to a HtmlTextWriter and put this into the response.

The result file work fine in excel, excel can parse the html and the result is readable, work perfect on excel 2003 and 2007, but in some machines with Excel 2008 (MACOS) excel shows only the raw html code and can't process this html code.

Any idea to configure excel?

This is the code to convert:

public static void ToExcel(GridView gridView, string fileName)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.Buffer = true;

    fileName = fileName.Replace(".xls", string.Empty) + ".xls";

    response.AddHeader("content-disposition",
                       "attachment;filename=" + fileName);
    response.Charset = "";
    response.ContentEncoding = Encoding.Unicode;
    response.BinaryWrite(Encoding.Unicode.GetPreamble());
    response.ContentType = MimeTypes.GetContentType(fileName);

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gridView.AllowPaging = false;
    //gridView.DataBind();

    //Change the Header Row back to white color
    gridView.HeaderRow.Style.Add("background-color", "#FFFFFF");

    //Apply style to Individual Cells
    for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++)
    {
        gridView.HeaderRow.Cells[i].Style.Add("background-color", "yellow");    
    }

    for (int i = 0; i < gridView.Rows.Count; i++)
    {
        GridViewRow row = gridView.Rows[i];

        //Change Color back to white
        row.BackColor = System.Drawing.Color.White;

        //Apply text style to each Row
        row.Attributes.Add("class", "textmode");

        //Apply style to Individual Cells of Alternating Row
        if (i % 2 != 0)
        {
            for (int j = 0; j < row.Cells.Count; j++)
            {
                row.Cells[j].Style.Add("background-color", "#C2D69B");
            }
        }
    }

    gridView.RenderControl(hw);

    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    response.Write(style);
    response.Output.Write(sw.ToString());
    response.Flush();
    response.End(); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-08-30 18:11:06

一般来说,依靠 Excel 解析你的 Html 是非常脆弱的。正如您所发现的,Mac 版本的 Excel 可能会出现问题。相反,您应该考虑使用电子表格 XML 或 NPOI 等引擎来构建实际的 Excel 文件。

无论是 Html 文档还是 Xml 文档,Excel 都会在从 Web 下载时向用户发出警告,表明该文件不是预期的同一类型(例如,您有一个 Html 文件声称是 Excel 文件,基于内容类型。)。如果您构建实际的 Excel 文件,则不会遇到兼容性问题,用户也不会收到此警告。

电子表格 XML

NPOI

In general, relying on Excel parsing your Html is very fragile. As you have discovered, the Mac version of Excel can have troubles with it. Instead, you should consider using Spreadsheet XML or an engine like NPOI to build an actual Excel file.

With either an Html document or an Xml document, Excel will throw a warning to the user on download from the web that the file is not of the same type expected (e.g., you have an Html file claiming to be an Excel file based on the content-type.). If you build an actual Excel file, you won't have the compatibility problems nor will users get this warning.

Spreadsheet XML

NPOI

奶茶白久 2024-08-30 18:11:06

嗯,你可以做类似的事情...

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=report.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        var stringWrite = new System.IO.StringWriter();
        var htmlWrite = new HtmlTextWriter(stringWrite);
        this.gridView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();

Hmm, you could do something like...

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=report.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        var stringWrite = new System.IO.StringWriter();
        var htmlWrite = new HtmlTextWriter(stringWrite);
        this.gridView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
七分※倦醒 2024-08-30 18:11:06

Excel 2008 for Mac 没有 VBA。我不知道如果没有 VBA 环境,C# 插件(我猜就是这个)如何运行。我知道对于我的 VBA 文件和加载项没有什么可做的。

Excel 2008 for Mac does not have VBA. I don't know how C# add-ins (I presume that is what this is) run if there is no VBA environment. I know for my VBA files and add-ins there is nothing that can be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文