C# – Convert Excel Cell Value From Text To Number Using C#

cexcel

I am using Windows Application. In that Application i exported the DataGrid into Excel Successfully… Now the problem is , When i exported from Grid to the Excel Sheet, The cell values are having some green color mark on left top corner in the Excel Sheet… I thought that is type cast problem . How Shall i avoid that Problem…. and How to change the cell value from text to Number …(i.e)Convert To Number….

Can Anyone tell me the solution of this problem?

My Code for Formatting That Excel Sheet For Some Range of Values,

wksheet.Range[GetRanges[0].ToString(), GetRanges[GetRanges.Count-2].ToString()].Merge();

wksheet.get_Range(GetRanges[0].ToString(), GetRanges[GetRanges.Count-].ToString()).Interior.Color = Color.FromArgb(192, 0, 0);
                         

Best Answer

I haven't a Windows machine to test on at the moment, but perhaps you would want to try changing the cell format, e.g.:

my_range.NumberFormat = "0.0"; // change number of decimal places as needed

Here's a full example from Microsoft: How to automate Microsoft Excel from Microsoft Visual C#.NET.