Search This Blog

Showing posts with label Write into excel. Show all posts
Showing posts with label Write into excel. Show all posts

Tuesday, September 28, 2010

Writing into an excel file

You need to add reference to  Com component - Microsoft.excel.

Excel.ApplicationClass app = new Excel.ApplicationClass();

Excel.Workbook wk = app.Workbooks.Open("F:\\MyExcel.xls", 0, false, 5,"", "", true, Excel.XlPlatform.xlWindows, "\t", true, false,0, true,true,false);

Excel.Sheets sheets = wk.Worksheets;

Excel.Worksheet sheet1 = (Excel.Worksheet)sheets.get_Item(1);

try{
for (int i = 1; i <= dtEmployee.Rows.Count; i++)  //dtEmployee is a datatable containing data
{
        for (int j = 1; j <= dtEmployee.Columns.Count; j++)
         {
                sheet1.Cells[i, j] = dtEmployee.Rows[i-1][j-1];
          }
}
}

catch{
}

finally{

app.Workbooks.Close();
app.Quit();
}