Excel ファイルの読み込み中に警告を受け取る
Contents
[
Hide
]
考えられる使用シナリオ
ときどき、ユーザーがワークブックを読み込もうとすることがあります。ワークブックは多少壊れていますが、読み込めます。このような場合、Aspose.Cells はワークブックの読み込み中に警告をスローします。これらの警告は、実装することでキャッチできます。**IWarningCallbackインターフェイスと設定LoadOptions.WarningCallback**財産。
Excel ファイルの読み込み中に警告を受け取る
次のサンプル コードは、Excel ファイルの読み込み中に警告を取得する方法を説明しています。コードはサンプルエクセルファイル投げる**DuplicateDefinedName**ロード時の警告。この警告は、**IWarningCallback.Warning()**コンソールに警告メッセージを出力するメソッド。次に、コードはワークブックを次のように保存します。出力エクセルファイル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"); |
コンソール出力
上記のコードを提供されたコマンドで実行したときのコンソール出力は次のとおりです。サンプルエクセルファイル.
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