epplus charts without gridlines in c# ( Its a web Application)

流过昼夜 提交于 2019-12-05 21:27:19

UPDATE

I created a PR which has been accepted that allow removing grid lines. Should will be in the next release (whenever that is) so look out for it:

https://epplus.codeplex.com/SourceControl/network/forks/aesalazar/Epplus/contribution/9025

https://github.com/JanKallman/EPPlus/blob/master/EPPlus/Drawing/Chart/ExcelChartAxis.cs#L895

Which can be used like this:

axis.RemoveGridlines(); //Removes major and minor
axis.RemoveGridlines(true, true); //Can choose major or minor with bools.

ORIGINAL

Its strange that they have not added that as an option to EPPlus since it only requires basic xml manipulation. Web or desktop should make no difference as far as EPPlus goes, you simply need to remove the xml references. This should do it:

using (var pck = new ExcelPackage(fileInfo))
{
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets.Add("Sheet1");

    //datatable is just some made up Table object
    worksheet.Cells.LoadFromDataTable(datatable, true);

    var chart = worksheet.Drawings.AddChart("chart test", eChartType.XYScatter);
    var series = chart.Series.Add(worksheet.Cells["B2:B11"], worksheet.Cells["A2:A11"]);

    //Get reference to the worksheet xml for proper namespace
    var chartXml = chart.ChartXml;
    var nsuri = chartXml.DocumentElement.NamespaceURI;
    var nsm = new XmlNamespaceManager(chartXml.NameTable);
    nsm.AddNamespace("c", nsuri);

    //XY Scatter plots have 2 value axis and no category
    var valAxisNodes = chartXml.SelectNodes("c:chartSpace/c:chart/c:plotArea/c:valAx", nsm);
    if (valAxisNodes != null && valAxisNodes.Count > 0)
        foreach (XmlNode valAxisNode in valAxisNodes)
        {
            var major = valAxisNode.SelectSingleNode("c:majorGridlines", nsm);
            if (major != null)
                valAxisNode.RemoveChild(major);

            var minor = valAxisNode.SelectSingleNode("c:minorGridlines", nsm);
            if (minor != null)
                valAxisNode.RemoveChild(minor);
        }

    //Other charts can have a category axis
    var catAxisNodes = chartXml.SelectNodes("c:chartSpace/c:chart/c:plotArea/c:catAx", nsm);
    if (catAxisNodes != null && catAxisNodes.Count > 0)
        foreach (XmlNode catAxisNode in catAxisNodes)
        {
            var major = catAxisNode.SelectSingleNode("c:majorGridlines", nsm);
            if (major != null)
                catAxisNode.RemoveChild(major);

            var minor = catAxisNode.SelectSingleNode("c:minorGridlines", nsm);
            if (minor != null)
                catAxisNode.RemoveChild(minor);
        }

    pck.Save();
}

Which gives this:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!