在vs2008环境C#对Excel基本操作

来源:互联网 发布:c语言return的用法 编辑:程序博客网 时间:2024/05/21 15:44

<!-- /* Font Definitions */ @font-face{font-family:新細明體;panose-1:2 2 3 0 0 0 0 0 0 0;mso-font-alt:PMingLiU;mso-font-charset:136;mso-generic-font-family:roman;mso-font-pitch:variable;mso-font-signature:3 137232384 22 0 1048577 0;}@font-face{font-family:"/@新細明體";panose-1:2 2 3 0 0 0 0 0 0 0;mso-font-charset:136;mso-generic-font-family:roman;mso-font-pitch:variable;mso-font-signature:3 137232384 22 0 1048577 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;mso-pagination:none;font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:新細明體;mso-font-kerning:1.0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 1.0cm 72.0pt 2.0cm;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:18.0pt;}div.Section1{page:Section1;}-->

环境:

Windows XP

Microsoft Visual Studio 2008

office 2003

 
   网上搜索C#实现excel操作的示例太多了,但不知道有多少是经过验证确实可行才发布出来的,也是因为开发需要,我找了一些代码却发现大多都不能正确执行完毕,于是决定补充自己在实践中遇到的要点以供参考。如下示例:

   添加的文件头:
     using System.Reflection; // 引用这个才能使用Missing字段
     using Excel;

       Excel.ApplicationClass excel = new Excel.ApplicationClass();
              excel.Visible = true;       //激活Excel
            WorkbookwBook   = excel.Workbooks.Add(true);
       //     WorksheetwSheet = (Excel._Worksheet)wBook.ActiveSheet;
            WorksheetwSheet =(Excel.Worksheet)wBook.ActiveSheet;      

 
                   excel.Cells[3, 5] = "本公司电话: " + Phone;
                   excel.Cells[4, 5] = "本公司传真: " + Zhen;
                   excel.Cells[5, 5] = "联系人: " + ComName;
                   excel.Cells[4, 1] = "客户: " + CustomerName;
                   excel.Cells[5, 1] = "联系人: " + Associate;
                   excel.Cells[3, 8] = "户名:";
                   excel.Cells[3, 9] = AccountName;
                   excel.Cells[4, 8] = "开户行:";
                   excel.Cells[4, 9] = BranchName;
                   excel.Cells[5, 8] = "帐号:";
                   excel.Cells[5, 9] = "'" + AccountID;


                      //设置禁止弹出保存和覆盖的询问提示框
           excel.DisplayAlerts = false;
            excel.AlertBeforeOverwriting= false;
            //保存工作薄
          //  wBook.Save();
            //每次保存激活的表,这样才能多次操作保存不同的Excel表,默认保存位置是在”我的文档"
           
           excel.Cells.Font.Size = 12;
           excel.Cells.Font.Bold = false;
          //  Excel.Rangem_objRange = m_objRange.get_Range(1, 3);
           wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Size = 24;
           wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Bold = true;
           wSheet.get_Range(excel.Cells[3, 1], excel.Cells[3, 1]).Font.ColorIndex = 3;//此处设为红色,不能用Font.Color来设置颜色
          // m_objRange.Cells.Font.Size = 24;
          // m_objRange.Cells.Font.Bold = true;

           excel.ActiveWorkbook.SaveCopyAs(filename);


         excel.Quit();
      代码注释部分只是简单描述各语句的原由,个别的还是值得推敲的。

      语句一 Workbook wBook   = excel.Workbooks.Add(true);
Workbooks.Add的参数是个object类型,通常使用true或null,表明工作簿在默认文档下创建,或者使用枚举值
      XlWBATemplate.xlWBATWorksheet,但如果传入一个excel完整文件名,却相当于打开已有工作簿。

      语句二 Worksheet wSheet = (Excel.Worksheet)wBook.ActiveSheet;    这样可以操作多个工作表的话,实例化之后加入到wBook.Worksheets中去。如果是打开已存在的工作簿,这条语句也可能会报错,最好是调用 wBook.ActiveSheet来获取或者再加些判断。

     语句三
       excel.ActiveWorkbook.SaveCopyAs(filename);这两句代码至关重要,而且必不可少,否则,保存时会弹出“是否保存sheet1.xls”的对话框。
判断当前激活的表,并保存这个表。

     语句四 excel.Quit();
     
这个关闭一直有疑点,因为C#操作com非托管对象时,凭借Quit()还没有释放掉对象,excel进程不一定会终止,于是,有人使用 KillProcess()来处理,我个人认为这不是一个好主意,可能会破坏其它正在执行的excel进程。目前我使用app
      =
      null;
权作安慰吧。不过有一点是一定要做到,就是在Quit()前不能再有任何更改,不然还是会弹出保存的对话框。所以退出前确保一定是执行过WorkBook或是ApplicationSave()方法的。
         
对于在c#中操作excel应用的方面很多,可能还会有些疑问出现,­知晓来龙去脉的朋友尽量补充以方便大家吧。

<script src="http://cpro.baidu.com/cpro/ui/cp.js" type="text/javascript"></script>-