|
| 1 | +--- |
| 2 | +title: How to apply styles to an Entire Excel Worksheet | Syncfusion |
| 3 | +description: Learn how to apply font settings and fill color to an entire Excel worksheet using the Syncfusion .NET Excel library (XlsIO) in C# and VB.NET. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to apply styles to the entire worksheet in Excel? |
| 10 | + |
| 11 | +The following examples show how to apply font attributes (name and size) and fill color to an entire worksheet using C# (cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Open("../../../Data/Input.xlsx", ExcelOpenType.Automatic); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + //Define new styles to apply in rows and columns |
| 23 | + IStyle columnStyle = workbook.Styles.Add("ColumnStyle"); |
| 24 | + columnStyle.Font.FontName = "Times New Roman"; |
| 25 | + columnStyle.Font.Size = 10; |
| 26 | + columnStyle.Color = Color.Pink; |
| 27 | + |
| 28 | + worksheet.SetDefaultColumnStyle(1, 16384, columnStyle); |
| 29 | + |
| 30 | + //Save the Excel document |
| 31 | + workbook.SaveAs("../../../Output/FontStyle.xlsx"); |
| 32 | +} |
| 33 | +{% endhighlight %} |
| 34 | + |
| 35 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 36 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 37 | +{ |
| 38 | + IApplication application = excelEngine.Excel; |
| 39 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 40 | + IWorkbook workbook = application.Workbooks.Open("../../Data/Input.xlsx", ExcelOpenType.Automatic); |
| 41 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 42 | + |
| 43 | + //Define new styles to apply in rows and columns |
| 44 | + IStyle columnStyle = workbook.Styles.Add("ColumnStyle"); |
| 45 | + columnStyle.Font.FontName = "Times New Roman"; |
| 46 | + columnStyle.Font.Size = 10; |
| 47 | + columnStyle.Color = Color.Pink; |
| 48 | + |
| 49 | + worksheet.SetDefaultColumnStyle(1, 16384, columnStyle); |
| 50 | + |
| 51 | + //Save the Excel document |
| 52 | + workbook.SaveAs("../../Output/FontStyle.xlsx"); |
| 53 | + |
| 54 | +} |
| 55 | +{% endhighlight %} |
| 56 | + |
| 57 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 58 | +Using excelEngine As New ExcelEngine() |
| 59 | + Dim application As IApplication = excelEngine.Excel |
| 60 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 61 | + |
| 62 | + Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Input.xlsx", ExcelOpenType.Automatic) |
| 63 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 64 | + |
| 65 | + 'Define new styles to apply in rows and columns |
| 66 | + Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle") |
| 67 | + columnStyle.Font.FontName = "Times New Roman" |
| 68 | + columnStyle.Font.Size = 10 |
| 69 | + columnStyle.Color = Color.Pink |
| 70 | + |
| 71 | + worksheet.SetDefaultColumnStyle(1, 16384, columnStyle) |
| 72 | + |
| 73 | + 'Save the Excel document |
| 74 | + workbook.SaveAs("../../Output/FontStyle.xlsx") |
| 75 | +End Using |
| 76 | +{% endhighlight %} |
| 77 | +{% endtabs %} |
| 78 | + |
| 79 | +N> |
| 80 | +* Applying a default style to cells replaces any existing styles. This is standard Excel behavior. |
| 81 | +* To add new styling without removing existing formats, set specific properties on targeted ranges. |
| 82 | + |
| 83 | +The following code snippet shows how to apply a new style without affecting existing styles: |
| 84 | + |
| 85 | +{% tabs %} |
| 86 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 87 | + worksheet.Range["A1:F13"].CellStyle.Font.FontName = "Times New Roman"; |
| 88 | + worksheet.Range["A1:F13"].CellStyle.Font.Size = 10; |
| 89 | + worksheet.Range["A1:F13"].CellStyle.Color = Color.Lavender; |
| 90 | +{% endhighlight %} |
| 91 | + |
| 92 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 93 | + worksheet.Range["A1:F13"].CellStyle.Font.FontName = "Times New Roman"; |
| 94 | + worksheet.Range["A1:F13"].CellStyle.Font.Size = 10; |
| 95 | + worksheet.Range["A1:F13"].CellStyle.Color = Color.Lavender; |
| 96 | +{% endhighlight %} |
| 97 | + |
| 98 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 99 | +worksheet.Range("A1:F13").CellStyle.Font.FontName = "Times New Roman" |
| 100 | +worksheet.Range("A1:F13").CellStyle.Font.Size = 10 |
| 101 | +worksheet.Range("A1:F13").CellStyle.Color = Color.Lavender |
| 102 | +{% endhighlight %} |
| 103 | +{% endtabs %} |
| 104 | + |
0 commit comments