加载 Excel 文件时收到警告
Contents
[
Hide
]
可能的使用场景
有时,用户尝试加载有些损坏但可加载的工作簿。在这种情况下,Aspose.Cells 在加载工作簿时会引发警告。您可以通过实施**IWarningCallback**界面与设置**LoadOptions.WarningCallback**财产。
加载 Excel 文件时收到警告
以下示例代码解释了如何在加载 excel 文件时获取警告。代码加载示例 excel 文件哪个抛出**DuplicateDefinedName**加载警告。这个警告然后被捕获**IWarningCallback.Warning()**在控制台上打印警告消息的方法。然后代码将工作簿另存为输出excel文件.如果您在 Microsoft Excel 中打开示例 excel 文件,它也会向您显示此屏幕截图所示的警告。另请检查下面给出的代码的控制台输出以获得更多理解。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Implement IWarningCallback interface to catch warnings while loading workbook | |
public class WarningCallback implements IWarningCallback | |
{ | |
public void warning(WarningInfo warningInfo) | |
{ | |
if(warningInfo.getWarningType() == WarningType.DUPLICATE_DEFINED_NAME) | |
{ | |
System.out.println("Duplicate Defined Name Warning: " + warningInfo.getDescription()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(GetWarningLoadingAnExcel.class) + "loading_saving/"; | |
//Create load options and set the WarningCallback property | |
//to catch warnings while loading workbook | |
LoadOptions options = new LoadOptions(); | |
options.setWarningCallback(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