Create excel graph with epplus

╄→гoц情女王★ 提交于 2019-11-30 05:40:59

问题


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.

Category Count

Category1 6

Category2 4

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

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

Thanks in advance for any response.


回答1:


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

Here is the working code

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";


来源:https://stackoverflow.com/questions/29976752/create-excel-graph-with-epplus

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