有没有一种方法可以使用C#/ EPPlus为Excel图设置网格线选项 [英] Is there a way to set gridline options for Excel graphs using C# / EPPlus

查看:309
本文介绍了有没有一种方法可以使用C#/ EPPlus为Excel图设置网格线选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EPPlus和C#创建一个excel图形。我可以成功创建以下图形:

但是,我希望程序在运行时在两个方向上同时创建主要和次要网格线;因此该图应如下所示:

I am trying to create an excel Graph using EPPlus and C#. I can successfully create the following Graph: However, I would like the program to create both major and minor gridlines in both directions at runtime; so that the graph looks like this:

我知道我可以在Excel中调整这些设置,但我会希望能够使用我的代码设置默认网格线。我在EPPlus中找不到可以处理此问题的设置(如果有,请告知我)。我试图弄清楚如何直接自定义XML,但是我也遇到了麻烦。

I know I can adjust these settings in Excel, but I would like to be able to set the default gridlines with my code. I cannot find a setting in EPPlus that will handle this (If there is one, please make me aware of it). I am trying to figure out how to customize the XML directly but I'm having trouble with that as well.

有没有办法做到这一点?会得到任何帮助。

Is there a way I can accomplish this? Any help is appreciated.

推荐答案

由于它很容易打开,所以没有添加它有点奇怪。这是使用XML的方法(如果要设置颜色和大小,则必须更多地使用XML进行操作,但这将使您入门):

It is a bit strange that was not added since it is fairly easy to turn on. Here is how to do it with XML (if you want to set color and size you have to get more into the XML manipulation but this will get you started):

using (var pck = new ExcelPackage(fileInfo))
{
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets.Add("Sheet1");
    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)
        {
            if (valAxisNode.SelectSingleNode("c:majorGridlines", nsm) == null)
                valAxisNode.AppendChild(chartXml.CreateNode(XmlNodeType.Element, "c:majorGridlines", nsuri));
            if (valAxisNode.SelectSingleNode("c:minorGridlines", nsm) == null)
                valAxisNode.AppendChild(chartXml.CreateNode(XmlNodeType.Element, "c:minorGridlines", nsuri));
        }

    //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)
        {
            if (catAxisNode.SelectSingleNode("c:majorGridlines", nsm) == null)
                catAxisNode.AppendChild(chartXml.CreateNode(XmlNodeType.Element, "c:majorGridlines", nsuri));
            if (catAxisNode.SelectSingleNode("c:minorGridlines", nsm) == null)
                catAxisNode.AppendChild(chartXml.CreateNode(XmlNodeType.Element, "c:minorGridlines", nsuri));
        }

    pck.Save();
}

这篇关于有没有一种方法可以使用C#/ EPPlus为Excel图设置网格线选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