EPPlus部分已存在。

5

我想使用epplus excel包更新(添加另一个工作表和图表)现有的xlsx文件。然而,在以下行中出现错误:

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

错误:类型为'System.InvalidOperationException'的未经处理异常发生在EPPlus.dll中。其他信息:部件已存在

有人能帮我吗?提前感谢。

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();
                }

1
Chart X 还是 Chart1?这个工作簿的其他工作表上是否已经存在 Chart1(或者你使用的任何 Chart X)? - Techie
是的,应该是Chart1,复制粘贴错误。已经编辑过了。 - Svartalfar
将xlsx重命名为.zip扩展名,并使用7zip或其他工具打开它。您是否看到了“xl \ drawings”文件夹?如果是,请查看xml文件并查看其中是否已经列出了“chart1”。 - Ernie S
嘿,你确定 var pieChart = worksheet... 是引发异常的地方吗? - AsafSavich
2个回答

1

请将图表1重命名为其他名称,因为您可能已经在Excel文件中的其他地方或代码中创建了相同名称的图表。


0

尝试这个(百分之百有效):

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);
}

答案与问题无关。问题是要添加一个与工作簿中已经存在的图表同名的图表,与保存电子表格无关。 - Pete

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接