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();
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?
– Paulo Balbino
yes, the first and seventh columns, both have values that are being converted into scientific notation.
– bp002
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.
– Paulo Balbino