Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
By using below code, it always inserts data into starting cell. I can generate excel file but I need to insert the data into different cells starting from H5 or I9 cell. Can anyone please look into this.

Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();

// single worksheet
Excel._Worksheet workSheet = excelApp.ActiveSheet;

// column headings
for (int i = 0; i < dt.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = dt.Columns[i].ColumnName;
}

// rows
for (int i = 0; i < dt.Rows.Count; i++)
{
// to do: format datetime values before printing
for (int j = 0; j < dt.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = dt.Rows[i][j];
}
}
excelApp.Visible = true;
Posted

1 solution

In the code you use, the value 1 is your offset.
workSheet.Cells[1, (i + 1)] = dt.Columns[i].ColumnName;

So you can simply manipulate this value to starty at the desired cells.

Goo luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900