標籤:style blog http io os ar strong 資料 div
今天幫現在餓公司寫個工具,要動態讀excel上的ip地址與連接埠號碼,來更改IE的Proxy 位址,由於好久沒寫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 的許多屬性、方法和事件與 Application 或 Microsoft.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);
C# 對Excel操作與分析