Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to format a Excel Cell from C#

Posted on 2008-06-13
2
Medium Priority
?
9,413 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 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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