This project has moved and is read-only. For the latest updates, please go here.

Can't recreate Charts

Jan 9, 2015 at 7:20 PM
Edited Jan 10, 2015 at 2:34 PM
I have problem with the charts, if my xlsx has more than 1 sheet, i do the following:
if (excel.Workbook.Worksheets.Count == 0)
    int count = excel.Workbook.Worksheets.Count;
    for (int i = 0; i < count - 1; i++)
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();
for (int i = 0; i < DataXs.Count; i++)
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!