【问题标题】:HTML to Excel Export with errorHTML 到 Excel 导出错误
【发布时间】:2018-02-15 08:34:04
【问题描述】:

但是,我正在导出创建的 HTML 并将其保存到 Excel 文件中;我得到错误 "Missing file ......css". 我试过注释掉aspx中引用样式表的区域,但我仍然遇到同样的问题。我什至将所有样式从 css 移到 aspx 文件中,但没有任何改进。

【问题讨论】:

标签: c# html css asp.net


【解决方案1】:

不确定这是否对您有帮助,但

我还注意到 Excel 在 CSS 和 HTML 中的导入方式略有不同,具体取决于版本以及它在 Windows 还是 OS X 中也是如此。

string attachment = "attachment; filename=exported_file.xls";
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        ctl.RenderControl(htextw);
        HttpContext.Current.Response.Write(stw.ToString());
        FileInfo fi = new FileInfo(Server.MapPath("../Content/Styles/StyleSheet.css"));
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        StreamReader sr = fi.OpenText();
        while (sr.Peek() >= 0)
        {
            sb.Append(sr.ReadLine());
        }
        sr.Close();
        Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>");
        stw = null;
        htextw = null;
        Response.Flush();
        Response.End();

【讨论】:

  • 让我试一试
  • 正在使用的对象是一个数据表来保存数据。这有关系吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多