外部リンクデータソースファイルの絶対パスを変更する
可能な使用シナリオ
外部リンクデータソースファイルの絶対パスを変更したい場合は、Workbook.AbsolutePathプロパティを使用してください。このプロパティは最初はExcelファイルをロードした場所のパスに設定されます。しかし、空の文字列に設定するか、あるいはローカルフォルダのパスやリモートネットワークのパスに設定することができます。このプロパティを変更すると、外部リンクデータソースファイルのパスも変更されます。
外部リンクデータソースファイルの絶対パスを変更する
次のサンプルコードは、外部リンクを含むサンプルExcelファイルをロードします。最初に外部リンクデータソースを表示し、リモートパスを表示します。その後、リモートパスを削除し、再度表示します。このとき、外部リンクデータソースはローカルパスで表示されます。その後、Workbook.AbsolutePathプロパティをローカルおよびリモートパスに変更し、再び外部リンクデータソースを表示します。その変更がコンソール出力に反映されています。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ChangeAbsolutePathofExternalLink.class) + "articles/"; | |
// Load your source excel file containing the external link | |
Workbook wb = new Workbook(dataDir + "sample.xlsx"); | |
// Access the first external link | |
ExternalLink externalLink = wb.getWorksheets().getExternalLinks().get(0); | |
// Print the data source of external link, it will print existing remote | |
// path | |
System.out.println("External Link Data Source: " + externalLink.getDataSource()); | |
// Remove the remote path and print the new data source | |
// Assign the new data source to external link and print again, it will | |
// now print data source with local path | |
externalLink.setDataSource("ExternalAccounts.xlsx"); | |
System.out.println("External Link Data Source After Removing Remote Path: " + externalLink.getDataSource()); | |
// Change the absolute path of the workbook, it will also change the | |
// external link path | |
wb.setAbsolutePath("C:\\Files\\Extra\\"); | |
// Now print the data source again | |
System.out.println("External Link Data Source After Changing Workbook.AbsolutePath to Local Path: " + externalLink.getDataSource()); | |
// Change the absolute path of the workbook to some remote path, it will | |
// again affect the external link path | |
wb.setAbsolutePath("http://www.aspose.com/WebFiles/ExcelFiles/"); | |
// Now print the data source again | |
System.out.println("External Link Data Source After Changing Workbook.AbsolutePath to Remote Path: " + externalLink.getDataSource()); |
コンソール出力
以下は、与えられたサンプルExcelファイルを使用して上記のサンプルコードを実行した後のコンソール出力またはデバッグ出力です。
External Link Data Source: http:\\ws874dmErit\WebFiles\Files\300\ExternalAccounts.xlsx
External Link Data Source After Removing Remote Path: D:\Downloads\ExternalAccounts.xlsx
External Link Data Source After Changing Workbook.AbsolutePath to Local Path: C:\Files\Extra\ExternalAccounts.xlsx
External Link Data Source After Changing Workbook.AbsolutePath to Remote Path: http://www.aspose.com/WebFiles/ExcelFiles/ExternalAccounts.xlsx