|
| 1 | +--- |
| 2 | +title: Apply number formatting to an entire column in Excel | Syncfusion |
| 3 | +description: Code example to apply number formatting to an entire column in Excel using Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to apply number formatting to an entire column in Excel? |
| 10 | + |
| 11 | +The following code examples demonstrate applying number formatting to an entire column in Excel using C# (Cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Column%20Number%20Format/.NET/ColumnNumberFormat/ColumnNumberFormat/Program.cs,180" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); |
| 20 | + IWorksheet sheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + //Case 1: Apply direct number format (zero-based index) |
| 23 | + sheet.Columns[0].NumberFormat = "yyyy-mm-dd"; //Column A |
| 24 | + sheet.Columns[3].NumberFormat = "$#,##0.00"; //Column D |
| 25 | + sheet.Columns[4].NumberFormat = "0.00%"; //Column E |
| 26 | + |
| 27 | + //Case 2: Apply style-based format (one-based index) |
| 28 | + IStyle style = workbook.Styles.Add("DecimalStyle"); |
| 29 | + style.NumberFormat = "0.00"; |
| 30 | + sheet.SetDefaultColumnStyle(3, style); //Column C |
| 31 | + |
| 32 | + //Saving the workbook |
| 33 | + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); |
| 34 | +} |
| 35 | +{% endhighlight %} |
| 36 | + |
| 37 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 38 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 39 | +{ |
| 40 | + IApplication application = excelEngine.Excel; |
| 41 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 42 | + IWorkbook workbook = application.Workbooks.Open("Input.xlsx"); |
| 43 | + IWorksheet sheet = workbook.Worksheets[0]; |
| 44 | + |
| 45 | + //Case 1: Apply direct number format (zero-based index) |
| 46 | + sheet.Columns[0].NumberFormat = "yyyy-mm-dd"; //Column A |
| 47 | + sheet.Columns[3].NumberFormat = "$#,##0.00"; //Column D |
| 48 | + sheet.Columns[4].NumberFormat = "0.00%"; //Column E |
| 49 | + |
| 50 | + //Case 2: Apply style-based format (one-based index) |
| 51 | + IStyle style = workbook.Styles.Add("DecimalStyle"); |
| 52 | + style.NumberFormat = "0.00"; |
| 53 | + sheet.SetDefaultColumnStyle(3, style); //Column C |
| 54 | + |
| 55 | + //Saving the workbook |
| 56 | + workbook.SaveAs("Output.xlsx"); |
| 57 | +} |
| 58 | +{% endhighlight %} |
| 59 | + |
| 60 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 61 | +Using excelEngine As New ExcelEngine() |
| 62 | + Dim application As IApplication = excelEngine.Excel |
| 63 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 64 | + Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx") |
| 65 | + Dim sheet As IWorksheet = workbook.Worksheets(0) |
| 66 | + |
| 67 | + 'Case 1: Apply direct number format (zero-based index) |
| 68 | + sheet.Columns(0).NumberFormat = "yyyy-mm-dd" 'Column A |
| 69 | + sheet.Columns(3).NumberFormat = "$#,##0.00" 'Column D |
| 70 | + sheet.Columns(4).NumberFormat = "0.00%" 'Column E |
| 71 | + |
| 72 | + 'Case 2: Apply style-based format (one-based index) |
| 73 | + Dim style As IStyle = workbook.Styles.Add("DecimalStyle") |
| 74 | + style.NumberFormat = "0.00" |
| 75 | + sheet.SetDefaultColumnStyle(3, style) 'Column C |
| 76 | + |
| 77 | + 'Save the workbook |
| 78 | + workbook.SaveAs("Output.xlsx") |
| 79 | +End Using |
| 80 | +{% endhighlight %} |
| 81 | +{% endtabs %} |
| 82 | + |
| 83 | +A complete working example that shows how to apply number formatting to an entire column in Excel using C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Column%20Number%20Format/.NET/ColumnNumberFormat">this GitHub page</a>. |
0 commit comments