c# 中没有网格线的 epplus 图表(它是一个网络应用程序)

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

Graph Without GridLines我正在尝试使用 epplus 和 c# 在 excel 文件中创建图表。 我能够创建图形,我正在尝试删除图形中的网格线,如图所示。我在 windows 应用程序中找到了解决方案,但我想在 Web 应用程序中这样做,下面是我用来生成图形的代码。

ExcelRange erLossesRangeMacInv = worksheet.Cells["G5:G10"];
var chartOEE=(ExcelBarChart)worksheetGraph.Drawings.AddChart("barChartOEE", eChartType.ColumnClustered);
chartOEE.SetSize(300, 300);//Setting size of graph
chartOEE.SetPosition(10, 10); // position of graph in excel
chartOEE.Title.Text = "OEE";
ExcelRange erLossesRangeOEE = worksheet.Cells["M5:M10"]; 
chartOEE.Style = eChartStyle.Style10;
chartOEE.Legend.Remove();
chartOEE.DataLabel.ShowValue = true;
chartOEE.YAxis.MaxValue = 100;
chartOEE.Series.Add(erLossesRangeOEE, erLossesRangeMacInv);

我找到了在 windows 表单中工作的代码。

chartOEE.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;

如何使用 epplus 和 c# 在 "Web Application" 中实现。

更新

我创建了一个允许删除网格线的 PR,该 PR 已被接受。应该会在下一个版本中(无论何时)所以请注意它:

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

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

可以这样使用:

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

原创

奇怪的是他们没有将其添加为 EPPlus 的选项,因为它只需要基本的 xml 操作。就 EPPlus 而言,网络或桌面应该没有区别,您只需要删除 xml 引用。应该这样做:

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();
}

这给出了这个: