Excel cell number format

Following example demonstrates how you can use GemBox.Spreadsheet to get formatted cell value. Formatted cell value is a textual representation of a cell value which is formatted with a number format applied on that cell.

Using this example you can also find out how specific number format from MS Excel should be used in GemBox.Spreadsheet API. Create new Excel file with MS Excel and populate its A2 cell in the first worksheet with a value and number format. Use that file as input to this example ('Upload your file' below) and run the example. Use value from output file's D column in GemBox.Spreadsheet API.

Excel cell numeric and text values formatted with GemBox.Spreadsheet
Excel cell numeric and text values formatted with GemBox.Spreadsheet
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        var workbook = ExcelFile.Load("%InputFileName%");

        var worksheet = workbook.Worksheets[0];

        worksheet.Cells[0, 2].Value = "ExcelCell.Value";
        worksheet.Columns[2].Style.NumberFormat = "@";

        worksheet.Cells[0, 3].Value = "CellStyle.NumberFormat";
        worksheet.Columns[3].Style.NumberFormat = "@";

        worksheet.Cells[0, 4].Value = "ExcelCell.GetFormattedValue()";
        worksheet.Columns[4].Style.NumberFormat = "@";

        for (int i = 1; i < worksheet.Rows.Count; i++)
            var sourceCell = worksheet.Cells[i, 0];

            worksheet.Cells[i, 2].Value = sourceCell.Value?.ToString();
            worksheet.Cells[i, 3].Value = sourceCell.Style.NumberFormat;
            worksheet.Cells[i, 4].Value = sourceCell.GetFormattedValue();

        // Set column widths.
        var columnWidths = new double[] { 192, double.NaN, 122, 236, 200 };
        for (int i = 0; i < columnWidths.Length; i++)
            if (!double.IsNaN(columnWidths[i]))
                worksheet.Columns[i].SetWidth(columnWidths[i], LengthUnit.Pixel);

        workbook.Save("Number Format.%OutputFileType%");
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim workbook = ExcelFile.Load("%InputFileName%")

        Dim worksheet = workbook.Worksheets(0)

        worksheet.Cells(0, 2).Value = "ExcelCell.Value"
        worksheet.Columns(2).Style.NumberFormat = "@"

        worksheet.Cells(0, 3).Value = "CellStyle.NumberFormat"
        worksheet.Columns(3).Style.NumberFormat = "@"

        worksheet.Cells(0, 4).Value = "ExcelCell.GetFormattedValue()"
        worksheet.Columns(4).Style.NumberFormat = "@"

        For i As Integer = 0 To worksheet.Rows.Count

            Dim sourceCell = worksheet.Cells(i, 0)

            worksheet.Cells(i, 2).Value = sourceCell.Value?.ToString()
            worksheet.Cells(i, 3).Value = sourceCell.Style.NumberFormat
            worksheet.Cells(i, 4).Value = sourceCell.GetFormattedValue()

        ' Set column widths.
        Dim columnWidths = New Double() {192, Double.NaN, 122, 236, 200}
        For i As Integer = 0 To columnWidths.Length - 1
            If Not Double.IsNaN(columnWidths(i)) Then worksheet.Columns(i).SetWidth(columnWidths(i), LengthUnit.Pixel)

        workbook.Save("Number Format.%OutputFileType%")
    End Sub
End Module

Check next example or download examples from GitHub.