用epplus创建excel图 [英] Create excel graph with epplus
本文介绍了用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.
类别计数
Category1 6
Category1 6
Category2 4
Category2 4
Category3 18
Category3 18
我需要显示带有计数值的图表,并显示每一行的类别名称。
我尝试了不同的值,但我不能弄清楚。
这是我当前的代码
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;
}
}
}
}
预先感谢任何回应。
推荐答案
不知不觉,我花了一点时间,发现了答案
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屋!
查看全文