Problem when exporting data from c# datagridview to excel spreadsheet

Asked

Viewed 1,226 times

0

Good morning guys, I have the following code below, in it I am taking the data from the datagridview and exporting to one . xls, I’m going through everything like String, the texts, names, which are Strings, are normal, but there are numbers with up to 44 digits which step as String, and excel already does the favor of converting to scientific notation, but want the full number.

Example, taken from a . xml and play on the datagrid, like this: 35222200000238000800050010000000000007977111

And in the spreadsheet is exporting me so: 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();
  • 1

    By the size of this number, I believe excel will only display it as text. In the "for" of the columns you can identify this column that contains this value?

  • yes, the first and seventh columns, both have values that are being converted into scientific notation.

  • Do an if to identify the column, if it is the desired column, before the instruction "Worksheet.Cells[i + 1, j + 1] = Cell.Value.Tostring();" check if it will work (not tested) Worksheet.Cells[i + 1, j + 1]. Numberformat = "@" and then assign the value.

1 answer

1


I made 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();
}

inserir a descrição da imagem aqui

  • Cara did exactly the same thing, but it didn’t work, the Numberformat does not auto complete in Cells. can’t call any method here, are using which version ? added which version of references ? of excel Interop ?

  • But there was some error or simply not shown the value as wanted?

  • error-free, with the same scientific notation value.

  • In my case Numberformat also did not auto complete, but I put it anyway, the version of the Interop I used is Microsoft Excel 15.0 Object Library (1.8.0.0)

  • Puts very strange, can the way in which I create the datagrid interfere, because I create the date through a datamember, I am doing as follows, I receive several xml of queries, from there pick all and together in one file, tract tags etc. then display by datamember assigning the Node root I created.

  • I think not, because the value to be assigned in the cell comes from the datagridview cell as string. I have linked the grid with a<T> list and it is working normally. You have debugged to see if you are entering the line containing the format statement?

  • Yes I will debug, until I tested these examples of formatting of this site http://csharp.net-informations.com/excel/csharp-format-excel.htm

  • This was done in the same way as I did http://stackoverflow.com/questions/2067926/format-an-excel-column-or-cell-as-text-in-c

  • 1

    I managed to find a very simple form rsrsrsrsrs, with its help and a String variable that plays the values and this variable in the cell ! value = Cell.Value.Tostring(); Worksheet.Cells[i + 1, j + 1]. Numberformat = "@"; Worksheet.Cells[i + 1, j + 1] = value.Tostring();

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.