The Last Day Of Summer

.NET技术 C# ASP.net ActiveReport SICP 代码生成 报表应用 RDLC
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用C#和Excel进行报表开发(六)-操作行和列

Posted on 2007-01-10 14:35  Cure  阅读(10868)  评论(2编辑  收藏  举报

Excel中,行和列实际上仍然是Range,只不过取得行和列的方法和前面几篇随笔出现的有点不同,要使用WorksheetRowsColumns属性的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进程的释放,前面几篇随笔已经有了,这里就不再重复了。