Solved

How to format a Excel Cell from C#

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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