Datagridview copy cols values into excel sheet

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im able to create a XLS file from the below code. I got a problem here,<br/>
<br/>
1. There are 4 columns in the datagridview, the fourth com is not copying normally. I have to move the cursor/tab to another cell or click somewhere in the GUI to copy all the 4 cols. If not the 4th col is not copying.<br/>
<pre class="prettyprint for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[6, i] = dataGridView1.Columns[i - 1].HeaderText;
}



// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 7, j + 1] = dataGridView1.Rows.Cells[j].Value.ToString();
}
}
[/code]
<br/>
<br/>
Thank you very much.

View the full article
 
Back
Top