【问题标题】:how to make formatted excel columns autofit with c#如何使用c#使格式化的excel列自动适应
【发布时间】:2018-06-10 11:20:53
【问题描述】:

我正在将 gridview 导出到 excel,这是我的代码-

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.Charset = "";

        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=windows-1250\">\n");


        HttpContext.Current.Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office'\n" +
        "xmlns:x='urn:schemas-microsoft-com:office:excel'\n" +

        "xmlns='http://www.w3.org/TR/REC-html40'>\n" +
        "<head>\n");

在这里,我无法使列自动调整。 我很困惑并在谷歌上搜索并尝试了许多解决方案,但问题仍然存在。 任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: c# asp.net excel gridview export-to-excel


    【解决方案1】:

    不幸的是,OpenXML 不支持 AutoFit 属性,它在 Excel 内部通过计算最长字段的宽度并将该宽度与列一起存储来处理。

    您还可以查看一个更强大的库,它可以为您计算宽度(EPPlusEPPlus.Core 使用 Worksheet.Column(colIndex).AutoFitColumn() 语法。

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      相关资源
      最近更新 更多