alonestarsz

博客园 首页 新随笔 联系 订阅 管理

   object missing = System.Reflection.Missing.Value;
   Excel.Application excel = new Excel.Application();
  
   excel.Visible = false;
   Excel.Workbooks oBooks = excel.Workbooks;
   //打开指定文件
   Excel.Workbook oBook = oBooks.Add(Server.MapPath("1-1.xls"));
   excel.DisplayAlerts = false;
   //写入数据
   for (int i = 10; i < 25; i++)
   {
    for (int j = 4; j < 17; j ++)
    {
     excel.Cells[i,j] = i + j;
    }
   }

   //保存文件
   oBook.SaveAs(Server.MapPath("test.xls"),missing,missing,missing,missing,missing,XlSaveAsAccessMode.xlExclusive,
    missing,missing,missing,missing,missing);
   excel.DisplayAlerts = true;

   //关闭进程,垃圾回收
   oBook.Close(missing,Server.MapPath("Book1.xls"),missing);
 
   if(oBook != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook);
   oBook = null;
   if(oBooks != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks);
   oBooks = null;
   if(excel != null)
   {
    excel.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
    excel = null;  
   }
   GC.Collect();

posted on 2004-10-20 09:45  天煞孤星  阅读(1349)  评论(1编辑  收藏  举报