程序如下:private void Button1_Click(object sender, System.EventArgs e)
{
System.Data.DataTable dt = (System.Data.DataTable)Session["List"];
DataSet ds = new DataSet();
ds.Merge(dt);
exporttoExcel(ds);
} protected void exporttoExcel(DataSet ExelDt)
{
int colIndex=1,rowIndex=1;
Excel.Application excel;

try
{
excel=new Excel.Application(); 
excel.Application.Workbooks.Add (true) ;
excel.Visible = true;
}
catch(Exception ae)
{
return;
} foreach(DataColumn col in ExelDt.Tables[0].Columns)
{
excel.Cells[1,colIndex]=col.ColumnName;
colIndex++; 
}
try
{
foreach(DataRow row in ExelDt.Tables[0].Rows) 

rowIndex++; 
colIndex=0; 
foreach(DataColumn col in ExelDt.Tables[0].Columns) 

colIndex++;
if(colIndex ==1)
{
excel.Cells[rowIndex,colIndex]="'"+row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}

}
}
catch(System.Exception)
{
Response.Write("<script>alert('输出Excel有错误,请确认没有关闭Excel!')</script>");
return;
}
}