开发者

Can't copy a Worksheet that contains a Chart using EPPLUS

开发者 https://www.devze.com 2023-03-18 22:40 出处:网络
I\'m trying to copy a worksheet that contains a chart, but when I try to do it, my code fires a \"package relationship with specified id does not exist for the source part\" exception, what are possib

I'm trying to copy a worksheet that contains a chart, but when I try to do it, my code fires a "package relationship with specified id does not exist for the source part" exception, what are possible reasons for this? *If I remove the chart from the template, everything works fine.

Here's my code:

  public void GenerateFromTemplate(DirectoryInfo outputPath, FileInfo templateFile,
        string newFileName, List<Dictionary<string, string>> cellDataList)
    {
        try
        {
            using (ExcelPackage p = new ExcelPackage(templateFile, true))
            {
                bool first = true;
                foreach (Dictionary<string, string> cellData in cellDataList) {
                    Logger.LogInfo("Adding new Sheet...");
                    ExcelWorksheet currentWorkSheet;
                    if (first)
                    {
                        currentWorkSheet 开发者_开发百科= p.Workbook.Worksheets[1];
                        first = false;
                    }
                    else {
                        currentWorkSheet = p.Workbook.Worksheets.Copy("Ticker", "Ticker" + p.Workbook.Worksheets.Count);
                    }

                    foreach (KeyValuePair<string, string> cell in cellData)
                    {
                        Logger.LogInfo(cell.Key + "cell value set to" + cell.Value);
                        currentWorkSheet.Cells[cell.Key].Value = cell.Value;
                    }
                }
                Byte[] bin = p.GetAsByteArray();

                string file = outputPath + newFileName;
                Logger.LogInfo("Writing Excel File to " + file);
                File.WriteAllBytes(file, bin);
                Logger.LogInfo("Writing Done");
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex);
        }
    }

Regards!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号