HTMLにスプレッドシートをレンダリングする際のデフォルトフォントを設定する
Contents
[
Hide
]
Aspose.Cellsを使用すると、スプレッドシートをHTMLにレンダリングする際にデフォルトフォントを設定することができます。そのためには、HtmlSaveOptions.DefaultFontNameを使用してください。このプロパティは、スプレッドシート内に無効または存在しないフォントを持つセルがある場合に役立ちます。その場合、HtmlSaveOptions.DefaultFontNameプロパティで指定したフォントでセルがレンダリングされます。
スプレッドシートをHTMLにレンダリングする際のデフォルトフォントの設定
次のサンプルコードは、ブックを作成し、最初のワークシートのセルB4にテキストを追加し、そのフォントを未知の/存在しないフォントに設定します。それからブックを異なるデフォルトフォント名(Courier New、Arial、Times New Romanなど)でHTML形式で保存します。
次のスクリーンショットは、HtmlSaveOptions.DefaultFontName プロパティを使用して異なるデフォルトフォント名を設定した効果を示しています。
このコードは、異なる{0}を指定して生成された出力HTMLファイルとCourier New、Arial、Times New Romanを生成します。
サンプルコード
This file contains hidden or 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); |