加载 Excel 文件时收到警告

可能的使用场景

有时,用户尝试加载有些损坏但可加载的工作簿。在这种情况下,Aspose.Cells 在加载工作簿时会引发警告。您可以通过实施**IWarningCallback**界面与设置**LoadOptions.WarningCallback**财产。

加载 Excel 文件时收到警告

以下示例代码解释了如何在加载 excel 文件时获取警告。代码加载示例 excel 文件哪个抛出**重复定义名称**加载警告。这个警告然后被捕获**IWarningCallback.Warning()**在控制台上打印警告消息的方法。然后代码将工作簿另存为输出excel文件.如果您在 Microsoft Excel 中打开示例 excel 文件,它也会向您显示此屏幕截图所示的警告。另请检查下面给出的代码的控制台输出以获得更多理解。

待办事项:图片_替代_文本

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Implement IWarningCallback interface to catch warnings while loading workbook
private class WarningCallback : IWarningCallback
{
public void Warning(WarningInfo warningInfo)
{
if (warningInfo.WarningType == WarningType.DuplicateDefinedName)
{
Console.WriteLine("Duplicate Defined Name Warning: " + warningInfo.Description);
}
}
}//WarningCallback
//------------------------------------------------------------------
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//Create load options and set the WarningCallback property
//to catch warnings while loading workbook
LoadOptions options = new LoadOptions();
options.WarningCallback = new WarningCallback();
//Load the source excel file
Workbook book = new Workbook(dataDir + "sampleDuplicateDefinedName.xlsx", options);
//Save the workbook
book.Save(dataDir + "outputDuplicateDefinedName.xlsx");

控制台输出

这是使用提供的执行时上述代码的控制台输出示例 excel 文件.

Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:Introduction!$D$16:$D$17

Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:Panel!$B$228

Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:'Queries '!$D$14:$D$16