Hi all,
I wonder if anybody can help me. I'm generating an excel graph using C#, it has two lines with datapoints. I would like to add my own bespoke text to the points and I have got this code working.
My issue is that although I specify many times that the font size should be 10 points when I open the excel spreadsheet the datalabels are not set to be font size 10. I have tried many different permutations with no joy, I'm using MS Visual Studio 2008 Professional Edition with Excel 2010.
My code is below which loops through the series in the graph:
Excel.SeriesCollection oSeriesCollection;
oSeriesCollection = (Excel.SeriesCollection)chartPage.SeriesCollection(Type.Missing);
IEnumerator iEChartSeries = oSeriesCollection.GetEnumerator();
Excel.Series oSeries;
Excel.Point pt;
int seriesCnt = 1;
int seriesCol = 3;
int seriesRow = 11;
int seriesPointCnt = 1;
Excel.Range myrange;
while(iEChartSeries.MoveNext())
{
if (seriesCnt == 1) // target
{
seriesCol = 3;
seriesRow = 11;
myrange = xlWorkSheet.get_Range(xlWorkSheet.Cells[seriesRow, seriesCol], xlWorkSheet.Cells[seriesRow, seriesCol]);
myrange.Font.Name = "Calibri";
myrange.Font.Size = 10.0;
oSeries = (Excel.Series)(iEChartSeries.Current);
Excel.Points pts = (Excel.Points)oSeries.Points(Type.Missing);
IEnumerator iPoints = pts.GetEnumerator();
while (iPoints.MoveNext())
{
if (seriesPointCnt == 1)
{
pt = (Excel.Point)(iPoints.Current);
pt.HasDataLabel = false;
}
else
{
pt = (Excel.Point)(iPoints.Current);
pt.HasDataLabel = true;
pt.DataLabel.Position = Excel.XlDataLabelPosition.xlLabelPositionRight;
pt.DataLabel.Font.Size = 10.0;
pt.DataLabel.Font.Name = "Calibri";
pt.DataLabel.Text = myrange.Cells.get_Value(Type.Missing).ToString();
seriesCol = seriesCol + 1;
myrange = xlWorkSheet.get_Range(xlWorkSheet.Cells[seriesRow, seriesCol], xlWorkSheet.Cells[seriesRow, seriesCol]);
myrange.Font.Name = "Calibri";
myrange.Font.Size = 10.0;
}
seriesPointCnt = seriesPointCnt + 1;
}
}
else if (seriesCnt == 2) // actual
{
seriesCol = 3;
seriesRow = 10;
myrange = xlWorkSheet.get_Range(xlWorkSheet.Cells[seriesRow, seriesCol], xlWorkSheet.Cells[seriesRow, seriesCol]);
myrange.Font.Name = "Calibri";
myrange.Font.Size = 10.0;
oSeries = (Excel.Series)(iEChartSeries.Current);
Excel.Points pts = (Excel.Points)oSeries.Points(Type.Missing);
IEnumerator iPoints = pts.GetEnumerator();
while (iPoints.MoveNext())
{
if (seriesPointCnt == 1)
{
pt = (Excel.Point)(iPoints.Current);
pt.HasDataLabel = false;
}
else
{
if (myrange.Value2==null)
{
pt = (Excel.Point)(iPoints.Current);
pt.HasDataLabel = false;
}
else
{
pt = (Excel.Point)(iPoints.Current);
pt.HasDataLabel = true;
pt.DataLabel.Position = Excel.XlDataLabelPosition.xlLabelPositionRight;
pt.DataLabel.Font.Size.Equals(10);
pt.DataLabel.Font.Name = "Calibri";
pt.DataLabel.Text = myrange.Cells.get_Value(Type.Missing).ToString();
seriesCol = seriesCol + 1;
myrange = xlWorkSheet.get_Range(xlWorkSheet.Cells[seriesRow, seriesCol], xlWorkSheet.Cells[seriesRow, seriesCol]);
myrange.Font.Name = "Calibri";
myrange.Font.Size = 10.0;
}
}
seriesPointCnt = seriesPointCnt + 1;
}
}
seriesPointCnt = 1;
seriesCnt = seriesCnt + 1;
}
If anyone can give me some pointers I would be very grateful.
Thanks,
Graham
Loading
Karthik AgarwalPosted Nov 25, 2011, 7:25 AM
There are two ways you can avoid this problem:
1. Set the font what ever you want in the excel sheet itself which will make things pretty simple.
2. Read the excel sheet first then over write the excel sheet font with what ever the font you want
It's pretty hard to point out in your code as i can't experience your problem.
Try these things and get back if the problem still exists.
Graham CockramPosted Nov 25, 2011, 6:04 AM
Yes, I set the font size in C# and then when I open the Excel spreadsheet and goto the tab where the graph is the datalabels associated with the points are at a different font size. Although, there are occasions where the font set in C# does take effect on the graph, the most annoying thing about this is issue is that it is not consistent. That said I have never managed to get all the points to font size 10.
Any pointers you could give me would be most welcome.
Thanks,
Graham
Karthik AgarwalPosted Nov 25, 2011, 5:44 AM