Save Info of Datagridview
How can I save the infor of the datagridView in an excel template?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Arnold PetronaPosted Sep 17, 2008, 5:08 PM
Ok, question about your code for understanding it :
Application oExcel = new Application();
workbook oWorkbook = (workbook)oExcel.Worlbooks.add();
Sheet oSheet = (Sheet)oWorkbook.ActiveSheet;
int i=0,j=0;
foreach(DatagridviewRow row in yourdatagridview)
{
foreach(DatagridviewColumn column in yourdatagridview)
{
oSheet.cells[i,j] = yourdatagridview[i,j].value;
i+=1;
}
j=+1;
}
Where the word Workbook and Sheet come from?
Bechir BejaouiPosted Jul 26, 2008, 1:30 PM
add a reference for a COM component called "Microsoft Excel 9.0 object library".
you can also import the excel library to using tlbimp.exe tool to import the excel 9.0 library
then
Application oExcel = new Application();
workbook oWorkbook = (workbook)oExcel.Worlbooks.add();
Sheet oSheet = (Sheet)oWorkbook.ActiveSheet;
int i=0,j=0;
foreach(DatagridviewRow row in yourdatagridview)
{
foreach(DatagridviewColumn column in yourdatagridview)
{
oSheet.cells[i,j] = yourdatagridview[i,j].value;
i+=1;
}
j=+1;
}