Hi,
I don't know your requriemetns on which column to to be displayed, but there seems some flaws in your for loops.
On the first for loop
for (int i = 3; i < gv_gsk.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = gv_gsk.Columns[i - 1].HeaderText;
}
you are starting with column 3 and magically adding an extra ghost column to the count and then you are decrementing the column by 1 so actually you are starting from column 2. I believe you want to add the column 2 to the column 3 in your excel. In that case you could rewrite the code as follow:
for (int i = 2; i < gv_gsk.Columns.Count; i++)
{
Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[1, (i + 1)] as Range;
range.Value2 = gv_gsk.Columns[i].HeaderText;
}
So here you are starting from the second column that you wanted to exported to the third column in Excel.
And on your third for loop:
for (int i = 0; i < gv_gsk.Rows.Count; i++)
{
for (int j = 2; j < gv_gsk.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = gv_gsk.Rows[i].Cells[j].Value.ToString();
}
}
For each row, now you are starting the column at 2, which is correct. Then on the excel you are inserting then in the wrong place. you want add the data row under the heading that is i + 1 not 2.
for (int i = 0; i < gv_gsk.Rows.Count; i++)
{
for (int j = 2; j < gv_gsk.Columns.Count; j++)
{
Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[(i + 1), (j + 1)] as Range;
range.Value2 = gv_gsk.Rows[i].Cells[j].Value.ToString();
}
}
Now your column header and the values should match in your excel as well as you are not adding an extra row between the header and the data.
I hope this helps.
Regards
Jegan