Link to home
Start Free TrialLog in
Avatar of sepknow
sepknow

asked on

How to format a Excel Cell from C#

I am exporting some double datatype value to an Excel file
format (using C#, see attached code snippet).
In this case, its two numbers 2.11 and 2.10.
Problem in Excel is, the number is displayed as 2.11 and 2.1 instead of 2.11 and 2.10.

Question  :
How can I format the Excel cell such that it will display 2.10 and not 2.1?
Excel.Application oXL = new Excel.Application();
oXL.Visible = false;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
 
try
{
    object[,] rawData = new object[10,10];
 
    rawData[1, 1] = 2.11;    
    rawData[2, 1] = 2.10;    
 
    oSheet.Columns.AutoFit();
 
    // Get file name to save exported data
    saveFileDialog.FileName = "somefile";
    DialogResult mDR = ParSaveFileDialog.ShowDialog();
    if (mDR == DialogResult.OK)
    {
        oWB.SaveAs(saveFileDialog.FileName, Excel.XlFileFormat.xlExcel9795, string.Empty, string.Empty, false, false,
                   Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                   false, 0, 0);
    }
finally
{
    oWB.Close(false, string.Empty, false);
    oXL.Quit();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oSheet.cells(1,ColumnDataIsIn).entirecolumn.numberformat = "0.00"

this will format whole column with two significant decimal places