为什么我的XLSM、XLSX用EPPLUS保存后还要修复?

Why my XLSM, XLSX have to be repaired after saving with EPPLUS?

用 EPPLUS 保存 XLSM 或 XLSX 文件后,打开它时,我收到此消息:

We found a problem with some content in 'YourExcelFile.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes

yes后会告诉我:

Removed Records: Named range from /xl/workbook.xml part (Workbook) Error

如何防止这些错误弹出?

问题出在此处找到的命名范围:

在我的代码中,我将所有单元格从公式转换为其值,因此不需要命名范围:

foreach (var cell in worksheet.Cells.Where(cell => cell.Formula != null))
{
    cell.Value = cell.Text;
}

然后我删除了所有名称范围。这解决了我的问题(我再也没有弹出窗口了!):

int nameCount = xlPackage.Workbook.Names.Count();
for (int i = 0;i < nameCount;)
{
    xlPackage.Workbook.Names.Remove(xlPackage.Workbook.Names[i].Name.ToString());
    nameCount--;
}

我当然希望这对某人有所帮助!我找了好几天!

如果有更高效的方法,欢迎在下方留言。