Solved

How to format a Excel Cell from C#

Posted on 2008-06-13
2
9,405 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:sepknow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 21776915
In VB, the format is either of these....

    oSheet.Cells(1, 1).NumberFormat = "0.00"
    oSheet.Range("A1").NumberFormat = "0.00"

Wayne
0
 
LVL 2

Expert Comment

by:TheLastStraw
ID: 21777410
oSheet.cells(1,ColumnDataIsIn).entirecolumn.numberformat = "0.00"

this will format whole column with two significant decimal places
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question