Quote:
Using this code i am not able to get first record in excel sheet
You are not getting the first record because your loop starts at 1 as shown in the following:
for (int j = 1; j < dt.Columns.Count + 1; j++)
and then you have the following code inside it:
ExcelApp.Cells[i, j] = dcc[j - 1].ToString();
Keep in mind that index of
Cells
starts at
0
so I think that causes the issue why you don't see the first record. Try something like this instead:
if (i == 1)
{
ExcelApp.Cells[i-1, j-1] = dcc[j - 1].ToString();
}
else { ExcelApp.Cells[i-1, j-1] = dt.Rows[i - 1][j - 1].ToString();
}
Or you could change your loop to start at 0 instead of 1 so your code would now look something like this:
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (i == 0)
{
ExcelApp.Cells[i, j] = dcc[j].ToString();
}
else { ExcelApp.Cells[i, j] = dt.Rows[i][j].ToString();
}
}
}
If you are still having an unexpected results, I would suggest you to debug your code, set a break point to your for-loop code and then step into it to figure out what the data is being transported.