This project has moved. For the latest updates, please go here.

Can't recreate Charts

Jan 9, 2015 at 6:20 PM
Edited Jan 10, 2015 at 1:34 PM
Hi,
I have problem with the charts, if my xlsx has more than 1 sheet, i do the following:
if (excel.Workbook.Worksheets.Count == 0)
{
    excel.Workbook.Worksheets.Add("Data");
}
else
{
    int count = excel.Workbook.Worksheets.Count;
    for (int i = 0; i < count - 1; i++)
    {
        excel.Workbook.Worksheets.Delete(2);
    }
}
so I delete all sheets except one, the first (first contains the data, others one chart), and after that I recreate the deleted sheets
ExcelWorksheet ws_data = excel.Workbook.Worksheets["Data"];
ArrayList ws_graphs = new ArrayList();
ws_graphs.Add(excel.Workbook.Worksheets.Add("Graph"));
for (int i = 0; i < DataXs.Count; i++)
{
    ws_graphs.Add(excel.Workbook.Worksheets.Add(((DataX)DataXs[i]).title));
}
this part of my program is working fine, but when I want to create a chart on the recreated sheets with the following code:
for (int index = 0; index < ws_graphs.Count; index++)
{
    ExcelWorksheet ws_graph = (ExcelWorksheet)ws_graphs[index];
    ExcelChart chart = ws_graph.Drawings.AddChart(ws_graph.Name, OfficeOpenXml.Drawing.Chart.eChartType.LineMarkers);
}
after with the first run everything is going well, because there is 0 sheet, but after the firs run, during the the others (2. 3. ...) when I already have sheets in the workbook and I delete and recreate them on the
 ExcelChart chart = ws_graph.Drawings.AddChart(ws_graph.Name, OfficeOpenXml.Drawing.Chart.eChartType.LineMarkers);
row I get an exception
Cannot add part for the specified URI because it is already in the package.
   at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
   at OfficeOpenXml.Drawing.ExcelDrawings.CreateDrawingXml()
   at OfficeOpenXml.Drawing.ExcelDrawings.AddChart(String Name, eChartType ChartType, ExcelPivotTable PivotTableSource)
   at OfficeOpenXml.Drawing.ExcelDrawings.AddChart(String Name, eChartType ChartType)
How can I fix it? I hope it is understandable. :)

Thanks in advence!