David Voltrubec. We had a similar problem where we wanted to draw graph for 2 tables in the same sheet one below the other. Flexcel doesn't support graph drawing dynamically as it doesn't has a property to set the range for a graph. What I did was
1. Write the table in XLSX file and save it.
2. Open it using a 3rd party Library (MIT license) which uses Open XML SDK and specify the range and type of graph to be drawn.
This way I could generate graphs on dynamic tables as well.
Below is a code extract where I used SpreadSheetLight library.
List<DrawExcelChart.DynamicExcelChart>
is generic list to hold the startRange and GetColumnAlphabetAndNumber
was to convert Column/Rows into Excel Alphanumeric Cell references.
private void DrawChartInExcelUsingSpreadSheetLight(string astrFileName, string astrFilePath, out string astrAlteredFileName)
{
List<DrawExcelChart.DynamicExcelChart> ActualCellStartEndRanges;
string finalFilePath = string.Empty;
int WorkSheetsCount = 0;
string newFilePath = string.Empty;
double ChartYpositionStart = 0;
double ChartYpositionEnd = 0;
string endPointAlphabet = string.Empty;
string endPointNumber = string.Empty;
string startPointAlphabet = string.Empty;
string startPointNumber = string.Empty;
DrawExcelChart myExcelChartObject = new DrawExcelChart();
SLChart SSLChartObject = null;
SLDocument SSLDocumentObject = null;
try
{
DecideCellStartEndRanges(out ActualCellStartEndRanges);
finalFilePath = astrFilePath + "\\" + astrFileName;
newFilePath = astrFilePath + "\\" + astrFileName.Replace("haschart", "");
using (SSLDocumentObject = new SLDocument(finalFilePath))
{
WorkSheetsCount = SSLDocumentObject.GetSheetNames().Count;
for (int workSheetCount = 0; workSheetCount < WorkSheetsCount; workSheetCount++)
{
SSLDocumentObject.SelectWorksheet(SSLDocumentObject.GetSheetNames()[workSheetCount]);
myExcelChartObject.GetColumnAlphabetAndNumber(ActualCellStartEndRanges[workSheetCount].FirstChart.startRange, out startPointAlphabet, out startPointNumber);
myExcelChartObject.GetColumnAlphabetAndNumber(ActualCellStartEndRanges[workSheetCount].FirstChart.endRange, out endPointAlphabet, out endPointNumber);
ChartYpositionStart = Convert.ToInt32(endPointNumber) + 1;
ChartYpositionEnd = Convert.ToInt32(endPointNumber) + 2;
SSLDocumentObject.SetRowHeight(Convert.ToInt32(startPointNumber), Convert.ToInt32(endPointNumber) + 1, 15);
SSLChartObject = SSLDocumentObject.CreateChart(ActualCellStartEndRanges[workSheetCount].FirstChart.startRange, ActualCellStartEndRanges[workSheetCount].FirstChart.endRange);
SSLChartObject.SetChartType(SpreadsheetLight.Charts.SLLineChartType.LineWithMarkers);
SSLChartObject.SetChartPosition(ChartYpositionStart, 1, ChartYpositionEnd, 14);
SSLDocumentObject.SetRowHeight(Convert.ToInt32(ChartYpositionEnd), 180);
SSLChartObject.SetChartStyle(SpreadsheetLight.Charts.SLChartStyle.Style2);
SSLChartObject.Border.SetAutomaticColor();
SSLChartObject.Legend.LegendPosition = DocumentFormat.OpenXml.Drawing.Charts.LegendPositionValues.Right;
SSLDocumentObject.InsertChart(SSLChartObject);
}
SSLDocumentObject.SelectWorksheet(SSLDocumentObject.GetSheetNames()[0]);
SSLDocumentObject.SaveAs(newFilePath);
}
astrAlteredFileName = astrFileName.Replace("haschart", "");
}
catch (Exception obj_Except)
{
astrAlteredFileName = string.Empty;
}
finally
{
ActualCellStartEndRanges = null;
finalFilePath = string.Empty;
WorkSheetsCount = 0;
newFilePath = string.Empty;
ChartYpositionStart = 0;
ChartYpositionEnd = 0;
endPointAlphabet = string.Empty;
endPointNumber = string.Empty;
startPointAlphabet = string.Empty;
startPointNumber = string.Empty;
myExcelChartObject = null;
SSLChartObject = null;
SSLDocumentObject = null;
}
}