Search code examples
c#excelopenxml-sdk

The Excel cell type is 'Numeric' but still it appears as 'General'


I am using OpenXML spreadsheet package in C# to convert datatable to excel. In code I am writing this to append cells with Numeric Type:

 public void MakeNumericCell(string cellReference, string cellStringValue, Row excelRow)
 {

    //  Add a new Excel Cell to our Row 
    Cell cell = new Cell() { CellReference = cellReference, DataType=CellValues.Number };
    CellValue cellValue = new CellValue(cellStringValue);       
    cell.Append(cellValue);
    excelRow.Append(cell);

 }

Please note that I am not asking How to convert Text cell to Number or change data type (format) of Excel Cell whose answer already exists on SO.

In my case, the cells do convert to number format in excel sheet. I can find the type of Excel cell by using formula =Type(Cell) which returns 1 if the cell type is numeric. As shown in image, the last column (name of column is Type of Cell) is evaluated using =Type(cell) formula which returns 1 for the C column, but the it still appears in General as highlighted in the image.

Am I doing something wrong here? Or is it an issue in Excel itself?

Numeric cells appear as General


Solution

  • You've misunderstood what the Type function does. It returns the type of the value, not the format of the cell.

    If you leave the formatting alone, and change the contents of the cell to "test," the type will change to 2. If you change it to "true" the type will come back as 4. It has nothing to do with the formatting of the cell, which is independent of the content.