Search code examples
c#gembox-spreadsheet

Gembox removes precision when saving as CSV


I'm using Gembox spreadsheet to export some data to .CSV but aren't getting any decimals in the output file.

When exporting as XLSX everything looks as expected.

I've tried both Gembox 3.7 and 3.9 but the result is the same.

Use the following code to reproduce the issue.

        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var ef = new ExcelFile();

        var ws = ef.Worksheets.Add("NumberFormatTest");

        ws.Cells[0, 0].Value = "Expected";
        ws.Cells[1, 0].Value = "0";
        ws.Cells[2, 0].Value = "0.0";
        ws.Cells[3, 0].Value = "0.00";

        ws.Cells[0, 1].Value = "Actual";
        ws.Cells[1, 1].Value = 0m;
        ws.Cells[1, 1].Style.NumberFormat = "0";
        ws.Cells[2, 1].Value = 0m;
        ws.Cells[2, 1].Style.NumberFormat = "0.0";
        ws.Cells[3, 1].Value = 0m;
        ws.Cells[3, 1].Style.NumberFormat = "0.00";

        ef.Save("Numberformat test.csv");
        ef.Save("Numberformat test.xlsx");

How can I get the correct result without resorting to .ToString("0.00")?


Solution

  • If interesting for someone else. I was in contact with their support and they told me that the number format isn't currently exported into CSV file format and that the following workaround should be used.

    foreach (var row in ws.Rows)
        foreach (var cell in row.AllocatedCells)
            if (cell.Value != null)
                cell.Value = cell.GetFormattedValue();
    
    
    ef.Save("Numberformat test.csv");