C#中.net对Excel进行开发的知识点

5 篇文章 0 订阅
2 篇文章 0 订阅

如何针对Excel编程,网上收集了一些资料,最后决定采用Microsoft Office自带的COM组件来进行开发,因为感觉这个用起来比较简单。

        首先当然是引用usingMicrosoft.Office.Interop.Excel;这个组件里面有四个关键的对象,由大到小分便是Application,Workbook,Worksheet和Range。对于Excel的操作基本上是引用这些对象的方法和属性,而且操作起来简单易懂,下面分别介绍一下每一个对象的一些功能和基本用法,涉及得可能不够全面。


       Application对象代表 Excel 应用程序本身。主要的属性都是控制一些全局的属性,比如状态(cursor,EditDirectlyInCell),显示(DisplayAlerts,DisplayFullScreen)和Excel里面一些元素(Workbooks,Sheets)的控制等。其中最关键的就是和Workbooks属性的交互,使我们可以打开,新建工作簿并进行一步的操作。

       打开一个现有的工作簿,使用Workbooks集合的Open方法,其中Open的方法提供大量的可选参数,一般情况下我们只要都不需要用到。如下面所示:

[csharp]  view plain  copy
  1. Excel.Workbook wb = ThisApplication.Workbooks.Open( filepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  

      若要引用制定的工作簿,可以一工作簿名作为索引,或者按序列也可以,譬如:

[csharp]  view plain  copy
  1. Excel.Workbook wb = ThisApplication.Workbooks[1];  
  2. // 保存前  
  3. wb = ThisApplication.Workbooks["Book1"];  
  4. // 保存后  
  5. wb = ThisApplication.Workbooks["Book1.xls"];  

       当然打开了就要关闭拉,所以一个重要的方法就是Quit,当我们操作完之后要执行

[csharp]  view plain  copy
  1. ThisApplication.Quit();  

如果您将 DisplayAlerts属性设置为False,则系统不会提示您保存任何未保存的数据。此外,如果您将Workbook的Saved属性设置为True,则不管您有没有进行更改,Excel 都不会提示您保存它。

        Workbook类代表了 Excel 应用程序内的一个单一的工作簿。这里关键的一个用法是Workbook类提供了一个Sheets属性,它返回一个Sheets对象。这个对象包含Sheet对象集合,其中每个对象既可以是Worksheet对象,也可以是Chart对象。

        而且通过Workbook类的BuiltInDocumentProperties属性来使用内置属性,并通过CustomDocumentProperties属性来使用自定义属性。这些属性都返回一个DocumentProperties对象,它是DocumentProperty对象的一个集合。通过集合内的名称或者索引可以使用集合的Item属性来检索特定的属性。 另外还可以使用Workbook 对象的 Styles 属性来与工作簿交互,并对工作簿内的范围应用样式,譬如单元格格式等样式修改。
        我们经常会用到的主要如下面所示:

   Activate方法激活一个工作簿,并且选择工作簿中的第一个工作表:

[csharp]  view plain  copy
  1. ThisApplication.Workbooks[1].Activate;  

   Close方法关闭一个指定的工作簿,并且(可选)指定是否保存修改。如果工作簿从未保存过,则可以指定一个文件名。下面的代码片段关闭工作簿,并且不保存修改:

[csharp]  view plain  copy
  1. ThisApplication.Workbooks(1).Close(false,Type.Missing, Type.Missing);  

   Protect和Unprotect 方法允许您保护一个工作簿,从而不能添加或者删除工作表,以及再次取消保护工作簿。

[csharp]  view plain  copy
  1. ThisApplication.Workbooks[1].Protect(GetPasswordFromUser(), Type.Missing, Type.Missing);  

   Save方法保存工作簿。如果您还未保存过工作簿,则应该调用SaveAs方法,这样您可以指定一个路径(如果还未保存过工作簿,Excel 会将其保存在当前文件夹中,并以创建工作簿时所给的名称命名): 

[csharp]  view plain  copy
  1. wb.Save();  

   SaveAs方法要比Save方法复杂的多。这个方法允许您保存指定的工作簿,并且指定名称、文件格式、密码、访问模式和其他更多的选项(可选)。

[csharp]  view plain  copy
  1. ThisApplication.ActiveWorkbook.SaveAs("C:\\MyWorkbook.xml",Excel.XlFileFormat.xlXMLSpreadsheet,Type.Missing,  Type.Missing, Type.Missing,Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing);  

    提示由于保存成某些格式需要一些交互,您也许想在调用 SaveAs 方法之前将 Application.DisplayAlerts 属性设置成 False。例如,在将一个工作表保存成 XML 格式时,Excel 会提醒您不能随工作簿保存 VBA 项目。如果将 DisplayAlerts 属性设置成 False,就不会出现这种警告。

   SaveCopyAs 方法将工作簿的一个副本保存到文件中,但不会修改在内存中打开的工作簿。当您想创建工作簿的备份,同时不修改工作簿的位置时,这个方法非常有用:

[csharp]  view plain  copy
  1. ThisApplication.ActiveWorkbook.SaveCopyAs("C:\\Test.xls");  

    警告 交互式地取消任何保存或者复制工作簿的方法会在您的代码中触发一个运行时异常。例如,如果您的过程调用 SaveAs 方法,但是没有禁用 Excel 的提示功能,并且您的用户在提示后单击“取消”,则 Excel 会将运行时错误返回给代码。

    

    Worksheet对象就是对一个工作表进行操作的类,也就是对一个sheet进行各种设置,但是这里有很多方法和前面提到的Application或者Workbook相似或者相同。这里有个比较重要的就是对批注的操作,不过这个功能没有怎么用,所以在这里也不作介绍。对于在Worksheet里面用得比较多的其实就是获取我想要操作的范围,然后调用Range类来对工作表的制定范围进行操作。

    获取工作表的范围一般有两种方法,一种是

[csharp]  view plain  copy
  1. Range range = WorkSheet.get_Range("A1","V1");  

    其中A1就是第A列第1行,这个相信大家都比较熟悉。第一个参数代表起始地址,第二个参数表示结束地址。就像我们画矩形一样,两个对角的坐标确定了就可以确定一个矩形范围。又或者可以是

[csharp]  view plain  copy
  1. Range range = WorkSheet.get_Range("A1:V1",Type,Missing);  

    若然第一个参数只为一个A1,那么就是针对一个单元格进行操作。另外一种方法是

[csharp]  view plain  copy
  1. Range range = WorkSheet.Range[WorkSheet.Cells[1, 7], WorkSheet.Cells[1,8]];  

其中WorkSheet.Cells[1, 7]中也和上面的A1一个意思,只不过Cells的下标可以像数组一样操作,更加灵活。如果两个WorkSheet.Cells[x,y]一样,那就表明操作一个单元格。


    Range对象是我们在 Excel 应用程序中最经常使用的对象;在您可以操作 Excel 内的任何区域之前,您需要将其表示为一个Range对象,然后使用该Range对象的方法和属性。Range类是很重要的,目前为止,本篇文章中的每个示例中在某种程度上都使用了一个Range对象。基本上来说,一个Range对象代表一个单元格、一行、一列、包含一个或者更多单元块(可以是连续的单元格,也可以式不连续的单元格)的选定单元格,甚至是多个工作表上的一组单元格。

    当我们获得一个制定的范围之后(也就是我们获取到range),那么我们就可以针对这个对象进行操作。这个对象的属性让我们可以设置字体,行高行宽,颜色,背景还有对齐方式等等我们日常的操作,例子如下

[csharp]  view plain  copy
  1. range.ColumnWidth= 40; //设置列宽  
  2. range.HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平居中  
  3. range.VerticalAlignment = XlHAlign.xlHAlignCenter;//垂直居中  
  4. range.Borders.LineStyle = 1;//设置边框  
  5. range.Font.Size = 10; //设置字体大小  
  6. range.RowHeight = 35; //设置行高  
  7. range.Font.Bold = true//设置字体样式  
  8. range.Font.Color = 38;   //设置字体颜色  
  9. range.Interior.ColorIndex =15;   //设置背景颜色  

    其实使用起来很简单,很多属性的字眼都是一看就知道表达的是什么意思,然后按照提示设置就OK了。当然,还有一些自动调整的函数,譬如可以使用range.EntireColumn.AutoFit();让列宽按照内容进行自动调整,或者AutoFill()进行 自动填充,还有其它方法在此就不作介绍了。

    

    本文所提到的都是在开发过程中用到的一些皮毛,主要是希望对之前的工作来一个总结,也是自己进步的一个纪念。具体可以参考Microsoft主页提供的技术文档,里面有更加详细的介绍和例子。下面附上项目开发中封装的Excel操作:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Text;  
  3. using System.Globalization;  
  4. using Microsoft.Office.Interop.Excel;  
  5. using System.Threading;  
  6. using System.Collections;  
  7. using System.Diagnostics;  
  8. using System.IO;  
  9. using System.Reflection;  
  10. using System.Runtime.InteropServices;  
  11. using System.Drawing;  
  12.   
  13. namespace uiExceller  
  14. {  
  15.     /// <summary>  
  16.     /// <para>封装对Excel的操作</para>  
  17.     /// </summary>  
  18.     public class ExcelManager : IDisposable  
  19.     {  
  20.         ApplicationClass App;  
  21.   
  22.         CultureInfo OriginalCulture;  
  23.   
  24.         private string _OpenFileName;  
  25.         /// <summary>  
  26.         /// 当前打开的文件名  
  27.         /// </summary>  
  28.         public string OpenFileName  
  29.         {  
  30.             get { return _OpenFileName; }  
  31.         }  
  32.   
  33.         /// <summary>  
  34.         /// 返回一个bool值确定当前的文件状态  
  35.         /// </summary>  
  36.         public bool AnyFileOpen  
  37.         {  
  38.             // After opening a file, we assign its name to _OpenedFileName. After closing,  
  39.             // we clear _OpenedFileName by assigning String.Empty to it.  
  40.             // So a String.Empty value shows no file is open  
  41.             get { return !String.IsNullOrEmpty(_OpenFileName); }  
  42.         }  
  43.   
  44.         private static ExcelManager instance = null;  
  45.         private static readonly object excellock = new object();  
  46.   
  47.         public static ExcelManager Instance  
  48.         {  
  49.             get  
  50.             {  
  51.                 lock (excellock)  
  52.                 {  
  53.                     if (instance == null)  
  54.                     {  
  55.                         instance = new ExcelManager();  
  56.                     }  
  57.                     return instance;  
  58.                 }  
  59.             }  
  60.         }  
  61.   
  62.         /// <summary>  
  63.         /// 默认构造函数.  
  64.         /// </summary>  
  65.         /// <remarks>  
  66.         /// <code>  
  67.         /// using(ExcelManager em = new ExcelManager())  
  68.         /// {  
  69.         ///     // codes using excel file go here, for example:  
  70.         ///     em.Open(filename);  
  71.         /// }  
  72.         /// </code>  
  73.         /// </example>  
  74.         /// <b>Note:</b> This Constructor changes current thread's culture to "en-US" and returns it to   
  75.         /// previous state after dispose.  
  76.         /// </remarks>  
[csharp]  view plain  copy
  1.     public ExcelManager()  
  2.     {  
  3.         OriginalCulture = Thread.CurrentThread.CurrentCulture;  
  4.         Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");  
  5.         //Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");  
  6.   
  7.         App = new ApplicationClass();  
  8.         App.DisplayAlerts = false;  
  9.     }  
  10.  
  11.     #region Functions to work with Files (workbooks)  
  12.   
  13.     /// <summary>  
  14.     ///新建一个Excel文件.  
  15.     /// </summary>  
  16.     /// <param name="fileName">生成的文件名</param>  
  17.     public void Create(string fileName)  
  18.     {  
  19.         try  
  20.         {  
  21.               
  22.             Close();  
  23.             App.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);  
  24.             App.ActiveWorkbook.SaveAs(fileName,  
  25.                                       XlFileFormat.xlWorkbookNormal,  
  26.                                       System.Type.Missing,  
  27.                                       System.Type.Missing,  
  28.                                       System.Type.Missing,  
  29.                                       System.Type.Missing,  
  30.                                       XlSaveAsAccessMode.xlNoChange,  
  31.                                       System.Type.Missing,  
  32.                                       System.Type.Missing,  
  33.                                       System.Type.Missing,  
  34.                                       System.Type.Missing,  
  35.                                       System.Type.Missing);  
  36.             _OpenFileName = fileName;  
  37.         }  
  38.         catch (Exception err)  
  39.         {  
  40.             throw new ExcelException(  
  41.                 String.Format(CultureInfo.InvariantCulture, "Error Creating File '{0}'", fileName), err);  
  42.         }  
  43.     }  
  44.   
  45.     /// <summary>  
  46.     /// 新建一个工作表.  
  47.     /// </summary>  
  48.     /// <param name="NewSheetName"></param>  
  49.     public void CreateSheet(string NewSheetName)  
  50.     {  
  51.         if (!AnyFileOpen)  
  52.             throw new ExcelException("No file is Open");  
  53.   
  54.         try  
  55.         {  
  56.             ((_Worksheet)App.Worksheets.Add(Type.Missing, App.Worksheets[App.Worksheets.Count], Type.Missing, Type.Missing)).Name = NewSheetName;  
  57.               
  58.         }  
  59.         catch (Exception err)  
  60.         {  
  61.             throw new ExcelException(  
  62.                 String.Format(CultureInfo.InvariantCulture, "Can not activate sheet '{0}'", NewSheetName), err);  
  63.         }  
  64.     }  
  65.       
  66.     /// <summary>  
  67.     /// 删除一个工作表.  
  68.     /// </summary>  
  69.     /// <param name=SheetName"></param>  
  70.     public void deleteSheet(string SheetName)  
  71.     {  
  72.         try  
  73.         {  
  74.             _Worksheet Sheet = (_Worksheet)App.Worksheets.get_Item(SheetName);  
  75.             App.DisplayAlerts = false;  
  76.             Sheet.Delete();  
  77.         }  
  78.         catch (Exception err)  
  79.         {  
  80.             throw new ExcelException(  
  81.                 String.Format(CultureInfo.InvariantCulture, "Can not delete sheet '{0}'", SheetName), err);  
  82.         }  
  83.     }  
  84.   
  85.     /// <summary>  
  86.     ///打开一个Excel文件.  
  87.     /// </summary>  
  88.     /// <param name="fileName">打开的文件名</param>  
  89.     public void Open(string fileName)  
  90.     {  
  91.         try  
  92.         {  
  93.             Close();  
  94.             Workbook wk = App.Workbooks.Open(fileName,  
  95.                                 false,  
  96.                                 false,  
  97.                                 System.Type.Missing,  
  98.                                 System.Type.Missing,  
  99.                                 System.Type.Missing,  
  100.                                 System.Type.Missing,  
  101.                                 System.Type.Missing,  
  102.                                 System.Type.Missing,  
  103.                                 false,  
  104.                                 System.Type.Missing,  
  105.                                 System.Type.Missing,  
  106.                                 false,  
  107.                                 System.Type.Missing,  
  108.                                 System.Type.Missing);  
  109.             _OpenFileName = fileName;  
  110.   
  111.             if (wk.ReadOnly)  
  112.                 throw new ExcelException(  
  113.                 String.Format(CultureInfo.InvariantCulture, "Readonly"));  
  114.   
  115.         }  
  116.         catch (Exception err)  
  117.         {  
  118.             if (err.Message == "Readonly")  
  119.                 throw new ExcelException(  
  120.                 String.Format(CultureInfo.InvariantCulture, "'{0}' 文件已经打开,无法操作,请先手工关闭此文件!", fileName), err);  
  121.             else  
  122.                 throw new ExcelException(  
  123.                     String.Format(CultureInfo.InvariantCulture, "Error Opening File '{0}'", fileName), err);  
  124.         }  
  125.     }  
  126.   
  127.     /// <summary>  
  128.     /// 关闭打开的文件.  
  129.     /// </summary>  
  130.     public void Close()  
  131.     {  
  132.         try  
  133.         {  
  134.             if(App.Workbooks!=null)  App.Workbooks.Close();  
  135.         }  
  136.         catch (Exception err)  
  137.         {  
  138.             throw new ExcelException(  
  139.                 String.Format(CultureInfo.InvariantCulture, "Error Closing File '{0}'", _OpenFileName), err);  
  140.         }  
  141.     }  
  142.   
  143.   
  144.     /// <summary>  
  145.     ///保存文件  
  146.     /// </summary>  
  147.     public void Save()  
  148.     {  
  149.         if (!AnyFileOpen)  
  150.             throw new ExcelException("No file is Open");  
  151.   
  152.         App.ActiveWorkbook.Save();  
  153.     }  
  154.   
  155.     /// <summary>  
  156.     /// 另存文件  
  157.     /// </summary>  
  158.     /// <param name="newFileName">新的文件名</param>  
  159.     public void SaveAs(string newFileName)  
  160.     {  
  161.         if (!AnyFileOpen)  
  162.             throw new ExcelException("No file is Open");  
  163.   
  164.         try  
  165.         {  
  166.             if (File.Exists(newFileName))  
  167.             {  
  168.                 File.Delete(newFileName);  
  169.             }  
  170.             App.ActiveWorkbook.SaveAs(newFileName,  
  171.                                     XlFileFormat.xlWorkbookNormal,  
  172.                                     System.Type.Missing,  
  173.                                     System.Type.Missing,  
  174.                                     System.Type.Missing,  
  175.                                     System.Type.Missing,  
  176.                                     XlSaveAsAccessMode.xlNoChange,  
  177.                                     System.Type.Missing,  
  178.                                     System.Type.Missing,  
  179.                                     System.Type.Missing,  
  180.                                     System.Type.Missing,  
  181.                                     System.Type.Missing);  
  182.   
  183.             _OpenFileName = newFileName;  
  184.         }  
  185.         catch (Exception err)  
  186.         {  
  187.             throw new ExcelException(  
  188.                 String.Format(CultureInfo.InvariantCulture, "Can not save file as '{0}'", newFileName), err);  
  189.         }  
  190.     }  
  191.  
  192.     #endregion  
  193.  
  194.     #region Functions to work with Worksheets  
  195.   
  196.     /// <summary>  
  197.     ///激活指定的工作表.  
  198.     /// </summary>  
  199.     /// <param name="sheetName">已有的工作表名</param>  
  200.     public void ActivateSheet(string sheetName)  
  201.     {  
  202.         if (!AnyFileOpen)  
  203.             throw new ExcelException("No file is Open");  
  204.   
  205.         try  
  206.         {  
  207.             foreach (_Worksheet wsheet in App.ActiveWorkbook.Sheets)  
  208.                 if (String.Compare(wsheet.Name, sheetName, true) == 0)  
  209.                 {  
  210.                     wsheet.Activate();  
  211.                     return;  
  212.                 }  
  213.             throw new ExcelException(String.Format("Can not find sheet '{0}'", sheetName));  
  214.         }  
  215.         catch (Exception err)  
  216.         {  
  217.             throw new ExcelException(  
  218.                 String.Format(CultureInfo.InvariantCulture, "Can not activate sheet '{0}'", sheetName), err);  
  219.         }  
  220.     }  
  221.   
  222.   
  223.     public void SetSheetPrntFormat(string SheetName, string PaperSize, double top, double left, double right, double bottom)  
  224.     {  
  225.         if (!AnyFileOpen)  
  226.             throw new ExcelException("No file is Open");  
  227.   
  228.         try  
  229.         {  
  230.             foreach (_Worksheet wsheet in App.Sheets)  
  231.             {  
  232.                 if (String.Compare(wsheet.Name, SheetName, true) == 0)  
  233.                 {  
  234.                     try  
  235.                     {  
  236.                         switch (PaperSize)  
  237.                         {  
  238.                             case "A3":  
  239.                                 wsheet.PageSetup.PaperSize = XlPaperSize.xlPaperA3;  
  240.                                 break;  
  241.                             case "A4":  
  242.                                 wsheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;  
  243.                                 break;  
  244.                         }  
  245.                     }  
  246.                     catch  
  247.                     {  
  248.                         throw new ExcelException(String.Format("打印机可能不支持 '{0}' 尺寸的纸张。", PaperSize));  
  249.                     }  
  250.                     wsheet.PageSetup.TopMargin = App.InchesToPoints(top / 2.54);  
  251.                     wsheet.PageSetup.LeftMargin = App.InchesToPoints(left / 2.54);  
  252.                     wsheet.PageSetup.RightMargin = App.InchesToPoints(right / 2.54);  
  253.                     wsheet.PageSetup.BottomMargin  = App.InchesToPoints(bottom / 2.54);  
  254.                     wsheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;  
  255.                     //wsheet.PageSetup.CenterHorizontally = true;  
  256.                     //wsheet.PageSetup.CenterVertically = true;  
  257.                     return;  
  258.                 }  
  259.             }  
  260.   
  261.             throw new ExcelException(String.Format("Can not find sheet '{0}'", SheetName));  
  262.         }  
  263.         catch (Exception err)  
  264.         {  
  265.             throw new ExcelException(  
  266.                 String.Format(CultureInfo.InvariantCulture, "Can not SetSheetPrntFormat sheet '{0}'"+err.Message, SheetName), err);  
  267.         }  
  268.     }  
  269.   
  270.     /// <summary>  
  271.     /// 重命名一个工作表.  
  272.     /// </summary>  
  273.     /// <param name="oldName">原来的名字</param>  
  274.     /// <param name="newName">新的名字</param>  
  275.     public void RenameSheet(string oldName, string newName)  
  276.     {  
  277.         if (!AnyFileOpen)  
  278.             throw new ExcelException("No file is Open");  
  279.   
  280.         try  
  281.         {  
  282.             foreach (_Worksheet wsheet in App.Sheets)  
  283.             {  
  284.                 if (String.Compare(wsheet.Name, oldName, true) == 0)  
  285.                 {  
  286.                     wsheet.Name = newName;  
  287.                     return;  
  288.                 }  
  289.             }  
  290.             throw new ExcelException(String.Format("Can not find sheet '{0}'", oldName));  
  291.         }  
  292.         catch (Exception err)  
  293.         {  
  294.             throw new ExcelException(  
  295.                 String.Format(CultureInfo.InvariantCulture, "Can not rename sheet '{0}' to '{1}'", oldName, newName), err);  
  296.         }  
  297.     }  
  298.   
  299.     /// <summary>  
  300.     ///重命名当前的工作表.  
  301.     /// </summary>  
  302.     /// <param name="newName"></param>  
  303.     public void RenameCurrentSheet(string newName)  
  304.     {  
  305.         if (!AnyFileOpen)  
  306.             throw new ExcelException("No file is Open");  
  307.   
  308.         try  
  309.         {  
  310.             (App.ActiveSheet as _Worksheet).Name = newName;  
  311.         }  
  312.         catch (Exception err)  
  313.         {  
  314.             throw new ExcelException(  
  315.                 "Can not rename current sheet", err);  
  316.         }  
  317.     }  
  318.     #endregion  
  319.  
  320.     #region Functions to work with cell and range values  
  321.   
  322.     public object GetValue(string cellAddress, Category category)  
  323.     {  
  324.         if (String.IsNullOrEmpty(cellAddress))  
  325.             throw new ArgumentNullException("cellAddress");  
  326.         if (!AnyFileOpen)  
  327.             throw new ExcelException("No file is Open");  
  328.   
  329.         try  
  330.         {  
  331.             Range range = (App.ActiveSheet as _Worksheet).get_Range(cellAddress, System.Type.Missing);  
  332.             if (category == Category.Numeric)  
  333.                 return range.Value2;  
  334.             else  
  335.                 return range.Text;  
  336.         }  
  337.         catch (Exception err)  
  338.         {  
  339.             throw new ExcelException(  
  340.                 String.Format(CultureInfo.InvariantCulture, "Can not access values at address '{0}'", cellAddress), err);  
  341.         }  
  342.     }  
  343.   
  344.     public double? GetNumericValue(string cellAddress)  
  345.     {  
  346.         return (double?)GetValue(cellAddress, Category.Numeric);  
  347.     }  
  348.   
  349.     public object GetFormattedValue(string cellAddress)  
  350.     {  
  351.         return GetValue(cellAddress, Category.Formatted);  
  352.     }  
  353.   
  354.     private bool IsWasteCellInMergeArea(Range range)  
  355.     {  
  356.         if (!((bool)range.MergeCells))  
  357.             return false;  
  358.         Range firstCellInMergeArea = range.MergeArea.Cells[1, 1] as Range;  
  359.         return !(range.Column == firstCellInMergeArea.Column && range.Row == firstCellInMergeArea.Row);  
  360.     }  
  361.   
  362.     public ArrayList GetRangeValues(string startCellAddress, string endCellAddress, Category category)  
  363.     {  
  364.         if (String.IsNullOrEmpty(startCellAddress))  
  365.             throw new ArgumentNullException("startCellAddress");  
  366.         if (String.IsNullOrEmpty(endCellAddress))  
  367.             throw new ArgumentNullException("endCellAddress");  
  368.         if (!AnyFileOpen)  
  369.             throw new ExcelException("No file is Open");  
  370.   
  371.         try  
  372.         {  
  373.             Range range = App.get_Range(startCellAddress, endCellAddress);  
  374.   
  375.             ArrayList arr = new ArrayList();  
  376.             foreach (Range r in range)  
  377.             {  
  378.                 if (IsWasteCellInMergeArea(r))  
  379.                     continue;  
  380.                 if (category == Category.Formatted)  
  381.                     arr.Add(r.Text);  
  382.                 else  
  383.                     arr.Add((double?)r.Value2);  
  384.             }  
  385.             return arr;  
  386.         }  
  387.         catch (Exception err)  
  388.         {  
  389.             throw new ExcelException(  
  390.                 String.Format(CultureInfo.InvariantCulture, "Can not get values of range '{0}:{1}'", startCellAddress, endCellAddress), err);  
  391.         }  
  392.     }  
  393.   
  394.     public ArrayList GetRangeFormattedValues(string startCellAddress, string endCellAddress)  
  395.     {  
  396.         return GetRangeValues(startCellAddress, endCellAddress, Category.Formatted);  
  397.     }  
  398.   
  399.     public ArrayList GetRangeNumericValues(string startCellAddress, string endCellAddress)  
  400.     {  
  401.         return GetRangeValues(startCellAddress, endCellAddress, Category.Numeric);  
  402.     }  
  403.   
  404.     public void SetValue(string cellAddress, object value)  
  405.     {  
  406.         if (String.IsNullOrEmpty(cellAddress))  
  407.             throw new ArgumentNullException("cellAddress");  
  408.         if (!AnyFileOpen)  
  409.             throw new ExcelException("No file is Open");  
  410.           
  411.         try  
  412.         {  
  413.             App.get_Range(cellAddress, System.Type.Missing).Value2 = value;  
  414.         }  
  415.         catch (Exception err)  
  416.         {  
  417.             throw new ExcelException(  
  418.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", cellAddress), err);  
  419.         }  
  420.     }  
  421.   
  422.     public void SetHyperLinkValue(string cellAddress,string linkAddress,object value)  
  423.     {  
  424.         if (String.IsNullOrEmpty(cellAddress))  
  425.             throw new ArgumentNullException("cellAddress");  
  426.         if (!AnyFileOpen)  
  427.             throw new ExcelException("No file is Open");  
  428.   
  429.         try  
  430.         {  
  431.             Range range = App.get_Range(cellAddress, System.Type.Missing);  
  432.             range.Value2 = value;  
  433.             ((_Worksheet)App.ActiveSheet).Hyperlinks.Add(range,linkAddress);  
  434.         }  
  435.         catch (Exception err)  
  436.         {  
  437.             throw new ExcelException(  
  438.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", cellAddress), err);  
  439.         }  
  440.     }  
  441.   
  442.     public Range GetRange(string cellAccess1, string cellAccess2)  
  443.     {  
  444.         Range range = null;  
  445.   
  446.         if (!AnyFileOpen)  
  447.             throw new ExcelException("No file is Open");  
  448.   
  449.         try  
  450.         {  
  451.            range= App.get_Range(cellAccess1, cellAccess2);  
  452.         }  
  453.         catch (Exception err)  
  454.         {  
  455.             throw new ExcelException(  
  456.                 String.Format(CultureInfo.InvariantCulture, "Can not copy value of cell '{0}'", cellAccess1), err);  
  457.         }  
  458.   
  459.         return range;  
  460.     }  
  461.   
  462.     /// <summary>  
  463.     /// 设置行高  
  464.     /// </summary>  
  465.     /// <param name="cellFrom1"></param>  
  466.     /// <param name="cellTo1"></param>  
  467.     public void SetRangeRowHeight(string cellFrom1, string cellTo1,object value)  
  468.     {  
  469.         if (!AnyFileOpen)  
  470.             throw new ExcelException("No file is Open");  
  471.   
  472.         try  
  473.         {  
  474.             // _Worksheet worksheet = (_Worksheet)App.ActiveWorkbook.Sheets[index];  
  475.   
  476.             App.get_Range(cellFrom1, cellTo1).RowHeight = value;  
  477.         }  
  478.         catch (Exception err)  
  479.         {  
  480.             throw new ExcelException(  
  481.                 String.Format(CultureInfo.InvariantCulture, "Can not copy value of cell '{0}'", cellFrom1), err);  
  482.         }  
  483.     }  
  484.   
  485.     /// <summary>  
  486.     /// 复制功能  
  487.     /// </summary>  
  488.     /// <param name="cellFrom1"></param>  
  489.     /// <param name="cellTo1"></param>  
  490.     public void RangeCopy(string cellFrom1, string cellTo1)  
  491.     {  
  492.         if (!AnyFileOpen)  
  493.             throw new ExcelException("No file is Open");  
  494.   
  495.         try  
  496.         {  
  497.            // _Worksheet worksheet = (_Worksheet)App.ActiveWorkbook.Sheets[index];  
  498.               
  499.             App.get_Range(cellFrom1).Copy(App.get_Range(cellTo1, cellTo1));  
  500.         }  
  501.         catch (Exception err)  
  502.         {  
  503.             throw new ExcelException(  
  504.                 String.Format(CultureInfo.InvariantCulture, "Can not copy value of cell '{0}'", cellFrom1), err);  
  505.         }  
  506.     }  
  507.   
  508.   
  509.     /// <summary>  
  510.     /// 带sheet激活的复制功能  
  511.     /// </summary>  
  512.     /// <param name="cellFrom1"></param>  
  513.     /// <param name="cellFrom2"></param>  
  514.     /// <param name="cellTo1"></param>  
  515.     /// <param name="cellTo2"></param>  
  516.     public void RangeCopy(string cellFrom1, string cellFrom2, string cellTo1, string cellTo2, int index)  
  517.     {  
  518.         if (!AnyFileOpen)  
  519.             throw new ExcelException("No file is Open");  
  520.   
  521.         try  
  522.         {  
  523.             _Worksheet worksheet = (_Worksheet)App.ActiveWorkbook.Sheets[index];  
  524.   
  525.             App.get_Range(cellFrom1, cellFrom2).Copy(worksheet.get_Range(cellTo1, cellTo2));  
  526.         }  
  527.         catch (Exception err)  
  528.         {  
  529.             throw new ExcelException(  
  530.                 String.Format(CultureInfo.InvariantCulture, "Can not copy value of cell '{0}'", cellFrom1), err);  
  531.         }  
  532.     }  
  533.   
  534.     /// <summary>  
  535.     /// 剪切一行  
  536.     /// </summary>  
  537.     /// <param name="FromRowIndex">被剪切的行</param>  
  538.     /// <param name="ToRowIndex">粘貼到的行</param>  
  539.     public void CutRow(int FromRowIndex,int ToRowIndex)  
  540.     {  
  541.         if (!AnyFileOpen)  
  542.             throw new ExcelException("No file is Open");  
  543.   
  544.         try  
  545.         {  
  546.             Range range = (Range)App.Rows[FromRowIndex];  
  547.             range.Cut((Range)App.Rows[ToRowIndex]);  
  548.   
  549.             range = null;  
  550.         }  
  551.         catch (Exception err)  
  552.         {  
  553.             throw new ExcelException(  
  554.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", FromRowIndex), err);  
  555.         }  
  556.     }  
  557.   
  558.     /// <summary>  
  559.     /// 插入一行  
  560.     /// </summary>  
  561.     /// <param name="RowIndex"></param>  
  562.     public void InsertRow(int RowIndex)  
  563.     {  
  564.   
  565.         if (!AnyFileOpen)  
  566.             throw new ExcelException("No file is Open");  
  567.   
  568.         try  
  569.         {  
  570.             Range range = (Range)App.Rows[RowIndex];  
  571.             range.Insert();  
  572.               
  573.             range = null;  
  574.         }  
  575.         catch (Exception err)  
  576.         {  
  577.             throw new ExcelException(  
  578.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", RowIndex), err);  
  579.         }  
  580.     }  
  581.   
  582.     /// <summary>  
  583.     /// 刪除一行  
  584.     /// </summary>  
  585.     /// <param name="RowIndex"></param>  
  586.     public void DeleteRow(int RowIndex)  
  587.     {  
  588.           
  589.         if (!AnyFileOpen)  
  590.             throw new ExcelException("No file is Open");  
  591.           
  592.         try  
  593.         {  
  594.             Range range =(Range)App.Rows[RowIndex];  
  595.   
  596.             range.Delete();  
  597.   
  598.             range = null;  
  599.         }  
  600.         catch (Exception err)  
  601.         {  
  602.             throw new ExcelException(  
  603.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", RowIndex), err);  
  604.         }  
  605.     }  
  606.   
  607.     /// <summary>  
  608.     /// 排序  
  609.     /// </summary>  
  610.     /// <param name="cellAddress1">Cell's address (for example "A2")</param>  
  611.     /// <param name="cellAddress2">Cell's address (for example "A2")</param>  
  612.     /// <param name="sortCellAddress1">Cell's sort (for example "A2")</param>  
  613.     /// <param name="sortCellAddress2">Cell's sort (for example "A2")</param>  
  614.     /// <param name="value">Any desired value</param>  
  615.     public void SortRange(string cellAddress1, string cellAddress2, string sortCellAddress1,string sortCellAddress2)  
  616.     {  
  617.   
  618.         if (!AnyFileOpen)  
  619.             throw new ExcelException("No file is Open");  
  620.   
  621.         try  
  622.         {  
  623.             Range rng = App.get_Range(cellAddress1, cellAddress2);  
  624.             Range rng1 = App.get_Range(sortCellAddress1, sortCellAddress2);  
  625.               
  626.             rng.Sort(rng1,  
  627.             XlSortOrder.xlDescending,  
  628.             Type.Missing, Type.Missing,  
  629.             XlSortOrder.xlAscending,  
  630.             Type.Missing, XlSortOrder.xlDescending,  
  631.             XlYesNoGuess.xlNo, Type.Missing, Type.Missing,  
  632.             XlSortOrientation.xlSortColumns,  
  633.             XlSortMethod.xlPinYin,  
  634.             XlSortDataOption.xlSortNormal,  
  635.             XlSortDataOption.xlSortNormal,  
  636.             XlSortDataOption.xlSortNormal);  
  637.   
  638.         }  
  639.         catch (Exception err)  
  640.         {  
  641.             throw new ExcelException(  
  642.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", sortCellAddress1), err);  
  643.         }  
  644.     }  
  645.   
  646.     /// <summary>  
  647.     /// Sets a cell content by use given color  
  648.     /// </summary>  
  649.     /// <param name="cellAddress">Cell's address (for example "A2")</param>  
  650.     /// <param name="value">Any desired value</param>  
  651.     /// <param name="cl">given color value</param>  
  652.     public void SetValue(string cellAddress, object value, int colorIndex)  
  653.     {  
  654.         if (String.IsNullOrEmpty(cellAddress))  
  655.             throw new ArgumentNullException("cellAddress");  
  656.         if (!AnyFileOpen)  
  657.             throw new ExcelException("No file is Open");  
  658.   
  659.         try  
  660.         {  
  661.             Range range = null;// 创建一个空的单元格对象  
  662.   
  663.             range = App.get_Range(cellAddress, Missing.Value);// 获取单个单元格  
  664.             range.Font.ColorIndex = colorIndex;      // 设置字体颜色  
  665.   
  666.             range.Value2 = value;// 设置单元格的值  
  667.         }  
  668.         catch (Exception err)  
  669.         {  
  670.             throw new ExcelException(  
  671.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", cellAddress), err);  
  672.         }  
  673.     }  
  674.   
  675.     public void SetValue(int rowindex,int colindex, object value)  
  676.     {  
  677.         if (rowindex ==null ||colindex ==null)  
  678.             throw new ArgumentNullException("cellAddress");  
  679.         if (!AnyFileOpen)  
  680.             throw new ExcelException("No file is Open");  
  681.   
  682.         try  
  683.         {  
  684.             App.Cells[rowindex,colindex] = value;  
  685.         }  
  686.         catch (Exception err)  
  687.         {  
  688.             throw new ExcelException(  
  689.                 String.Format(CultureInfo.InvariantCulture, "Can not set value of cell '{0}'", rowindex.ToString () +colindex.ToString () ), err);  
  690.         }  
  691.     }  
  692.   
  693.     /// <summary>  
  694.     ///清理rangge.  
  695.     /// </summary>  
  696.     /// <param name="cellAddress1">Cell's address (for example "A2")</param>  
  697.     /// <param name="cellAddress2">Cell's address (for example "A2")</param>  
  698.     public void RangeClear(string cellAddress1, string cellAddress2)  
  699.     {  
  700.         if (String.IsNullOrEmpty(cellAddress1))  
  701.             throw new ArgumentNullException("cellAddress1");  
  702.         if (String.IsNullOrEmpty(cellAddress2))  
  703.             throw new ArgumentNullException("cellAddress2");  
  704.         if (!AnyFileOpen)  
  705.             throw new ExcelException("No file is Open");  
  706.   
  707.         try  
  708.         {  
  709.             App.get_Range(cellAddress1, cellAddress2).Clear();  
  710.         }  
  711.         catch (Exception err)  
  712.         {  
  713.             throw new ExcelException(  
  714.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress1), err);  
  715.         }  
  716.     }  
  717.   
  718.     /// <summary>  
  719.     /// 设置一个cell的边线.  
  720.     /// </summary>  
  721.     /// <param name="cellAddress1">Cell's address (for example "A2")</param>  
  722.     /// <param name="cellAddress2">Cell's address (for example "A2")</param>  
  723.     /// <param name="value">Any desired value</param>  
  724.     public void SetBorderColor(string cellAddress1,string cellAddress2, object value)  
  725.     {  
  726.         if (String.IsNullOrEmpty(cellAddress1))  
  727.             throw new ArgumentNullException("cellAddress1");  
  728.         if (String.IsNullOrEmpty(cellAddress2))  
  729.             throw new ArgumentNullException("cellAddress2");  
  730.         if (!AnyFileOpen)  
  731.             throw new ExcelException("No file is Open");  
  732.   
  733.         try  
  734.         {  
  735.             App.get_Range(cellAddress1,cellAddress2).Borders.Color = value;  
  736.         }  
  737.         catch (Exception err)  
  738.         {  
  739.             throw new ExcelException(  
  740.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress1), err);  
  741.         }  
  742.     }  
  743.   
  744.     public void SetNumberFormat(string cellAddress, object value)  
  745.     {  
  746.         if (String.IsNullOrEmpty(cellAddress))  
  747.             throw new ArgumentNullException("cellAddress");  
  748.         if (!AnyFileOpen)  
  749.             throw new ExcelException("No file is Open");  
  750.   
  751.         try  
  752.         {  
  753.               
  754.             App.get_Range(cellAddress, System.Type.Missing).NumberFormat = value;  
  755.         }  
  756.         catch (Exception err)  
  757.         {  
  758.             throw new ExcelException(  
  759.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress), err);  
  760.         }  
  761.     }  
  762.   
  763.     public void CellMerge(string cellAddress1,string cellAddress2)  
  764.     {  
  765.         if (String.IsNullOrEmpty(cellAddress1))  
  766.             throw new ArgumentNullException("cellAddress1");  
  767.         if (!AnyFileOpen)  
  768.             throw new ExcelException("No file is Open");  
  769.   
  770.         try  
  771.         {  
  772.             Range range=App.get_Range(cellAddress1,cellAddress2);  
  773.             range.Merge(range.MergeCells);  
  774.         }  
  775.         catch (Exception err)  
  776.         {  
  777.             throw new ExcelException(  
  778.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress1), err);  
  779.         }  
  780.     }  
  781.   
  782.     public void SetColumnAutoFit(string cellAddress)  
  783.     {  
  784.         if (String.IsNullOrEmpty(cellAddress))  
  785.             throw new ArgumentNullException("cellAddress");  
  786.         if (!AnyFileOpen)  
  787.             throw new ExcelException("No file is Open");  
  788.   
  789.         try  
  790.         {  
  791.   
  792.             App.get_Range(cellAddress, System.Type.Missing).EntireColumn.AutoFit();  
  793.         }  
  794.         catch (Exception err)  
  795.         {  
  796.             throw new ExcelException(  
  797.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress), err);  
  798.         }  
  799.     }  
  800.   
  801.     public void SetColumnWidth(string cellAddress, object value)  
  802.     {  
  803.         if (String.IsNullOrEmpty(cellAddress))  
  804.             throw new ArgumentNullException("cellAddress");  
  805.         if (!AnyFileOpen)  
  806.             throw new ExcelException("No file is Open");  
  807.   
  808.         try  
  809.         {  
  810.   
  811.             App.get_Range(cellAddress).ColumnWidth = value;  
  812.         }  
  813.         catch (Exception err)  
  814.         {  
  815.             throw new ExcelException(  
  816.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress), err);  
  817.         }  
  818.     }  
  819.   
  820.     public void SetFontSize(string cellAddress, object value)  
  821.     {  
  822.         if (String.IsNullOrEmpty(cellAddress))  
  823.             throw new ArgumentNullException("cellAddress");  
  824.         if (!AnyFileOpen)  
  825.             throw new ExcelException("No file is Open");  
  826.   
  827.         try  
  828.         {  
  829.   
  830.             App.get_Range(cellAddress, System.Type.Missing).Font.Size = value;  
  831.         }  
  832.         catch (Exception err)  
  833.         {  
  834.             throw new ExcelException(  
  835.                 String.Format(CultureInfo.InvariantCulture, "Can not set NumberFormat of cell '{0}'", cellAddress), err);  
  836.         }  
  837.     }  
  838.   
  839.     public void MergeAndWriteValue(string startCellAddress, string endCellAddress, object value, int colorIndex)  
  840.     {  
  841.         if (String.IsNullOrEmpty(startCellAddress))  
  842.             throw new ArgumentNullException("startCellAddress");  
  843.         if (String.IsNullOrEmpty(endCellAddress))  
  844.             throw new ArgumentNullException("endCellAddress");  
  845.         if (!AnyFileOpen)  
  846.             throw new ExcelException("No file is Open");  
  847.   
  848.         try  
  849.         {  
  850.             Range range = null;// 创建一个空的单元格对象  
  851.   
  852.             range = App.get_Range(startCellAddress, endCellAddress);// 获取多个单元格  
  853.             range.Merge(Missing.Value);  
  854.             // 设置单元格左边框加粗  
  855.             range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;  
  856.             // 设置单元格右边框加粗  
  857.             range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;  
  858.             range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 设置单元格水平居中  
  859.             range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 设置单元格垂直居中  
  860.   
  861.             range.Borders.LineStyle = 1;    // 设置单元格边框  
  862.             range.Font.Bold = true;         // 加粗字体  
  863.             range.Font.Size = 12;           // 设置字体大小  
  864.             range.Font.ColorIndex = colorIndex;      // 设置字体颜色  
  865.             range.Interior.ColorIndex = 33;  // 设置单元格背景色  
  866.   
  867.             range.Value2 = value;  
  868.   
  869.         }  
  870.         catch (Exception err)  
  871.         {  
  872.             throw new ExcelException(  
  873.                 String.Format(CultureInfo.InvariantCulture, "Can not set values of range '{0}:{1}'", startCellAddress, endCellAddress), err);  
  874.         }  
  875.     }  
  876.   
  877.     public void SetRangeValue(string startCellAddress, string endCellAddress, object value)  
  878.     {  
  879.         if (String.IsNullOrEmpty(startCellAddress))  
  880.             throw new ArgumentNullException("startCellAddress");  
  881.         if (String.IsNullOrEmpty(endCellAddress))  
  882.             throw new ArgumentNullException("endCellAddress");  
  883.         if (!AnyFileOpen)  
  884.             throw new ExcelException("No file is Open");  
  885.   
  886.         try  
  887.         {  
  888.             App.get_Range(startCellAddress, endCellAddress).Value2 = value;  
  889.         }  
  890.         catch (Exception err)  
  891.         {  
  892.             throw new ExcelException(  
  893.                 String.Format(CultureInfo.InvariantCulture, "Can not set values of range '{0}:{1}'", startCellAddress, endCellAddress), err);  
  894.         }  
  895.     }  
  896.   
  897.     public void SetRangeValues(string startCellAddress, string endCellAddress, IList values)  
  898.     {  
  899.         if (values == null)  
  900.             throw new ArgumentNullException("values");  
  901.         if (String.IsNullOrEmpty(startCellAddress))  
  902.             throw new ArgumentNullException("startCellAddress");  
  903.         if (String.IsNullOrEmpty(endCellAddress))  
  904.             throw new ArgumentNullException("endCellAddress");  
  905.         if (!AnyFileOpen)  
  906.             throw new ExcelException("No file is Open");  
  907.   
  908.         try  
  909.         {  
  910.             int index = 0;  
  911.             Range range = App.get_Range(startCellAddress, endCellAddress);  
  912.             foreach (Range r in range)  
  913.             {  
  914.                 if (index >= values.Count)  
  915.                     return;  
  916.                 if (IsWasteCellInMergeArea(r))  
  917.                     continue;  
  918.                 r.Value2 = values[index];  
  919.                 index++;  
  920.             }  
  921.         }  
  922.         catch (Exception err)  
  923.         {  
  924.             throw new ExcelException(  
  925.                 String.Format(CultureInfo.InvariantCulture, "Can not set values of range '{0}:{1}'", startCellAddress, endCellAddress), err);  
  926.         }  
  927.     }  
  928.     #endregion  
  929.  
  930.     #region IDisposable Members  
  931.   
  932.     private bool _disposedValue; // To detect redundant calls  
  933.   
  934.     public void Dispose()  
  935.     {  
  936.         Dispose(true);  
  937.         GC.SuppressFinalize(this);  
  938.     }  
  939.   
  940.     [DllImport("User32.dll", CharSet = CharSet.Auto)]  
  941.     public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);  
  942.   
  943.     protected virtual void Dispose(bool disposing)  
  944.     {  
  945.           
  946.         if (!_disposedValue)  
  947.             if (disposing)  
  948.             {  
  949.                 if (App != null)  
  950.                 {  
  951.                     Close();  
  952.                     //App.Quit();  
  953.                     //App = null;  
  954.                     //instance = null;  
  955.                     //GC.Collect();  
  956.                     IntPtr t = new IntPtr(App.Hwnd); //得到这个句柄,具体作用是得到这块内存入口   
  957.                     int k = 0;  
  958.                     GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k   
  959.                     System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用   
  960.                     p.Kill(); //关闭进程k   
  961.   
  962.                     App=null;  
  963.                     instance = null;  
  964.   
  965.                     Thread.CurrentThread.CurrentCulture = OriginalCulture;  
  966.                 }  
  967.             }  
  968.           
  969.         _disposedValue = true;  
  970.     }  
  971.  
  972.     #endregion  
  973. }  
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值