IRONSOFTWAREHOME

How to Set Cell Data Formats in C# with IronXL

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronXL enables formatting Excel cells with built-in data formats like currency, percentages, dates, and custom number formats by setting the FormatString property on cells or ranges without Interop dependencies.

Quickstart: Apply Built-In Format to a Single Cell in One Line

This sample shows how to create a new Excel workbook and apply a built-in format to a specific cell using IronXL's API. No Interop dependencies needed - just set FormatString and save.

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. 2Copy and run this code snippet.

    IronXL.WorkBook book = IronXL.WorkBook.Create(); book.DefaultWorkSheet["B2"].FormatString = IronXL.Formatting.BuiltinFormats.Currency2;
    C#
  3. 3Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

How Do I Set Cell Data Formats in Excel?

The FormatString property can be accessed through both cells and ranges. Therefore, it is possible to set data formats for individual cells, columns, rows, and any selected range. This flexibility makes IronXL particularly powerful when you need to work with Excel files in C# without Interop.

Data formatting is essential for presenting information clearly in spreadsheets. Whether creating financial reports with currency values, scientific data with precise decimal places, or date-based analytics, proper formatting ensures your data is both readable and professional. IronXL simplifies this process by providing direct access to Excel's formatting engine through a straightforward API.

using IronXL;
using IronXL.Formatting;
using System;
using System.Linq;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the data format to 12300.00%
workSheet["A1"].Value = 123;
workSheet["A1"].FormatString = BuiltinFormats.Percent2;

// Set the data format to 123.0000
workSheet["A2"].Value = 123;
workSheet["A2"].FormatString = "0.0000";

// Set data display format to range
DateTime dateValue = new DateTime(2020, 1, 1, 12, 12, 12);
workSheet["A3"].Value = dateValue;
workSheet["A4"].Value = new DateTime(2022, 3, 3, 10, 10, 10);
workSheet["A5"].Value = new DateTime(2021, 2, 2, 11, 11, 11);

IronXL.Range range = workSheet["A3:A5"];

// Set the data format to 1/1/2020 12:12:12
range.FormatString = "MM/dd/yy h:mm:ss";

workBook.SaveAs("dataFormats.xlsx");
Excel spreadsheet showing formatted cells: percentage, number, and date/time values in different display formats

How Can I Set Cell Values as Strings Without Conversion?

When setting the value in IronXL, use StringValue instead of Value to directly assign the exact value to the cell without automatic conversion. This works like placing an apostrophe before the cell value in Excel. This technique is particularly useful when working with product codes, phone numbers, or other data that should remain as text. For more advanced string manipulation, check out how to write Excel values in .NET.

// Assign value as string
workSheet["A1"].StringValue = "4402-12";

How Do I Use Built-in Formats in IronXL?

The IronXL library offers a variety of predefined format strings that can be accessed through the IronXL.Formatting.BuiltinFormats class for formatting Excel cells. This allows you to customize how data is displayed in your Excel spreadsheets. These built-in formats cover the most common business scenarios and are compatible with all Excel versions.

When creating spreadsheets programmatically, applying consistent formatting is crucial for maintaining professional standards across your documents. The BuiltinFormats class provides over 30 pre-configured format patterns that match Excel's standard formatting options.

using IronXL;
using IronXL.Formatting;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Use builtin formats
workSheet["A1"].Value = 123;
workSheet["A1"].FormatString = BuiltinFormats.Accounting0;

workBook.SaveAs("builtinDataFormats.xlsx");

What Built-in Data Formats Are Available?

For the Duration type, input values are represented in days. For example, 1 day equals 24 hours, and 1.05 days equals 25 hours and 12 minutes. This is particularly useful when working with Excel data that involves time tracking or project management. Here are the available format types:

Excel spreadsheet showing built-in number formats with input values and formatted outputs for various data types

What Does Each Format Type Do?

  • General: Default format displaying numbers without specific formatting.
  • Duration1: Shows duration in minutes and seconds like "mm:ss."
  • Duration2: Shows duration in hours, minutes, and seconds like "[h]:mm:ss."
  • Duration3: Shows duration in minutes, seconds, and milliseconds like "mm:ss.0."
  • Accounting0: Accounting format without decimals, parentheses for negatives like "#,##0_);(#,##0)."
  • Accounting0Red: Accounting format without decimals, red for negatives like "#,##0_);Red."
  • Accounting2: Accounting format with two decimals, parentheses for negatives like "#,##0.00;(#,##0.00)."
  • Accounting2Red: Accounting format with two decimals, red for negatives like "#,##0.00_);Red."
  • Time1: 12-hour format with AM/PM like "h:mm AM/PM."
  • Time2: 12-hour format with seconds and AM/PM like "h:mm:ss AM/PM."
  • Time3: 24-hour format without seconds like "h:mm."
  • Time4: 24-hour format with seconds like "h:mm:ss."
  • ShortDate: Short date format like "m/d/yy."
  • ShortDateAndTime: Short date and time format like "m/d/yy h:mm."
  • LongDate1: Long date with month, day, year like "d-mmm-yy."
  • LongDate2: Long date with day and abbreviated month like "d-mmm."
  • LongDate3: Long date with abbreviated month and year like "mmm-yy."
  • Fraction1: Fractional values with one digit like "# ?/?."
  • Fraction2: Fractional values with two digits like "# ??/??."
  • Scientific1: Scientific notation with one decimal like "##0.0E+0."
  • Scientific2: Scientific notation with two decimals like "0.00E+00."
  • Percent: Percentage without decimals like "0%."
  • Percent2: Percentage with two decimals like "0.00%."
  • Currency0: Currency without decimals, parentheses for negatives like "$#,##0_);($#,##0)."
  • Currency0Red: Currency without decimals, red for negatives like "$#,##0_);Red."
  • Currency2: Currency with two decimals, parentheses for negatives like "$#,##0.00;($#,##0.00)."
  • Currency2Red: Currency with two decimals, red for negatives like "$#,##0.00_);Red."
  • Thousands0: Numbers with thousands separator, no decimals like "#,##0."
  • Thousands2: Numbers with thousands separator, two decimals like "#,##0.00."
  • Number0: Numbers without decimals like "0."
  • Number2: Numbers with two decimals like "0.00."
  • Text: Plain text format like "@."

