更改外部链接数据源文件的绝对路径
Contents
[
Hide
]
可能的使用场景
如果要更改外部连接数据源文件的绝对路径,请使用 Workbook.AbsolutePath 属性。最初,该属性将设置为加载 Excel 文件的路径。但是您可以将其设置为空字符串,或者将其设置为某个本地文件夹路径或远程网络路径。每当更改此属性时,外部连接数据源文件的路径也将被更改。
更改外部连接数据源文件的绝对路径
以下示例代码加载了带有外部连接的 示例 excel 文件。首先打印外部连接数据源,打印远程路径。然后删除远程路径,再次打印,这次打印外部连接数据源为本地路径。然后将 Workbook.AbsolutePath 属性更改为本地和远程路径,并再次打印外部连接数据源,更改将反映在控制台输出中。
示例代码
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