-
Notifications
You must be signed in to change notification settings - Fork 891
Using Rich Text
Francois Botha edited this page Feb 26, 2018
·
3 revisions
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Rich Text");
Let's start with a plain text and then decorate it...
var cell1 = ws.Cell(1, 1).SetValue("The show must go on...");
We want everything in blue except the word show (which we want in red and with Broadway Font)
cell1.Style.Font.FontColor = XLColor.Blue; // Set the color for the entire cell
cell1.RichText.Substring(4, 4)
.SetFontColor(XLColor.Red)
.SetFontName("Broadway"); // Set the color and font for the word "show"
On the next example we'll start with an empty cell and add the rich text
var cell = ws.Cell(3, 1);
// Add the text parts
cell.RichText
.AddText("Hello").SetFontColor(XLColor.Red)
.AddText(" BIG ").SetFontColor(XLColor.Blue).SetBold()
.AddText("World").SetFontColor(XLColor.Red);
Here we're showing that even though we added three pieces of text you can treat them like a single one.
cell.RichText.Substring(4, 7).SetUnderline();
Right now cell.RichText has the following 5 strings:
- "Hell" -> Red
- "o" -> Red, Underlined
- " BIG " -> Blue, Underlined, Bold
- "W" -> Red, Underlined
- "orld" -> Red
Of course you can loop through each piece of text and check its properties:
foreach (var richText in cell.RichText)
{
if(richText.Bold)
ws.Cell(3, 2).Value = String.Format("\"{0}\" is Bold.", richText.Text);
}
ws.Columns().AdjustToContents();
wb.SaveAs("UsingRichText.xlsx");
- How do I deliver an Excel file in ASP.NET?
- Does it support Excel 2003 and prior formats (.xls)?
- How can I insert an image?
- Text with numbers are getting converted to numbers, what's up with that?
- How do I get the result of a formula?
- Data Types
- Creating Multiple Worksheets
- Organizing Sheets
- Loading and Modifying Files
- Using Lambda Expressions
- Cell Values
- Workbook Properties
- Using Formulas
- Evaluating Formulas
- Creating Rows And Columns Outlines
- Hide Unhide Rows And Columns
- Freeze Panes
- Copying Worksheets
- Using Hyperlinks
- Data Validation
- Hide Worksheets
- Sheet Protection
- Tab Colors
- Conditional Formatting
- Pivot Table example
- Sparklines
- Copying IEnumerable Collections
- Inserting Data
- Inserting Tables
- Adding DataTable as Worksheet
- Adding DataSet
- Styles - Alignment
- Styles - Border
- Styles - Fill
- Styles - Font
- Styles - NumberFormat
- NumberFormatId Lookup Table
- Style Worksheet
- Style Rows and Columns
- Using Default Styles
- Using Colors
- ClosedXML Predefined Colors
- Excel Indexed Colors
- Using Rich Text
- Using Phonetics
- Defining Ranges
- Merging Cells
- Clearing Ranges
- Deleting Ranges
- Multiple Ranges
- Shifting Ranges
- Transpose Ranges
- Named Ranges
- Accessing Named Ranges
- Copying Ranges
- Using Tables
- Sorting Data
- Selecting Cells and Ranges
- Row Height and Styles
- Selecting Rows
- Inserting Rows
- Inserting and Deleting Rows
- Adjust Row Height and Column Width to Contents
- Row Cells
- Column Width and Styles
- Selecting Columns
- Inserting Columns
- Inserting and Deleting Columns
- Adjust Row Height and Column Width to Contents
- Column Cells
- Pages Tab
- Paper Size Lookup Table
- Margins Tab
- Headers and Footers Tab
- Sheet Tab
- Print Areas and Page Breaks