Solved

How to format a Excel Cell from C#

Posted on 2008-06-13
2
9,401 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now