Åtkomst och uppdatering av delar av riktad text från cellen
Åtkomst och uppdatering av delar av riktad text från cellen
Följande kod visar användningen av Cell.GetCharacters() och Cell.SetCharacters() metod med källa excel filen som du kan ladda ner från den angivna länken. Källa excel filen har en rik text i cell A1. Den har 3 delar och varje del har ett olika teckensnitt. Följande kodsnutt kommer åt dessa delar och uppdaterar den första delen med ett nytt teckensnittsnamn. Slutligen sparar den arbetsboken som utdata excel fil. När du öppnar den kommer du att märka att teckensnittet för den första delen av texten har ändrats till “Arial”.
C#-kod för att nå och uppdatera delarna av riktext av cellen
// 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); |
Konsoloutput som genereras av provkoden
Här är konsolresultatet av den ovanstående kodprov med den källa excelfilen.
Before updating the font settings....
Century
Courier New
Verdana
After updating the font settings....
Arial
Courier New
Verdana