我正在尝试复制包含图表的工作表,但是当我尝试执行此操作时,我的代码会触发“源部分不存在指定ID的包关系”异常,可能的原因是什么? *如果我从模板中删除图表,一切正常。
这是我的代码:
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);
}
}
问候!