Data formatting and number formatting in Excel control how numbers, dates, times, and other data display in cells, enhancing readability and ensuring data accuracy. With data formats, you can present information in specific formats like percentages or currencies. Similarly, number formats let you customize decimal places and display options.

How Do I Apply Multiple Formats to Different Ranges?

In real-world applications, you often need to apply different formats to various parts of your spreadsheet. This example demonstrates formatting a financial report with multiple data types. This approach is particularly useful when exporting to Excel in C# from databases or other data sources:

using IronXL;
using IronXL.Formatting;
using System;

// Create a financial report with multiple formats
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Headers
workSheet["A1"].Value = "Financial Report Q4 2023";
workSheet["A1"].Style.Font.Bold = true;

// Revenue section with currency formatting
workSheet["A3"].Value = "Revenue";
workSheet["B3"].Value = 2500000;
workSheet["B3"].FormatString = BuiltinFormats.Currency0;

// Growth percentage
workSheet["A4"].Value = "YoY Growth";
workSheet["B4"].Value = 0.157;
workSheet["B4"].FormatString = BuiltinFormats.Percent2;

// Date formatting
workSheet["A6"].Value = "Report Date";
workSheet["B6"].Value = DateTime.Now;
workSheet["B6"].FormatString = BuiltinFormats.LongDate1;

// Apply accounting format to expense columns
IronXL.Range expenseRange = workSheet["B8:B12"];
expenseRange.FormatString = BuiltinFormats.Accounting2Red;

// Save the formatted report
workBook.SaveAs("FinancialReport_Q4_2023.xlsx");

This example shows how different format types combine to create professional financial documents. For more advanced formatting options, including conditional formatting, you can extend these concepts further.

When working with large datasets or creating reports programmatically, consistent formatting becomes critical. IronXL's formatting system integrates seamlessly with other features like math functions and edit formulas, allowing you to create fully automated reporting solutions.

For developers migrating from Office Interop or seeking better performance, IronXL's formatting system provides a significant advantage. Unlike traditional approaches, you can format thousands of cells efficiently without COM interop overhead. To learn more about Excel automation best practices, explore our comprehensive Excel documentation.

Frequently Asked Questions

What is the main advantage of using IronXL for Excel cell formatting in C#?

IronXL enables Excel cell formatting in C# without relying on Interop, allowing developers to use built-in data formats like currency, percentages, dates, and custom number formats through the `FormatString` property.

How can I format a single cell in Excel using IronXL?

To format a single cell, set the `FormatString` property on the desired cell or range. For example, `workBook.DefaultWorkSheet["B2"].FormatString = IronXL.Formatting.BuiltinFormats.Currency2;` applies a currency format to cell B2.

Can IronXL format entire ranges of cells in an Excel sheet?

Yes, IronXL allows setting data formats for individual cells, columns, rows, and any selected range using the `FormatString` property, making it versatile for handling extensive formatting tasks.

What built-in formats are available in IronXL?

IronXL provides over 30 built-in format patterns accessible through the `IronXL.Formatting.BuiltinFormats` class, covering common scenarios like currency, percentage, scientific notation, and various date and time formats.

How do I handle string values in IronXL without automatic conversion?

To prevent automatic conversion of string values, use the `StringValue` property instead of `Value`. This is useful for maintaining data like product codes or phone numbers as text.

Is it possible to apply multiple formats in a single spreadsheet using IronXL?

Yes, you can apply different formats to various spreadsheet parts using IronXL. This is ideal for creating financial reports with diverse data types and maintaining professionalism across documents.

Does IronXL offer any performance benefits over Interop for Excel formatting?

IronXL provides significant performance improvements over traditional Interop by eliminating COM interop overhead, allowing efficient formatting of thousands of cells without relying on Office components.

How does IronXL's formatting enhance Excel reporting in C#?

IronXL’s formatting system integrates seamlessly with math functions and formula edits, making it invaluable for creating fully automated Excel reports programmatically.

What is a quick way to start using IronXL for formatting Excel cells?

To get started with IronXL, download the C# library, create a new or open an existing Excel worksheet, set the `FormatString` property to the desired format, and export your edited spreadsheet.

How does IronXL support data format customization?

Besides built-in formats, IronXL allows you to define custom formats using the `FormatString` property, providing flexibility when presenting specific types of data in Excel cells.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 2,237,574Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronXL"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronXL to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronXL.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required