在Excel中,行和列實際上仍然是Range,只不過取得行和列的方法和前面幾篇隨筆出現的有點不同,要使用Worksheet的Rows和Columns屬性的get_Item方法,下面是例子代碼,演示了怎樣選中行,並且在行之間復制數據,和插入行:
ThisApplication = new Excel.Application(); ThisWorkbook = ThisApplication.Workbooks.Open("z:\\Book1.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); ThisApplication.DisplayAlerts = false; xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(1); //Excel.Range range = xlSheet.get_Range("G4",Type.Missing); Excel.Range range = (Excel.Range)xlSheet.Rows.get_Item(18, Type.Missing); Excel.Range range1 = (Excel.Range)xlSheet.Rows.get_Item(19, Type.Missing); range.Value = "123"; range.Copy(range1); range1.Rows.Insert(XlInsertShiftDirection.xlShiftDown); ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
關於Excel進程的釋放,前面幾篇隨筆已經有了,這裡就不再重復了。