Get Warnings while Loading Excel File
Possible Usage Scenarios
Sometimes the user tries to load the workbook which is somewhat corrupt but loadable. In such case, Aspose.Cells throws warnings while loading the workbook. You can catch these warnings by implementing the IWarningCallback interface and setting LoadOptions.WarningCallback property.
Get Warnings while Loading Excel File
The following sample code explains how to get warnings while loading excel file. The code loads the sample excel file which throws DuplicateDefinedName warning on loading. This warning is then caught by IWarningCallback.Warning() method that prints the warning messages on the console. The code then saves the workbook as output excel file. If you open the sample excel file in Microsoft Excel, it will also display you this warning as shown in this screenshot. Please also check the console output of the code given below for more understanding.
Sample Code
//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()); | |
} | |
} | |
} |
// 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"); |
Console Output
Here is the console output of the above code when executed with the provided sample excel file.
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