Excelファイルの読み込み時に警告を取得する
Contents
[
Hide
]
可能な使用シナリオ
ユーザーがロード可能なデータがあるが不正なワークブックを読み込もうとすることがあります。このような場合、Aspose.Cellsはワークブックを読み込む際に警告を発行します。これらの警告をIWarningCallbackインターフェースを実装してLoadOptions.WarningCallbackプロパティを設定することでキャッチすることができます。
Excelファイルの読み込み中に警告を受け取る
Excelファイルを読み込みながら警告を取得する方法について以下のサンプルコードを説明します。このコードは、サンプルエクセルファイルをロードし、ロード中にDuplicateDefinedName警告を発生させます。この警告はIWarningCallback.Warning()メソッドでキャッチされ、コンソールに警告メッセージが出力されます。その後、ワークブックを出力エクセルファイルとして保存します。サンプルエクセルファイルをMicrosoft 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"); |
コンソール出力
提供されたサンプルエクセルファイルを使用して上記のコードを実行した際のコンソール出力は次のとおりです。
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