Search code examples
c#openxmlclosedxml

Invalid data after writing to the Excel cell using ClosedXML


I'm trying to write a value to cell "10.1", but after writing in the cell, I get the value: "43110"

class Program
{
    static void Main( string[] args )
    {
        var workbook = new XLWorkbook();
        var worksheet = workbook.Worksheets.Add( "Sample Sheet" );
        worksheet.Cell( "A1" ).Value = "10,1";
        worksheet.Cell( "A1" ).DataType = XLDataType.Text;

        string testValue = worksheet.Cell( "A1" ).Value.ToString();
        Console.WriteLine( testValue );

        workbook.SaveAs( "testSheet.xlsx" );

        Console.ReadKey();
    }
}

If you want to run the code from this example, before this install the ClosedXML package from NuGet


Solution

  • The problem was solved with the help of the ClosedXML community, thanks https://github.com/igitur

    " ClosedXML is recognizing the string as a valid date. I'm guessing that your regional date settings allow . or , as a date separator. If you're sure you want to set the value as data type text, you can prepend the value with '."

    i.e.

     worksheet.Cell( "A1" ).Value = "'10,1";
    

    More info about this at https://github.com/ClosedXML/ClosedXML/wiki/Cell-Values