Skip to main content

Command Palette

Search for a command to run...

Format Numbers in Excel in C# (Currency/Date/Percentage)

Published
3 min readView as Markdown

In C# development, efficiently and accurately controlling the number format in Excel files is a key requirement in many automated processing scenarios. For example, in scenarios such as financial reporting, data export, and report generation, the uniformity and standardization of number formats (such as currency, percentage, date, and scientific notation) directly affect the professionalism and readability of the final document.

This article will introduce how to set different Excel number formats using the free library - Free Spire.XLS for .NET, helping you achieve refined control over number formats in C# projects.


I. Common Number Format Issues

In actual development, developers often encounter the following problems:

  • Financial data (such as amounts) displayed as "1.23E+05" instead of "123,000"
  • Percentage values not automatically adding the "%" symbol
  • Date and time fields being incorrectly recognized as numbers
  • Missing thousand separators, affecting reading experience

These issues can directly impact the credibility of reports and user experience.


II. Supported Number Format Types

Free Spire.XLS supports the following common types:

Format TypeCode Example (C#)Applicable Scenarios
Currency"¥#,##0.00"Financial statements, bills
Percentage"0.00%"Cost proportion, growth rate
Date"yyyy-MM-dd"Time records, log exports
Scientific Notation"0.00E+00"Big data, engineering calculations
Integer"0"Number of people, serial numbers, etc.
Custom"0.00" or "#,##0"Flexible adaptation to business needs

Installation:

Install-Package FreeSpire.XLS

Note: All formats are set through the CellRange.NumberFormat property.


III. Practical Example: C# Code to Set Number Formats

using Spire.Xls;

namespace SetNumberFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Workbook object
            Workbook workbook = new Workbook();

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            // Integer
            sheet.Range["A1"].NumberValue = 123;
            sheet.Range["A1"].NumberFormat = "00";

            // Thousands separator
            sheet.Range["A2"].NumberValue = 1234.5678;
            sheet.Range["A2"].NumberFormat = "#,##0.00";

            // Percentage
            sheet.Range["A3"].NumberValue = 0.12345;
            sheet.Range["A3"].NumberFormat = "0.0%";

            // Number with text
            sheet.Range["A4"].NumberValue = 1234;
            sheet.Range["A4"].NumberFormat = "\"Quantity: \"0";

            // Currency format
            sheet.Range["A5"].NumberValue = 1234.5678;
            sheet.Range["A5"].NumberFormat = "¥#,##0.00";

            // Scientific notation
            sheet.Range["A6"].NumberValue = 1234.5678;
            sheet.Range["A6"].NumberFormat = "0.00E+00";

            // Date
            sheet.Range["A7"].NumberValue = 45930;
            sheet.Range["A7"].NumberFormat = "yyyy-MM-dd";

            // Time
            sheet.Range["D13"].NumberValue = 0.5;
            sheet.Range["D13"].NumberFormat = "h:mm:ss AM/PM";

            // Save the result
            workbook.SaveToFile("NumberFormats.xlsx", ExcelVersion.Version2016);
        }
    }
}

Custom Format Strings: Format strings can be modified according to needs. For example, replacing the “$” in the currency format with another currency symbol, or changing the “0.0%” in the percentage format to “0.00%” to retain two decimal places.


With the above sample, developers can modify the format strings and input values according to actual needs to quickly adapt to Excel data display requirements in different scenarios.

More from this blog

coding

19 posts