更改外部链接数据源文件的绝对路径
Contents
[
Hide
]
可能的使用场景
如果要更改外部链接数据源文件的绝对路径,那么请使用工作簿.绝对路径财产。最初,此属性将设置为加载 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
// 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