在单元格内添加 HTML 富文本
Contents
[
Hide
]
Aspose.Cells支持将以Microsoft Excel为导向的HTML转换为XLS/XLSX格式。也就是说,由Microsoft Excel生成的HTML可以使用Aspose.Cells转换回XLS/XLSX格式。
同样,如果有一些简单的HTML,Aspose.Cells可以将其转换为HTML丰富文本。Aspose.Cells提供Cell.setHtmlString()属性,可以接受这样的简单HTML并将其转换为格式化的单元格文本。
示例
以下截图显示了使用Aspose.Cells生成的输出Excel文件。如您所见,它显示了在单元格A1中添加的HTML格式化富文本,使用Cell.setHtmlString()属性。
这是生成上述截图中显示的输出Excel文件的示例代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(AddImageHyperlinks.class); | |
// Instantiate a new workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Insert a string value to a cell | |
worksheet.getCells().get("C2").setValue("Image Hyperlink"); | |
// Set the 4th row height | |
worksheet.getCells().setRowHeight(3, 100); | |
// Set the C column width | |
worksheet.getCells().setColumnWidth(2, 21); | |
// Add a picture to the C4 cell | |
int index = worksheet.getPictures().add(3, 2, 4, 3, dataDir + "aspose-logo.jpg"); | |
// Get the picture object | |
com.aspose.cells.Picture pic = worksheet.getPictures().get(index); | |
// Set the placement type | |
pic.setPlacement(PlacementType.FREE_FLOATING); | |
// Add an image hyperlink | |
pic.addHyperlink("http://www.aspose.com/"); | |
com.aspose.cells.Hyperlink hlink = pic.getHyperlink(); | |
// Specify the screen tip | |
hlink.setScreenTip("Click to go to Aspose site"); | |
// Save the Excel file | |
workbook.save(dataDir + "ImageHyperlink.xls"); |