Modifier le chemin absolu du fichier de source de données de lien externe
Scénarios d’utilisation possibles
Si vous souhaitez modifier le chemin absolu du fichier de source de données du lien externe, veuillez utiliser leWorkbook.AbsolutePathWorkbook.AbsolutePathla propriété. Initialement, cette propriété sera définie sur le chemin à partir duquel le fichier Excel a été chargé. Mais vous pouvez le définir sur une chaîne vide ou vous pouvez le définir sur un chemin de dossier local ou un chemin de réseau distant. Chaque fois que vous modifierez cette propriété, le chemin du fichier de source de données de lien externe sera également modifié.
Modifier le chemin absolu du fichier de source de données de lien externe
L’exemple de code suivant charge leexemple de fichier excel qui contient un lien externe. Il imprime d’abord la source de données du lien externe qui imprime le chemin distant. Ensuite, il supprime le chemin distant et imprime à nouveau, cette fois, il imprime la source de données du lien externe avec le chemin local. Ensuite, il change leWorkbook.AbsolutePathWorkbook.AbsolutePathpropriété à un chemin local et distant et imprime à nouveau la source de données du lien externe et les modifications sont reflétées dans la sortie de la console.
Exemple de code
// 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()); |
Sortie console
Voici la console ou la sortie de débogage après l’exécution de l’exemple de code ci-dessus avec leexemple de fichier 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