Export data to MS Excel (.Net) (C#) – Windows Application

April 20, 2010

First of all, add Reference:

Select COM tab and select Microsoft.Office.Interop.Excel

Then write the below code:

Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

ExcelApp.Workbooks.Add(true);

int columnIndex = 0;

foreach (DataColumn col in Ds.Tables[0].Columns)
{
columnIndex++;
ExcelApp.Cells[1, columnIndex] = col.ColumnName;
}

int rowIndex = 0;
foreach (DataRow row in Ds.Tables[0].Rows)
{
rowIndex++;
columnIndex = 0;
foreach (DataColumn col in Ds.Tables[0].Columns)
{
columnIndex++;
ExcelApp.Cells[rowIndex + 1, columnIndex] = row[col.ColumnName].ToString();
}
}
ExcelApp.Visible = true;

//To show excel as minimized window

ExcelApp.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;

Thanks—-

<Write any comments>

Hello world!

February 4, 2010

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!


Follow

Get every new post delivered to your Inbox.