セルのリッチテキストの一部をアクセスして更新する
Contents
[
Hide
]
Aspose.Cellsを使用すると、セルのリッチテキストの部分にアクセスして更新することができます。このために、Cell.GetCharacters()およびCell.SetCharacters()メソッドを使用することができます。これらのメソッドは、フォント名、フォント色、太字などのフォントのさまざまなプロパティにアクセスおよび更新するために使用できるFontSettingオブジェクトの配列を返し、受け入れます。
セルのリッチテキストの部分にアクセスして更新
以下のコードは、提供されたリンクからダウンロードできるソースのExcelファイルを使用して、Cell.GetCharacters()およびCell.SetCharacters()メソッドの使用方法を示しています。ソースのExcelファイルには、セルA1にリッチテキストが含まれています。3つの部分があり、それぞれ異なるフォントを持っています。以下のコードスニペットは、これらの部分にアクセスして最初の部分のフォントを更新し、最終的にワークブックを出力のExcelファイルとして保存します。それを開くと、テキストの最初の部分のフォントが**“Arial”**に変更されていることがわかります。
C# コードでセルのリッチテキストの部分にアクセスして更新する
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); | |
string inputPath = dataDir + "Sample.xlsx"; | |
string outputPath = dataDir + "Output.out.xlsx"; | |
Workbook workbook = new Workbook(inputPath); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
Cell cell = worksheet.Cells["A1"]; | |
Console.WriteLine("Before updating the font settings...."); | |
FontSetting[] fnts = cell.GetCharacters(); | |
for (int i = 0; i < fnts.Length; i++) | |
{ | |
Console.WriteLine(fnts[i].Font.Name); | |
} | |
// Modify the first FontSetting Font Name | |
fnts[0].Font.Name = "Arial"; | |
// And update it using SetCharacters() method | |
cell.SetCharacters(fnts); | |
Console.WriteLine(); | |
Console.WriteLine("After updating the font settings...."); | |
fnts = cell.GetCharacters(); | |
for (int i = 0; i < fnts.Length; i++) | |
{ | |
Console.WriteLine(fnts[i].Font.Name); | |
} | |
// Save workbook | |
workbook.Save(outputPath); |
サンプルコードによって生成されたコンソール出力
以下は、ソースエクセルファイル を使用して上記サンプルコードのコンソール出力です。
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana