![]() |
Cell formatting when converting from DataGridView to Excel
Hi
I need to convert data from Data Grid View to Excel. The below code is doing that successfully but my problem is I have few columns which have a very long number (16 characters). When the data is converting until 15th digit the numbers are correct but the 16th one always convert to zero. I feel I need to format that cell as "Text" so that it treats the data as Character instead of Numeric. Can any one help me how to do it. My Code: ======= try { // Get the class type and instantiate Excel. Type objClassType; objClassType = Type.GetTypeFromProgID("Excel.Application"); objApp_Late = Activator.CreateInstance(objClassType); //Get the workbooks collection. objBooks_Late = objApp_Late.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, objApp_Late, null); //Add a new workbook. objBook_Late = objBooks_Late.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objBooks_Late, null); //Get the worksheets collection. objSheets_Late = objBook_Late.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, objBook_Late, null); //Get the first worksheet. Parameters = new Object[1]; Parameters[0] = 1; objSheet_Late = objSheets_Late.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, objSheets_Late, Parameters); // Now add the data from the grid to the sheet for (i = 0; i < datagridview.RowCount - 1; i++) { for (c = 0; c < datagridview.ColumnCount; c++) { //Get a range object that contains cell. Parameters = new Object[2]; Parameters[0] = columns[c] + Convert.ToString(i + 2); Parameters[1] = Missing.Value; objRange_Late = objSheet_Late.GetType().InvokeMember("Range", BindingFlags.GetProperty, null, objSheet_Late, Parameters); //Write Headers in cell. Parameters = new Object[1]; Parameters[0] = datagridview.Rows[i].Cells[headers[c]].Value.ToString(); objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, objRange_Late, Parameters); } } |
All times are GMT +1. The time now is 09:55 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com