Insertar hipervínculos en Excel u OpenOffice

Adición de hipervínculos a datos de enlaces

Adición de hipervínculos

Se pueden agregar tres tipos de hipervínculo a una celda usando Aspose.Cells:

Aspose.Cells permite a los desarrolladores agregar hipervínculos a archivos de Excel ya sea usando el API ohojas de calculo de diseñador(hojas de cálculo donde los hipervínculos se crean manualmente y se usa Aspose.Cells para importarlos a otras hojas de cálculo).

Aspose.Cells proporciona una clase,Libro de trabajo que representa un archivo de Excel Microsoft. ÉlLibro de trabajola clase contiene unColección de hojas de trabajo que permite el acceso a cada hoja de trabajo en el archivo de Excel. Una hoja de trabajo está representada por elHoja de cálculo clase. ÉlHoja de cálculoLa clase proporciona diferentes métodos para agregar diferentes hipervínculos a archivos de Excel.

Agregar enlace a una URL

ÉlHoja de cálculo la clase contiene unhipervínculos recopilación. Cada artículo en elhipervínculos colección representa unHipervínculo . Agregue hipervínculos a las URL llamando alhipervínculos colecciónAgregarmétodo. ÉlAgregarmétodo toma los siguientes parámetros:

  • Cell nombre, el nombre de la celda a la que se agregará el hipervínculo.
  • Número de filas, el número de filas en este rango de hipervínculo.
  • Número de columnas, el número de columnas de este rango de hipervínculo
  • URL, la dirección URL.
// 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");

En el ejemplo anterior, se agrega un hipervínculo a una URL en una celda vacía,A1En tales casos, si una celda está vacía, la dirección URL también se agrega a esa celda vacía como su valor. Si la celda no está vacía y se agrega un hipervínculo, el valor de la celda parece texto sin formato. Para que parezca un hipervínculo, aplique la configuración de formato adecuada en esa celda.

// 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");

Agregar un enlace a un Cell en el mismo archivo

Es posible agregar hipervínculos a celdas en el mismo archivo de Excel llamando alhipervínculos colecciónAgregarmétodo. Él[Agregar](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String)El método ) funciona tanto para hipervínculos internos como externos. Una versión del método sobrecargado toma los siguientes parámetros:

  • Cell nombre, el nombre de la celda a la que se agregará el hipervínculo.
  • Número de filas, el número de filas en este rango de hipervínculo.
  • Número de columnas, el número de columnas en este rango de hipervínculo.
  • URL, la dirección de la celda objetivo.
// 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");

Agregar un enlace a un archivo externo

Es posible agregar hipervínculos a archivos de Excel externos llamando alhipervínculos colecciónAgregarmétodo. ÉlAgregarmétodo toma los siguientes parámetros:

  • Cell nombre, el nombre de la celda a la que se agregará el hipervínculo.
  • Número de filas, el número de filas en este rango de hipervínculo.
  • Número de columnas, el número de columnas en este rango de hipervínculo.
  • URL, la dirección del destino, archivo de Excel externo.
// 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");

Temas avanzados