You are on page 1of 2

13/6/2016

c#SettingdatausedbyalinegraphusingEPPlusStackOverflow

StackOverflowisacommunityof4.7
millionprogrammers,justlikeyou,
helpingeachother.

signup

login

tour

help

JointheStackOverflowcommunityto:

Jointhemitonlytakesaminute:
Askprogramming
questions

Signup

Answerandhelp
yourpeers

Getrecognizedforyour
expertise

SettingdatausedbyalinegraphusingEPPlus
IamtryingtoaddasimplelinegraphinexcelautomaticallybyusingEPPlus.IhaveandknowtherangeofcellsthatcontainthedatathatIwanttouse.Iwantthegraphto
looklikethefollowing:

The

Before

and

After

columnsgoonforalotmorethanwhatisshown.

Iamusingthiscodetocreatethegraphandpositionit,butIamnotsurehowtosetthedatathatthegraphuses:
ExcelChartec=ws.Drawings.AddChart("LineTimeGraph",
OfficeOpenXml.Drawing.Chart.eChartType.Line);
ec.Title.Text="Benchmarks";
ec.SetPosition(_times.Count+2,0,1,0);

Itmighthavesomethingtodowith

ec.Series.Add

butIamnotsurehowtoproperlyuseit.Ifyoucouldpointmeintherightdirectionthatwouldbegreat.

c# excel epplus
askedNov10'12at10:20

3aw5TZetdf
2,425

15

32

1Answer

Hopethishelpsyou:
ExcelChartec=ws.Drawings.AddChart("LineTimeGraph",
OfficeOpenXml.Drawing.Chart.eChartType.Line);
ec.Title.Text="Benchmarks";
ec.SetPosition(_times.Count+2,0,1,0);
ec.SetSize(800,600);
varran1=sheet.Cells["A3:A10"];
varran2=sheet.Cells["G3:G10"];//labelrangeifthereis.Otherwise,letselectblank
rangetheneditXMLdatalatertoremove'c:cat'tags(bellowexample)
varserie1=ec.Series.Add(ran1,ran2);
//useserie1variabletoformatandsoon
//setserie1.HeaderAddresstoA2also
varran1=sheet.Cells["B3:B10"];
varserie2=ec.Series.Add(ran1,ran2);
//useserie2variabletoformatandsoon

AnotherfullexampleI'vejusttested:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingOfficeOpenXml;
usingSystem.IO;
usingOfficeOpenXml.Drawing.Chart;
namespaceTestExcelEPPluss
{
classProgram
{
staticvoidMain(string[]args)
{
ExcelPackagepackage=newExcelPackage();
varsheet=package.Workbook.Worksheets.Add("TestingGraph");
Randomr=newRandom();
varcell=sheet.Cells["A1"];
cell.Value="Before";
cell=sheet.Cells["B1"];
cell.Value="After";
for(inti=0;i<100;i++)
{
cell=sheet.Cells[i+2,1];
cell.Value=r.Next(300,500);
cell=sheet.Cells[i+2,2];
cell.Value=r.Next(300,500);
}
ExcelChartec=(ExcelLineChart)sheet.Drawings.AddChart("chart_1",
eChartType.Line);
ec.SetPosition(1,0,3,0);
ec.SetSize(800,300);
//ec.Legend.Add();
varran1=sheet.Cells["A2:A101"];
varran2=sheet.Cells["0:0"];
varserie1=(ExcelLineChartSerie)ec.Series.Add(ran1,ran2);
serie1.Header=sheet.Cells["A1"].Value.ToString();
ran1=sheet.Cells["B2:B101"];
varserie2=ec.Series.Add(ran1,ran2);
http://stackoverflow.com/questions/13321179/settingdatausedbyalinegraphusingepplus

1/2

13/6/2016

c#SettingdatausedbyalinegraphusingEPPlusStackOverflow

serie2.Header=sheet.Cells["B1"].Value.ToString();
varxml=ec.ChartXml;
varlst=xml.GetElementsByTagName("c:lineChart");
foreach(System.Xml.XmlNodeiteminlst[0].ChildNodes)
{
if(item.Name.Equals("ser"))
{
foreach(System.Xml.XmlNodesubiteminitem.ChildNodes)
{
if(subitem.Name.Equals("c:cat"))
{
item.RemoveChild(subitem);
break;
}
}
}
}
stringpath=@"C:\test1.xlsx";
File.WriteAllBytes(path,package.GetAsByteArray());
package.Dispose();
Console.WriteLine("DonePath:{0}",path);
Console.ReadLine();
}
}
}
c:cat isthecategoryfor ser series,letremove c:cat tagsfromchartsxml,thenyourchart
willuse 1,2,3,4,5,... asdefaultforthecategory(xaxishere).
editedNov13'12at8:30

answeredNov12'12at7:07

Han
1,513

11

24

Thanks,Iwilltrythissoon.BylabelrangedoyoumeanifIwantthegraphtohavespecificvaluesonthex
andyaxis,suchas 123456 ? 3aw5TZetdf Nov12'12at7:20
It'sXaxisvaluesrange.Itcanbenull,thenXvalueswillbe 123456... likeinyourillustrating

picture.HanNov12'12at7:24
IhavetesteditbutwhenIcall ec.Series.Add(ran1,null) itthrowsa NullReferenceException .Iam
assumingitisbecauseofthe null passed.Areyousureyoucanpassnull? 3aw5TZetdf Nov12'12at

7:56
Ijustthoughtitworks.Lettrytopassablankrange,otherwiseyoushouldmakeXvaluesrange

correspondingly.Sorryforthemistake.HanNov12'12at8:33
Noproblem,Iwilltryitsoonortomorrow(itslatehere). 3aw5TZetdf Nov12'12at8:38

http://stackoverflow.com/questions/13321179/settingdatausedbyalinegraphusingepplus

2/2

You might also like