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

80酷酷网    80kuku.com

  asp.net|excel 

在按钮的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");
}
  

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: