Solved

How to format a Excel Cell from C#

Posted on 2008-06-13
2
9,400 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
Comment Utility
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
Comment Utility
oSheet.cells(1,ColumnDataIsIn).entirecolumn.numberformat = "0.00"

this will format whole column with two significant decimal places
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

11 Experts available now in Live!

Get 1:1 Help Now