将超链接插入 Excel 或 OpenOffice
添加超链接到链接数据
超链接用于在两个实体之间创建链接。每个人都熟悉超链接的使用,尤其是在网站上。
使用 Aspose.Cells,开发人员可以在 Microsoft Excel 文件中创建不同种类的超链接。本主题讨论 Aspose.Cells 支持哪些类型的超链接以及如何在我们的 Excel 文件中使用它们。
添加超链接
使用 Aspose.Cells 可以将三种类型的超链接添加到单元格:
Aspose.Cells 允许开发人员使用 API 或设计师电子表格(手动创建超链接并使用 Aspose.Cells 将它们导入其他电子表格的电子表格)。
Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了向 Excel 文件添加不同超链接的不同方法。
将链接添加到 URL
这工作表类包含一个超级链接收藏。中的每一项超级链接集合代表一个超级链接.通过调用超级链接收藏的[添加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。这添加方法采用以下参数:
- Cell name,超链接将添加到的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围的列数
- 网址,网址地址。
// 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"); |
在上面的示例中,超链接被添加到空单元格中的 URL,A1.在这种情况下,如果单元格为空,则 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(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"); |
在同一文件中添加指向 Cell 的链接
可以通过调用超级链接收藏的[添加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。这[添加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String)方法适用于内部和外部超链接。重载方法的一种版本采用以下参数:
- Cell name,超链接将添加到的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围内的列数。
- 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(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"); |
添加指向外部文件的链接
可以通过调用超级链接收藏的[添加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。这添加方法采用以下参数:
- Cell name,超链接将添加到的单元格的名称。
- Number of rows,这个超链接范围内的行数。
- Number of columns,这个超链接范围内的列数。
- URL,目标地址,外部Excel文件。
// 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"); |