C# 对Excel操作与分析

今天帮现在饿公司写个工具,要动态读excel上的ip地址与端口号,来更改IE的代理地址,由于好久没写Excel的操作了,只能查阅以前的项目,总结一下:
首先我们要引用我们的com接口的excel
Microsoft.Office.Interop.Excel.Application excel对象表示 Excel 应用程序本身。Application 对象公开了大量有关正在运行的应用程序、应用于该实例的选项以及在该实例中打开的当前用户的对象的信息。
注意:我们不能将Excel 中 Application 对象的 EnableEvents 属性设置为 false。将此属性设置为 false 将阻止 Excel 引发任何事件,包括宿主控件的事件。
workbook对象
Microsoft.Office.Interop.Excel.Workbook 类表示 Excel 应用程序中的单个工作簿
Worksheet 对象
Microsoft.Office.Interop.Excel.Worksheet 对象是 Worksheets 集合的成员。Microsoft.Office.Interop.Excel.Worksheet 的许多属性、方法和事件与 ApplicationMicrosoft.Office.Interop.Excel.Workbook 类提供的成员完全相同或相似。

Excel 提供 Sheets 集合作为 Microsoft.Office.Interop.Excel.Workbook 对象的属性,但是 Excel 中没有 Sheet 类。相反,Sheets 集合的每个成员都是一个 Microsoft.Office.Interop.Excel.Worksheet 对象,或者是一个 Microsoft.Office.Interop.Excel.Chart 对象
Range 对象

Microsoft.Office.Interop.Excel.Range 对象是 Excel 应用程序中最常用的对象。在能够处理 Excel 内的任何范围之前,必须将它表示为 Range 对象,并处理该对象的方法和属性。Range 对象表示一个单元格、一行、一列、包含一个或多个单元格块(可以连续,也可以不连续)的单元格选定范围,甚至多个工作表中的一组单元格。

写操作:
  Microsoft.Office.Interop.Excel.ApplicationClass MyExcel =     newMicrosoft.Office.Interop.Excel.ApplicationClass();
     MyExcel.Visible = false;//excel是否可见
     MyExcel.DisplayAlerts = false;//屏蔽一些弹出窗口
   Microsoft.Office.Interop.Excel.Workbooks MyWorkBooks = MyExcel.Workbooks;
   Microsoft.Office.Interop.Excel.Workbook MyWorkBook = MyWorkBooks.Add(System.Type.Missing);
 Microsoft.Office.Interop.Excel.Worksheet MyWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Worksheets[1];//指定一个sheet页
Microsoft.Office.Interop.Excel.Range MyRange = MyWorkSheet.get_Range("A1",  "D1");//我们指定一个指定的区域我们也可以动态设置Range区域,
最后将DataTable之类的数据转换成二维数组赋值给这个区域,
 object[,] MyData;
 MyRange.Value2 = MyData;
如果就单个值给excel我们可以
MyWorkSheet.get_Range("A1",System.Type.Missing).value2="aaaaaaaaa";

读操作:
 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
 object Nothing = System.Type.Missing;
   excel.Application.Workbooks.Open(Application.StartupPath + "\\RedEx.xls", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing,Nothing,Nothing,Nothing);  //打开一个工作薄     
  Microsoft.Office.Interop.Excel.Workbooks    wbs = excel.Workbooks
  Microsoft.Office.Interop.Excel.Workbook wb = wbs[1];
  Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
  string sExA =ws.get_Range("A"+num, System.Type.Missing).Value2.ToString();
  string sExB = ws.get_Range("B" + num, System.Type.Missing).Value2.ToString();      

利用excel分析
//加载宏
 Microsoft.Office.Interop.Excel.ApplicationClass MyExcel
  MyExcel.AddIns["分析工具库 - VBA 函数"].Installed = false;
    MyExcel.AddIns["分析工具库 - VBA 函数"].Installed = true;
利用宏来分析,可以在excel录制宏查看名称
MyWorkSheet是工作页
 MyExcel.Run("Pttestm", MyWorkSheet.get_Range("E2", "E" + iCount.ToString()), MyWorkSheet.get_Range("H2", "H" + iCount.ToString()), MyWorkSheet.get_Range("M2", System.Type.Missing), false, 0.05, 0, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                                               System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                                               System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                                               System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing
                                               , System.Type.Missing, System.Type.Missing, System.Type.Missing);



原文地址:https://www.cnblogs.com/JackWang/p/1128579.html