Hyperlinks in Excel oder OpenOffice einfügen

Drei Arten von Hyperlinks können mithilfe von Aspose.Cells zu einer Zelle hinzugefügt werden:

Mit Aspose.Cells können Entwickler Hyperlinks zu Excel-Dateien entweder über die API oder Designer-Arbeitsmappen (Arbeitsmappen, bei denen Hyperlinks manuell erstellt werden und Aspose.Cells verwendet wird, um sie in andere Arbeitsmappen zu importieren) hinzufügen.

Aspose.Cells bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei darstellt. Die Workbook-Klasse enthält eine WorksheetCollection, die den Zugriff auf jede Arbeitsmappe in der Excel-Datei ermöglicht. Eine Arbeitsmappe wird durch die Worksheet-Klasse dargestellt. Die Worksheet-Klasse bietet verschiedene Methoden zum Hinzufügen verschiedener Hyperlinks zu Excel-Dateien.

Die Worksheet-Klasse enthält eine Hyperlinks-Sammlung. Jedes Element in der Hyperlinks-Sammlung stellt einen Hyperlink dar. Fügen Sie Hyperlinks zu URLs hinzu, indem Sie die Methode Add der Hyperlinks-Sammlung aufrufen. Die Methode Add nimmt die folgenden Parameter an:

  • Zellname, der Name der Zelle, zu der der Hyperlink hinzugefügt wird.
  • Anzahl der Zeilen, die Anzahl der Zeilen im Hyperlink-Bereich.
  • Anzahl der Spalten, die Anzahl der Spalten im Hyperlink-Bereich.
  • URL, die URL-Adresse.
// 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(AddingLinkToURL.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
// Adding a hyperlink to a URL at "A1" cell
hyperlinks.add("A1", 1, 1, "http://www.aspose.com");
// Saving the Excel file
workbook.save(dataDir + "AddingLinkToURL_out.xls");
// Print message
System.out.println("Process completed successfully");

In obigem Beispiel wird einem Hyperlink eine URL in einer leeren Zelle, A1, hinzugefügt. In solchen Fällen, wenn eine Zelle leer ist, wird auch die URL-Adresse zu dieser leeren Zelle als Wert hinzugefügt. Ist die Zelle nicht leer und wird ein Hyperlink hinzugefügt, sieht der Wert der Zelle wie normaler Text aus. Um ihn wie einen Hyperlink aussehen zu lassen, wenden Sie die entsprechenden Formatierungseinstellungen auf diese Zelle an.

// 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(AddingLinkToURLNotEmpty.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
// Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
// Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
// Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
// Adding a hyperlink to a URL at "A1" cell
hyperlinks.add("A1", 1, 1, "http://www.aspose.com");
// Saving the Excel file
workbook.save(dataDir + "AddingLinkToURLNotEmpty_out.xls");

Es ist möglich, Hyperlinks zu Zellen in derselben Excel-Datei hinzuzufügen, indem Sie die Methode Add der Hyperlinks-Sammlung aufrufen. Die Methode Add funktioniert sowohl für interne als auch externe Hyperlinks. Eine Version der überladenen Methode nimmt die folgenden Parameter an:

  • Zellname, der Name der Zelle, zu der der Hyperlink hinzugefügt wird.
  • Anzahl der Zeilen, die Anzahl der Zeilen im Hyperlink-Bereich.
  • Anzahl der Spalten, die Anzahl der Spalten im Hyperlink-Bereich.
  • URL, die Adresse der Zielzelle.
// 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(AddingLinkToAnotherCell.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
workbook.getWorksheets().add();
Worksheet sheet = worksheets.get(0);
// Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
// Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
// Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in the same Excel file
hyperlinks.add("B3", 1, 1, "Sheet2!B9");
// Saving the Excel file
workbook.save(dataDir + "ALinkTACell_out.xls");
// Print message
System.out.println("Process completed successfully");

Es ist möglich, Hyperlinks zu externen Excel-Dateien hinzuzufügen, indem die Methode Add der Hyperlinks -Sammlung aufgerufen wird. Die Methode Add nimmt die folgenden Parameter an:

  • Zellname, der Name der Zelle, zu der der Hyperlink hinzugefügt wird.
  • Anzahl der Zeilen, die Anzahl der Zeilen im Hyperlink-Bereich.
  • Anzahl der Spalten, die Anzahl der Spalten im Hyperlink-Bereich.
  • URL, die Adresse des Ziels, externen Excel-Datei.
// 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(AddingLinkToExternalFile.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
// Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
// Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
// Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
// Adding a link to the external file
hyperlinks.add("A5", 1, 1, dataDir + "book1.xls");
// Saving the Excel file
workbook.save(dataDir + "ALToEFile_out.xls");
// Print message
System.out.println("Process completed successfully");

Erweiterte Themen