Cambiar la ruta absoluta del archivo de origen de datos de enlace externo
Escenarios de uso posibles
Si desea cambiar la ruta absoluta del archivo de origen de datos de enlace externo, utilice la propiedad Workbook.AbsolutePath. Inicialmente, esta propiedad se establecerá en la ruta desde la cual se cargó el archivo de Excel. Pero puede establecerla en una cadena vacía o en alguna ruta de carpeta local o remota. Cada vez que cambie esta propiedad, también se cambiará la ruta del archivo de origen de datos de enlace externo.
Cambiar la ruta absoluta del archivo de origen de datos de enlace externo
El siguiente código de ejemplo carga el archivo de Excel de ejemplo que contiene un enlace externo. Primero imprime el origen de datos de enlace externo que imprime la ruta remota. Luego elimina la ruta remota e imprime nuevamente, esta vez imprime el origen de datos de enlace externo con la ruta local. Luego cambia la propiedad Workbook.AbsolutePath a una ruta local y remota e imprime nuevamente el origen de datos de enlace externo y los cambios se reflejan en la salida de la consola.
Código de muestra
// 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()); |
Salida de la consola
Aquí está la salida de la consola o depuración después de la ejecución del código de ejemplo anterior con el archivo de Excel de ejemplo.
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