Problem when exporting data from datagridview c # to excel sheet

0

Hello, I have the following code, in it I'm getting the data from the datagridview and exporting to an .xls, I'm passing everything as String, the texts, names, which are Strings, are normal, but there are numbers with until 44 digits that I pass as String, and excel already does the favor of converting to scientific notation, but I want the complete number.

Example, I get an .xml and game in the datagrid, like this: 35222200000238000800050010000000000007977111

And in the worksheet I'm exporting like this: 3.52222E + 43

SaveFileDialog salvar = new SaveFileDialog();
        Microsoft.Office.Interop.Excel.Application App;
        Microsoft.Office.Interop.Excel.Workbook WorkBook;
        Microsoft.Office.Interop.Excel.Worksheet WorkSheet;


        object misValue = System.Reflection.Missing.Value;

        App = new Microsoft.Office.Interop.Excel.Application();

        WorkBook = App.Workbooks.Add(misValue);
        WorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets.get_Item(1);

        int i = 0;
        int j = 0;

        for (i = 0; i <= dataGridView2.RowCount - 1; i++)
        {
            for (j = 0; j <= dataGridView2.ColumnCount - 1; j++)
            {

                DataGridViewCell cell = dataGridView2[j, i];
                WorkSheet.Cells[i + 1, j + 1] = cell.Value.ToString();

            }

        }

        salvar.Title = "Exportar para Excel";
        salvar.Filter = "Arquivo do Excel *.xls | *.xls";
        salvar.ShowDialog();

        WorkBook.SaveAs(salvar.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCurrentPlatformText,misValue, misValue, misValue, misValue,
        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,misValue, misValue, misValue, misValue, misValue);
        WorkBook.Close(true, misValue, misValue);
        App.Quit();
    
asked by anonymous 13.02.2015 / 12:17

1 answer

1

I've set the example:

for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
    DataGridViewCell cell = dataGridView1[j, i];
    if (j == 2)
    {
        WorkSheet.Cells[i + 1, j + 1].NumberFormat = "@";
    } 
    WorkSheet.Cells[i + 1, j + 1] = cell.Value.ToString();
}

    
13.02.2015 / 13:20