在ASP_NET中写Excel文件(调试通过)

private void writeButton_Click(object sender, System.EventArgs e)
  {//写Xls
   //首先将做作Excel的COM引用到工程里
   //写Excel文件时,还要把项目文件夹的权限进行设置,对iuser_machine用户有可写的权限
   //或者设置web.config<identity impersonate="true" userName="管理员用户名"password="密码"/>
   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.Write("写入成功");
   //Response.Redirect( Request.ApplicationPath +  "/test.xls");
  }

你可能感兴趣的:(在ASP_NET中写Excel文件(调试通过))