LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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);


}
}


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print of DataGridView on worksheet is always blank Mark Excel Programming 0 October 8th 08 10:33 PM
Winform Exporting Data to Excel form Datagridview Nischal Excel Programming 0 November 28th 07 07:16 PM
Converting Excel Cell Location into X and Y Coordinates [email protected] Excel Programming 3 June 7th 07 07:54 AM
Converting Text files to Excel sheets with specific formatting BamaBrad Excel Programming 4 September 21st 05 08:36 PM
Converting an object in Excel into an actual cell astronautambo Excel Discussion (Misc queries) 0 September 7th 05 06:51 PM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"