5
votes

I want to update (add another sheet and add chart) an existing xlsx file by using epplus excel package. However, I got an error in following line

var pieChart = worksheet.Drawings.AddChart("Chart1", OfficeOpenXml.Drawing.Chart.eChartType.Pie);

Error : An unhandled exception of type 'System.InvalidOperationException' occurred in EPPlus.dll Additional information: Part already exist

Can anyone help me? Thank you in advance.

using (ExcelPackage pck = new ExcelPackage())
            {
       using (FileStream stream = new FileStream("Report.xlsx", FileMode.Open))
                {
                    pck.Load(stream);

                    ExcelWorksheet worksheet = pck.Workbook.Worksheets.Add("1");

                    var data = new List<KeyValuePair<string, int>>
    {
        new KeyValuePair<string, int>("Group A", 44613),
        new KeyValuePair<string, int>("Group B", 36432),
        new KeyValuePair<string, int>("Group C", 6324),
        new KeyValuePair<string, int>("Group A", 6745),
        new KeyValuePair<string, int>("Group B", 23434),
        new KeyValuePair<string, int>("Group C", 5123),
        new KeyValuePair<string, int>("Group A", 34545),
        new KeyValuePair<string, int>("Group B", 5472),
        new KeyValuePair<string, int>("Group C", 45637),
        new KeyValuePair<string, int>("Group A", 37840),
        new KeyValuePair<string, int>("Group B", 20827),
        new KeyValuePair<string, int>("Group C", 4548),
    };

                    //Fill the table
                    var startCell = worksheet.Cells[1, 1];
                    startCell.Offset(0, 0).Value = "Group Name";
                    startCell.Offset(0, 1).Value = "Value";

                    for (var i = 0; i < data.Count(); i++)
                    {
                        startCell.Offset(i + 1, 0).Value = data[i].Key;
                        startCell.Offset(i + 1, 1).Value = data[i].Value;
                    }

                    //Add the chart to the sheet
                    var pieChart = worksheet.Drawings.AddChart("Chart1", OfficeOpenXml.Drawing.Chart.eChartType.Pie);
                    pieChart.SetPosition(data.Count + 1, 0, 0, 0);
                    pieChart.Title.Text = "Test Chart";
                    pieChart.Title.Font.Bold = true;
                    pieChart.Title.Font.Size = 12;



                    pck.Save();
                }
2
Is it Chart X or Chart1? Is there already Chart1 (or Chart X whatever you are using) present on other sheets of this workbook? - Techie
Yes, that should be Chart1, copy-paste mistake.Already edited. - Svartalfar
Rename the xlsx to .zip extension and open it with 7zip or some other tool. Does you see a xl\drawings folder? If so, look at the xml files and see if there is a "chart1" listed in there already. - Ernie S
Hey, Are you sure var pieChart = worksheet... is where you get the exception? - AsafSavich

2 Answers

1
votes

please rename chart 1 to another name because probably you already have created a chart with the same name in your excel file somewhere else in your code

0
votes

try this (will work 100%) :

using (ExcelPackage packageNew = new ExcelPackage())
{
ExcelWorksheet worksheetNew = packageNew.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].Value = "what ever";
.
.
.
Byte[] bin =    package.GetAsByteArray();
                string file = newFile.FullName; ;
                File.WriteAllBytes(file, bin);

                //These lines will open it in Excel
                ProcessStartInfo pi = new ProcessStartInfo(file);
                Process.Start(pi);
}