如何使用c#格式化excel列自动调整

问题描述 投票:1回答:1

我正在将gridview导出为ex​​cel,这是我的代码 -

        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个回答
0
投票

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

您还可以查看一个更强大的库,它将在计算宽度时完成所有繁重工作(EPPlusEPPlus.Core使用Worksheet.Column(colIndex).AutoFitColumn()语法来解释。

© www.soinside.com 2019 - 2024. All rights reserved.