在单元格中添加HTML富文本的C++示例
Contents
[
Hide
]
Aspose.Cells支持将以Microsoft Excel为导向的HTML转换为XLS/XLSX格式。也就是说,由Microsoft Excel生成的HTML可以使用Aspose.Cells转换回XLS/XLSX格式。
类似地,如果是一些简单的HTML,Aspose.Cells可以将其转换为HTML富文本。Aspose.Cells提供 Cell::GetHtmlString 方法,能够接受简单HTML并将其转换为格式化的单元格文本。
下面的代码示例向您展示了如何在单元格中添加HTML富文本。请查看输出Excel文件的屏幕截图。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Get the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access cell A1
Cell cell = worksheet.GetCells().Get(u"A1");
// Set HTML formatted text in the cell
cell.SetHtmlString(u"<Font Style=\"FONT-WEIGHT: bold;FONT-STYLE: italic;TEXT-DECORATION: underline;FONT-FAMILY: Arial;FONT-SIZE: 11pt;COLOR: #ff0000;\">This is simple HTML formatted text.</Font>");
// Save the workbook
workbook.Save(outDir + u"output_out.xlsx");
std::cout << "HTML formatted text added to cell A1 successfully!" << std::endl;
Aspose::Cells::Cleanup();
}