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();

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.