Search code examples
c#excelgembox-spreadsheet

Gembox spreadsheet : Is it possible to apply "Convert To Number" for the cells with warning?


I am trying set my formatted datatable to an excel worksheet.

Simply :

ws.InsertDataTable(myDatatable,
new InsertDataTableOptions()
{
    ColumnHeaders = false,
    StartRow = 5
});

But myDatatable cells contains numeric values mostly. And when I insert my datatable into excel via Gembox Spreadsheet, many of the cells got "Number Stored as Text" warning.

I don't want to disable this warning, but I want to apply a "Convert To Number" as we do in excel.


Solution

  • I presume that those numeric values in myDatatable are of string type, am I right?
    In that case try the following:

    var options = new InsertDataTableOptions()
    {
        ColumnHeaders = false,
        StartRow = 5
    };
    
    options.DataTableCellToExcelCellConverting += (s, e) =>
    {
        string textNumber = e.DataTableValue as string;
        double number;
        if (textNumber != null && double.TryParse(textNumber, out number))
            e.ExcelCellValue = number;
    };
    
    ws.InsertDataTable(myDatatable, options);