Få åtkomst till och uppdatera delarna av Rich Text på Cell
Contents
[
Hide
]
Aspose.Cells låter dig komma åt och uppdatera delarna av cellens RTF-text. För detta ändamål kan du användaCell.GetCharacters() ochCell.SetCharacters() metoder. Dessa metoder kommer att återvända och acceptera arrayen avFontSettingobjekt som du kan använda för att komma åt och uppdatera olika egenskaper för teckensnitt som teckensnittsnamn, teckensnittsfärg, fetstil, etc.
Få åtkomst till och uppdatera delarna av Rich Text på Cell
Följande kod visar användningen avCell.GetCharacters() ochCell.SetCharacters() metod med hjälp avsource excel-filsom du kan ladda ner från den medföljande länken. Excel-källfilen har en rik text i cellen A1. Den har 3 delar och varje del har olika typsnitt. Följande kodsnutt kommer åt dessa delar och uppdaterar den första delen med ett nytt teckensnittsnamn. Slutligen sparar den arbetsboken somoutput excel-fil . När du öppnar den kommer du att se att teckensnittet för den första delen av texten har ändrats till**“Arial”**.
C# kod för att komma åt och uppdatera delarna av Rich Text av Cell
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); |
Konsolutdata genererad av exempelkoden
Här är konsolutgången för ovanstående exempelkod med hjälp avsource excel-fil.
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana