.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
操作EXCEL文件成功释放COM对象的代码

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    Excel.Application app = new Excel.ApplicationClass();
    Excel.Workbooks  workbooks = app.Workbooks;
    Excel._Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    Excel.Sheets sheets = workbook.Sheets;
    Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);
    Excel.Range  range = worksheet.Cells;
   for (int i = 1; i <= ds.Tables[0].Columns.Count; i++)
   {
    range[1,i] = ds.Tables[0].Columns[i-1].ColumnName;
   }

   for(int n = 0 ; n < ds.Tables[0].Rows.Count; n++)
   {
    for(int m = 1; m <= ds.Tables[0].Columns.Count; m++)
    {
     range[n+2,m] = ds.Tables[0].Rows[n][m-1].ToString();
    }
   }
    
   if(System.IO.File.Exists("d:\\excel.xls"))
   {
    System.IO.File.Delete("d:\\excel.xls");
   }
    worksheet.SaveAs("d:\\excel.xls",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    //app.Visible = true;
    
    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
    range = null;
    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
   worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
   sheets = null;
   workbook.Close(false,Missing.Value,Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
   workbook = null;
   workbooks.Close();
   System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
   workbooks = null;
   app.Quit();
   System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
   app = null;




相关文章

相关软件