Hide Expand
Copy Code
Copy CodeCode
string connectionstring = null;Code
string sql = null;Code
string data = null;Code
int i = 0;Code
int j = 0;Code
Microsoft.Office.Interop.Excel.Application xlApp;Code
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;Code
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;Code
object misValue = System.Reflection.Missing.Value;Code
Code
xlApp = new Microsoft.Office.Interop.Excel.Application();Code
xlWorkBook = xlApp.Workbooks.Add(misValue);Code
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);Code
connectionstring = "Data Source=(local);Initial Catalog=Report_Database;integrated security=True";Code
cnn = new SqlConnection(connectionstring);Code
cnn.Open();Code
sql = "SELECT * FROM Report_Tab";Code
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);Code
DataSet ds = new DataSet();Code
dscmd.Fill(ds);Code
for (i = 0; i 0].Rows.Count - 1; i++)Code
{Code
for (j = 0; j 0].Columns.Count - 1; j++) {Code
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();Code
xlWorkSheet.Cells[i + 1, j + 1] = data;Code
}Code
}Code
xlApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\sajid\\Desktop\\test.xls");Code
xlApp.ActiveWorkbook.Saved = true;Code
xlApp.Quit();Code
releaseObject(xlWorkSheet);Code
releaseObject(xlWorkBook);Code
releaseObject(xlApp);Code
MessageBox.Show("Excel file created , you can find the file D:\\Sam-informations.xls");

Answers
Know the answer? Post it. Somebody with the same question will find it here.