using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
if (System.IO.File.Exists(this.MapPath("\\userField\\empinfo.xls")))
{
System.IO.File.Delete(this.MapPath("\\userField\\empinfo.xls"));
}
wk.SaveAs(this.MapPath("\\userField\\empinfo.xls"), ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();
this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../userField/empinfo.xls');", true);