在ASP.NET中使用EXCEL之三 写Excel文件

- 中国WEB开发者网络 (http://www.webasp.net)
-- 技术教程 (http://www.webasp.net/article/)
--- 在ASP.NET中使用EXCEL之三 写Excel文件 (http://www.webasp.net/article/27/26032.htm)
-- 作者:未知
-- 发布日期: 2005-08-23
 

在按钮的click事件中输入如下代码,即可以实现写入Excel文件。

写Excel文件时,还要把项目文件夹的权限进行设置,对iuser_machine用户有可写的权限。

private void Button1_Click(object sender, System.EventArgs e)
  {string filename="";
   Excel.ApplicationClass oExcel;
   oExcel = new Excel.ApplicationClass();
   oExcel.UserControl = false;
   Excel.WorkbookClass wb = (Excel.WorkbookClass) oExcel.Workbooks.Add(System.Reflection.Missing.Value);
   for(int i = 1;i <= 5; i++)
   {
    oExcel.Cells[i,1]=i.ToString();
    oExcel.Cells[i,2]="'第2列";
    oExcel.Cells[i,3]="'第3列";
    oExcel.Cells[i,4]="'第4列";
   }  
   wb.Saved = true;
   filename= Request.PhysicalApplicationPath +  "test.xls";
   oExcel.ActiveWorkbook.SaveCopyAs(filename);
   oExcel.Quit();
   System.GC.Collect();
   Response.Redirect( Request.ApplicationPath +  "/test.xls");
}
  


webasp.net