用 epplus 创建 excel 图表 [英] Create excel graph with epplus

查看:49
本文介绍了用 epplus 创建 excel 图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的.我有一个两列的excel表.第 1 列对图例进行了描述,例如 Category1 Category 2 等.第 2 列的数字总数为 6、4、18 等.

here is what I have. I have an excel sheet with two column. Column 1 had descriptions for legend, like Category1 Category 2 etc. Column 2 has numbers with total count like 6,4,18 etc.

<头>
类别计数
类别 16
类别 24
类别 318

我需要显示包含计数值的图表并显示每行的类别名称.我尝试了不同的值,但我无法弄清楚.这是我当前的代码

I need to display a graph with the count values and display the category names for each line. I tried different values but I cannot figure it out. Here is my current code

        ExcelChart ec = (ExcelLineChart)chartSheet.Drawings.AddChart("chart_1",      
                        eChartType.Line);
            ec.SetPosition(1, 0, 3, 0);
            ec.SetSize(800, 300);
            var ran1 = chartSheet.Cells["A4:A6"];
            var ran2 = workSheet.Cells["0:0"];

            var serie1 = (ExcelChartSerie)ec.Series.Add(ran1, ran2);
            serie1.Header = chartSheet.Cells["A3"].Value.ToString();

            ran1 = chartSheet.Cells["B4:B6"];
            var serie2 = ec.Series.Add(ran1, ran2);
            serie2.Header = chartSheet.Cells["B3"].Value.ToString();

            var xml = ec.ChartXml;
            var lst = xml.GetElementsByTagName("c:lineChart");
            foreach (System.Xml.XmlNode item in lst[0].ChildNodes) {
                if (item.Name.Equals("ser")) {
                    foreach (System.Xml.XmlNode subitem in item.ChildNodes) {
                        if (subitem.Name.Equals("c:cat")) {
                            item.RemoveChild(subitem);
                            break;
                        }
                    }
                }
            }

这给了我一行计数,但没有像我想要的那样显示类别名称.

This gives me a line with counts, but does not display the category names like I wanted.

提前感谢您的回复.

推荐答案

没关系,我摸索了一下,找到了答案

Nevermind, I fiddled around a little bit and found the answer

这是工作代码

ExcelChart chart = chartSheet.Drawings.AddChart("FindingsChart",
OfficeOpenXml.Drawing.Chart.eChartType.ColumnClustered);
chart.Title.Text = "Category Chart";
chart.SetPosition(1, 0, 3, 0);
chart.SetSize(800, 300);
var ser1 = (ExcelChartSerie)(chart.Series.Add(workSheet.Cells["B4:B6"],
workSheet.Cells["A4:A6"]));
ser1.Header = "Category";

这篇关于用 epplus 创建 excel 图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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