在Excel或OpenOffice中插入超链接
添加超链接以链接数据
超链接用于在两个实体之间创建链接。每个人都熟悉超链接的使用,特别是在网站上。
使用Aspose.Cells,开发人员可以在Microsoft Excel文件中创建不同类型的超链接。本主题讨论了Aspose.Cells支持哪些类型的超链接以及如何在我们的Excel文件中使用它们。
添加超链接
使用Aspose.Cells可以向单元格添加三种类型的超链接:
Aspose.Cells允许开发人员通过使用API或设计者电子表格(手动创建超链接,并使用Aspose.Cells将它们导入其他电子表格)向Excel文件中添加超链接。
Aspose.Cells提供一个表示Microsoft Excel文件的类,即Workbook,Workbook类包含一个WorksheetCollection,它允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了向Excel文件添加不同超链接的不同方法。
添加指向URL的链接
Worksheet类包含一个Hyperlinks集合。Hyperlinks集合中的每一项都代表一个Hyperlink。通过调用Hyperlinks集合的Add 方法可以向URL添加超链接。Add 方法接受以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,此超链接范围的列数
- URL,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"); |
在上面的示例中,将超链接添加到一个空单元格A1的URL。在这种情况下,如果单元格为空,则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"); |
将链接添加到同一文件中的单元格
可以通过调用 Hyperlinks 集合的 Add 方法向同一 Excel 文件中的单元格添加超链接。Add 方法适用于内部和外部超链接。其中一个重载的方法版本接受以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- 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"); |
向外部文件添加链接
可以通过调用 Hyperlinks 集合的 Add 方法向外部 Excel 文件添加超链接。Add 方法接受以下参数:
- 单元格名称,超链接将添加到的单元格的名称。
- 行数,超链接范围中的行数。
- 列数,超链接范围中的列数。
- 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"); |