C#中对Excel的操作---需要注意的几点

来源:互联网 发布:r数据的导入和导出 编辑:程序博客网 时间:2024/05/16 10:51

C#中对Excel的操作相关的说明已经很多了,本文只做简单的步骤说明:

1、添加COM引用:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library。

2、使用Workbooks,Workbook,Worksheets,Worksheet等对象实例完成功能实现。

3、释放资源。---但在此处需要着重注意,当程序调用Excel时,会生成一个Excel.exe进程(在任务管理器中可以看到),如果处理不当,则在程序关闭之前会该进程会一直存在。并且如果由于代码逻辑的需要,每次调用一个Excel实例,就会生成一个Excel.exe进程,那么就会出现越来越多的Excel.exe进程,一定程度上会大量浪费计算机资源。所以,针对这种情况,最好要在Excel实例使用完毕时进行合理的资源释放,将Excel.exe进程杀掉。网上类似的讨论很多,我实际测试了一下,有一种方式可以确实有效的将Excel.exe进程杀掉:那就是GC.Collect(),也就是强制进行垃圾回收。但是它的使用也有技巧,不要将GC.Collect()与Excel的调用放在一起,否则也许会无法杀掉Excel.exe进程。

以下是我写的一个简单的demo:

  1. //类实现
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Excel;
  6. using Microsoft.Office.Core;
  7. namespace XXX
  8. {
  9.     class ExcelCtrl
  10.     {
  11.         private ApplicationClass m_Excel;
  12.         private Workbook m_Book;
  13.         private Worksheet m_Sheet;
  14.         private Range m_Range;
  15.         public int m_RowsCount;
  16.         public ExcelCtrl(string ExcelFile, string ExcelSheet)
  17.         {
  18.             try
  19.             {
  20.                 m_Excel = new ApplicationClass();
  21.                 m_Book = m_Excel.Workbooks.Open(ExcelFile,
  22.                 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  23.                 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  24.                 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  25.                 Type.Missing, Type.Missing);
  26.                 m_Sheet = (Worksheet)m_Book.Worksheets[ExcelSheet];
  27.                 m_RowsCount = m_Sheet.UsedRange.Cells.Rows.Count;
  28.             }
  29.             catch (System.Exception ex)
  30.             {
  31.                 throw ex;
  32.             }
  33.         }
  34.         public string ReadShell(string ShellPos)
  35.         {
  36.             try
  37.             {
  38.                 m_Range = m_Sheet.get_Range(ShellPos, Type.Missing);
  39.                 return m_Range.Text.ToString().Trim();
  40.             }
  41.             catch (System.Exception ex)
  42.             {
  43.                 throw ex;
  44.             }
  45.         }
  46.         public void Dispose()
  47.         {
  48.             try
  49.             {
  50.                 m_Book.Close(Type.Missing, Type.Missing, Type.Missing);
  51.                 m_Excel.Quit();
  52.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
  53.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Book);
  54.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Sheet);
  55.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Range);
  56.                 m_Excel = null;
  57.                 m_Book = null;
  58.                 m_Sheet = null;
  59.                 m_Range = null;
  60.             }
  61.             catch (System.Exception ex)
  62.             {
  63.                 throw ex;
  64.             }
  65.         }
  66.     }
  67. }
  68. //类调用
  69. private void Button1_Click(object sender, EventArgs e)
  70. {
  71.     ExcelCtrl excel = null;
  72.     string strTest;
  73.     try
  74.     {
  75.         ExcelCtrl excel = new ExcelCtrl("test.xls""Sheet1");
  76.         try
  77.         {
  78.             strTest = excel.ReadShell("A1");
  79.         }
  80.         finally
  81.         {
  82.             excel.Dispose();
  83.         }
  84.     }
  85.     catch (System.Exception ex)
  86.     {
  87.         MessageBox.Show(ex.Message);
  88.         return;
  89.     }
  90.     finally
  91.     {
  92.         GC.Collect();
  93.     }
  94. }
原创粉丝点击