954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Datagridview to Excel - Format Cells

Hi,

I am extracting data from datagridview to Excel. But for columns with so many numbers Excel turns the format to 6,45646E+11
Is there a way that I can prevent this.
I tried to use worksheets but I could not even save the data by using that.
Here is my code:

int i = 1;
            int j;

            foreach (DataRow row in datatable.Rows)
            {
                j = 1;
                foreach (DataColumn col in datatable.Columns)
                {
                    ExcelApp.Cells[i, j] = row[col.ColumnName].ToString();
                    j = j + 1;
                }
                i = i + 1;
            }

 ExcelApp.ActiveWorkbook.SaveAs(filename, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    ExcelApp.ActiveWorkbook.Saved = true;

            ExcelApp.Quit();
irmak1771
Newbie Poster
3 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I'm not sure if there is a typo in your format example because that seems rather large, but have you looked into controlling the NumberFormat property? See: http://msdn.microsoft.com/en-us/library/aa224873(office.11).aspx

It is supposed to allow you the same flexibility as the number properties formatter in Excel.

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: