C ා format the number string in Excel

Keywords: C# Excel Programming

In Excel, numeric strings are expressed in different formats and can represent different data meanings. For example, in financial statements, a specific number string format is needed to reflect amount information, currency, data accuracy, increase and decrease trend, etc. Let's share how to format the number string in Excel table through C ා programming.

Use tools: Spire.XLS for .NET

C code example

using Spire.Xls;
using System;

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

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

            //Write text to B1,C1
            sheet.Range["B1"].Text = "Digital format";
            sheet.Range["C1"].Text = "Application effect";
            sheet.Range["B1"].Style.Font.IsBold = true;
            sheet.Range["C1"].Style.Font.IsBold = true;

            //stay B3 Write text (i.e. number format) in the C3 Write numbers in Chinese and apply number format
            sheet.Range["B3"].Text = "0";
            sheet.Range["C3"].NumberValue = 12345678;
            sheet.Range["C3"].NumberFormat = "0";

            //Repeat the above steps to write more formats and corresponding effects
            sheet.Range["B4"].Text = "0.00";
            sheet.Range["C4"].NumberValue = 12345678;
            sheet.Range["C4"].NumberFormat = "0.00";

            sheet.Range["B5"].Text = "#,##0.00";
            sheet.Range["C5"].NumberValue = 12345678;
            sheet.Range["C5"].NumberFormat = "#,##0.00";

            sheet.Range["B6"].Text = "¥#,##0.00";
            sheet.Range["C6"].NumberValue = 12345678;
            sheet.Range["C6"].NumberFormat = "¥#,##0.00";

            sheet.Range["B7"].Text = "0;[Red]-0";
            sheet.Range["C7"].NumberValue = -12345678;
            sheet.Range["C7"].NumberFormat = "0;[Red]-0";

            sheet.Range["B8"].Text = "0.00;[Blue]-0.00";
            sheet.Range["C8"].NumberValue = -12345678;
            sheet.Range["C8"].NumberFormat = "0.00;[Blue]-0.00";

            sheet.Range["B9"].Text = "#,##0;[Red]-#,##0";
            sheet.Range["C9"].NumberValue = -12345678;
            sheet.Range["C9"].NumberFormat = "#,##0;[Red]-#,##0";

            sheet.Range["B10"].Text = "#,##0.00;[Green]-#,##0.000";
            sheet.Range["C10"].NumberValue = -12345678;
            sheet.Range["C10"].NumberFormat = "#,##0.00;[Green]-#,##0.00";

            sheet.Range["B11"].Text = "0.00E+00";
            sheet.Range["C11"].NumberValue = 12345678;
            sheet.Range["C11"].NumberFormat = "0.00E+00";

            sheet.Range["B12"].Text = "0.00%";
            sheet.Range["C12"].NumberValue = 12345678;
            sheet.Range["C12"].NumberFormat = "0.00%";

            sheet.Range["B13"].Text = "(###)###-###";
            sheet.Range["C13"].NumberValue = 128346841;
            sheet.Range["C13"].NumberFormat = "(###)###-###";

            sheet.Range["B14"].Text = "###-###";
            sheet.Range["C14"].NumberValue = 2019328;
            sheet.Range["C14"].NumberFormat = "###-#-##";

            //Set column width
            sheet.Columns[1].ColumnWidth = 20;
            sheet.Columns[2].ColumnWidth = 20;

            //Save document
            workbook.SaveToFile("output.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("output.xlsx");
        }
    }
}

Setting effect:

(end of this paper)

Reprint please indicate the source!

Posted by Wes1890 on Sat, 30 Nov 2019 19:46:22 -0800