We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converting string to double does not translate to Excel

I am trying to convert a string to a double and set the value in MS Excel.

I have tried the following methods of converting:

double d = Convert.ToDouble(value);
cell.PutValue(value);
style.Number = 2; //Decimal to the hundreths
cell.SetStyle(style);

and,
cell.PutValue(value.ToString(), true);

however, when the data loads in Excel I get the error message that ‘the number is formatted as text’
image.png (3.6 KB)

even though when I select the properties of the cell it shows it has been formatted correctly.
image.png (17.6 KB)

Are you aware of what could cause this and how to have Excel recognize the value appropriately?
I have also tried to convert the value to a Fraction and Currency and get the exact same results.

@MrJoeyClausen,

Thanks for your query.

Apparently, using the line of code, i.e.,
cell.PutValue(value.ToString(), true);

it should fix your issue.

Anyways, we need to evaluate your issue/case a bit with your underlying data. Could you provide us complete code (runnable) to reproduce the issue. Also, attach your template file (if any). We will check it soon.

Your solution was one of my original attempts at fixing this problem, but did not work. If you look at my original post, that was included as something I found in the documentation and tried to apply to my solution.

@MrJoeyClausen,

The issue might be due to your own code or data which you are converting/formatting. That’s why I requested you to share your complete code to show the issue. I have tested using the following sample code, it works fine and I do not find the issue (“the number is formatted as text”) in Excel when opening the output file into it:
e.g
Sample code:

    Workbook workbook = new Workbook();
    Worksheet sheet = workbook.Worksheets[0];
    string val = "37356.7524";
    sheet.Cells["A1"].PutValue(val.ToString(), true);
    Style style = workbook.CreateStyle();
    style.Number = 2;
    sheet.Cells["A1"].SetStyle(style);

    workbook.Save("e:\\test2\\out1.xlsx");

The first two conditions DateTimeOffset and Int work fine. It is the third condition typeof(decimal) that is not working.

public void FormatCellValue(ExcelReportBuilderContext context, string value, Type valueType)
{
var cell = CurrentWorksheet.Cells[context.CurrentCellLocation.Row, context.CurrentCellLocation.ColIndex];
Style style = context.Workbook.CreateStyle();

        if (valueType != null)
        {
            if (valueType == typeof(DateTimeOffset))
            {
                cell.PutValue(value,true);
                style.Number = 14;  // date formatted as: m/d/yy
                cell.SetStyle(style);
            }
            if (valueType == typeof(int))
            {
                //int val = Convert.ToInt32(value);
                cell.PutValue(value, true);
                //style.Number = 1;   //Int
                //cell.SetStyle(style);
            }
            if (valueType == typeof(decimal))
            {
                cell.PutValue(value,true);
                style.Number = 2;   //Decimal to the hundreths
                cell.SetStyle(style);
            }
        }
    }

Please disregard this topic. You were correct in that I had another piece of code that was interfering with my expected results.

Your answer of: “cell.PutValue(value.ToString(), true);” was the correct answer and solved my problem.

Many thanks.

@MrJoeyClausen

Thanks for your feedback and using Aspose APIs.

It is good to know that your issue is resolved now so we have closed this topic. Let us know if you encounter any other issue, we will be glad to look into it and help you further.