将呈现电子表格时的默认字体设置为 HTML
Contents
[
Hide
]
Aspose.Cells 允许您在将电子表格呈现为 HTML 时设置默认字体。请使用HtmlSaveOptions.DefaultFontName以此目的。当电子表格中的某些单元格具有无效或不存在的字体时,此属性很有用。然后这些单元格将以指定的字体呈现HtmlSaveOptions.DefaultFontName财产。
将呈现电子表格时的默认字体设置为 HTML
以下示例代码创建一个工作簿并在第一个工作表的单元格 B4 中添加一些文本,并将其字体设置为某种未知/不存在的字体。然后通过设置不同的默认字体名称(如 Courier New、Arial、Times New Roman 等)将工作簿保存在 HTML 中。
截图显示了通过设置不同的默认字体名称的效果HtmlSaveOptions.DefaultFontName财产。
该代码生成使用 Courier New 输出 HTML 文件 , 这用 Arial 输出 HTML 和使用 Times New Roman 输出 HTML 文件.
示例代码
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object and access first worksheet. | |
Workbook wb = new Workbook(); | |
Worksheet ws = wb.Worksheets[0]; | |
// Access cell B4 and add some text inside it. | |
Cell cell = ws.Cells["B4"]; | |
cell.PutValue("This text has some unknown or invalid font which does not exist."); | |
// Set the font of cell B4 which is unknown. | |
Style st = cell.GetStyle(); | |
st.Font.Name = "UnknownNotExist"; | |
st.Font.Size = 20; | |
cell.SetStyle(st); | |
// Now save the workbook in html format and set the default font to Courier New. | |
HtmlSaveOptions opts = new HtmlSaveOptions(); | |
opts.DefaultFontName = "Courier New"; | |
wb.Save(dataDir + "out_courier_new_out.htm", opts); | |
// Now save the workbook in html format once again but set the default font to Arial. | |
opts.DefaultFontName = "Arial"; | |
wb.Save(dataDir + "out_arial_out.htm", opts); | |
// Now save the workbook in html format once again but set the default font to Times New Roman. | |
opts.DefaultFontName = "Times New Roman"; | |
wb.Save(dataDir + "times_new_roman_out.htm", opts); |