需要引用com:MS Excel 10.0 Object LIbray (.net带的),例子如下:
在客户IE生成EXCEL
string SQL_excel="select * from files ;

Response.ContentType = "application/octet-stream";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment;filename=result.xls");
SqlConnection conn=new SqlConnection(Application["conn"].ToString());
SqlDataAdapter excel_da=new SqlDataAdapter(SQL_excel,conn);
DataSet excel_ds=new DataSet("files");
excel_da.Fill(excel_ds);
DataTable excel_dt=excel_ds.Tables[0];
DataGrid1.DataSource=excel_ds;
DataGrid1.DataBind();
conn.Close();
this.EnableViewState = false;
StringWriter tw=new StringWriter();
HtmlTextWriter hw=new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
在服务器生成excel
int rowIndex=1;
int colIndex=0;
Excel.Application excel= new Excel.Application(); 
try
{
excel.Application.Workbooks.Add(true);
excel.Visible=true;
}
catch
{
Response.Write("您可能没有安装OFFICE!");
return ;
}
foreach(DataRow row in excel_dt.Rows)
{
Response.Write("do");
rowIndex++;
colIndex=0;
foreach(DataColumn col in excel_dt.Columns) 
{
Response.Write("go");
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();

}
}
excel.Visible=true;

解决方案 »

  1.   

    请在详细点
    是添加COM,还是添加using
      

  2.   

    没找到MS Excel 10.0 Object LIbray 啊!
    但有MS Office 10.0 Object LIbray !
    是这个吗?
      

  3.   

    是在添加COM里的!
    不是Office的,是Excel,你仔细找找,有的,你装的是OFFICEXP吗?
      

  4.   

    我没安装Office,用的是.NET2002,操作系统是2000
    是在添加引用里选择添加引用中的com中找吗?
      

  5.   

    添加COM的,添加了之后不用引用了
    Office2000是Excel 9.0
    OfficeXP是Excel 10.0
    Excel.Application  App = new Excel.Application();
      

  6.   

    哦!
    原来必须要安装OFFICE啊!
    谢了!