使用COM组件实现对Excel文件的操控

1 使用COM组件操控Excel的优势和缺点


优势:
提供了完整的Excel操控能力。可以方便的进行复杂操作。
提供了对Excel文件的底层操作,工作效率高。
和VBA方式相比可以脱离特定的Excel文件进行使用。
缺点:
代码复杂不易理解。
在操作中需要处理大量的数据类型。


2.使用方法

 Visual Studio 2005中默认提供了封装好的Microsoft Office 2003的.Net Excel编程模型。图中所显示的是已经安装了Office2005之后VS2005所带的.Net Excel模型。大家可以看到名为“Microsoft Excel 12.0 Object Library”的COM组件。   

 由于Excel的版本一直在升级,如果我们需要操作更新版本的Excel文件,但又没有安装最新的Office,这个时候我们就需要手工进行Excel COM组件的封装。
这个时候我们需要使用 Framework v2.0 SDK tools中的一个工具tlbimp.exe-类型库导入程序(
http://msdn2.microsoft.com/zh-cn/library/tt0cf3sx(VS.80).aspx),默认安装位置在。C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin

 把需要封装的Excel的主程序(excel.exe)复制到 tlbimp工具所在目录下。
执行命令: tlbimp excel.exe /out:excel.dll
生成封装好的excel.dll。然后就可对excel.dll进行引用。


成功引用后,引用下会出现
1.Excel
2.Microsoft.Office.Core
3.Microsoft.Office.Interop.Excel
三个类库。
其中:1为我们手工封装的Excel COM组件。2、3为我们直接引用的安装在VS2005中的Excel COM组件。如果使用VS2005内置COM组件,1将不会出现,如使用手工封装的COM组件2、3就不会出现。

 

3.使用的基本流程


a.建立一个Excel进程

Microsoft.Office.Interop.Excel.Application app1  =  
                
new  Microsoft.Office.Interop.Excel.Application();

b.设置进程的界面是否可见

app1.Visible  =   true ;

c.建立或打开一个 Workbook对象
生成新Workbook

Workbook book1  =  app1.Workbooks.Add(Type.Missing);

或打开已有Workbook

Workbook book1  =  app1.Workbooks.Open( " D:/test.xlsx " , Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,         Type.Missing, Type.Missing, Type.Missing, Type.Missing,         Type.Missing, Type.Missing, Type.Missing, Type.Missing,         Type.Missing);

d.生成一个Sheet对象

Worksheet sheet1  =  (Worksheet)book1.Sheets[ 1 ];

e.设置访问区域

Range rng1  =  sheet1.get_Range(“A1:C3,E2:G6”, Type.Missing);  // 设置操作区域为个不连续区域
f.对访问区域进行操作
rng1.NumberFormatLocal 
=  “@”;  // 格式设置为文本
rng1.Value2  =  “2006147214E00045”;  // 对操作区域赋值

g.保存操作过程

app1.DisplayAlerts  =   false ;   // 不显示保存对话框
book1.Save();   // 保存修改

h.对于要处理多个Excel文件的程序还需要关闭book1并保存修改数据。

book1.Close(Type.Missing,  " D:/test.xlsx " , Type.Missing);

i.关闭Excel进程

app1.Quit();

4.完整的程序代码

使用VS2005 编写,需.Net Framework 2.0支持

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  Microsoft.Office.Interop.Excel;     // 引用Excel的COM组件

namespace  WinFormExcelWelcome
... {
    
public partial class Form1 : Form
    
...{
        
public Form1()
        
...{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
...{
            Microsoft.Office.Interop.Excel.Application app1 
= 
                
new Microsoft.Office.Interop.Excel.Application();       //在Windows中生成一个Excel.exe进程
            app1.Visible = true;                                        //运行时显示Excel主窗口
            try
            
...{
                Workbook book1 
= app1.Workbooks.Add(Type.Missing);      //使用 app1生成一个book对象
                Worksheet sheet1 = (Worksheet)book1.Sheets[1];          //从book1对象中生成Sheet对象并赋值
                Range rng1 = sheet1.get_Range("A1", Type.Missing);      //设置操作区域
                rng1.Value2 = "Hello World!";                           //对操作区域赋值
            }

            
catch
            
...{
                app1.Quit();                                            
//结束进程
            }

        }


        
private void button2_Click(object sender, EventArgs e)
        
...{
            Microsoft.Office.Interop.Excel.Application app1 
= 
                
new Microsoft.Office.Interop.Excel.Application();
            app1.Visible 
= true;                                                //运行时显示Excel主窗口
            try
            
...{   
                
//打开一个已经存在的excel文件
                Workbook book1 = app1.Workbooks.Open("D:/test.xlsx", Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing);
                Worksheet sheet1 
= (Worksheet)book1.Sheets["Sheet1"];
                Range rng1 
= sheet1.get_Range("A1:C3,E2:G6", Type.Missing);     //设置操作区域为2个不连续区域
                rng1.NumberFormatLocal = "@";
                rng1.Value2 
= "2006147214E00045";                               //对操作区域赋值
                Range rng2 = sheet1.get_Range("F1:F6", Type.Missing);
                rng2.Value2 
= "2006147214E00045";
                app1.DisplayAlerts 
= false;                                     //不显示保存对话框
                book1.Save();                                                   //保存
            }

            
catch
            
...{
                app1.Quit();                                                    
//结束进程
            }


        }


        
private void button3_Click(object sender, EventArgs e)
        
...{
            Microsoft.Office.Interop.Excel.Application app1 
= 
                
new Microsoft.Office.Interop.Excel.Application();
            app1.Visible 
= false;    //运行时不显示Excel主窗口
            try
            
...{
                Workbook book1 
= app1.Workbooks.Add(Type.Missing);  
                Worksheet sheet1 
= (Worksheet)book1.Sheets[1];
                app1.DisplayAlerts 
= true;
                
//将打开的Excel文件另存:D est.xlsx 格式 xlXMLSpreadsheet(xlsm)、xlOpenXMLWorkbook(xlsx)、 xlExcel12(xlsb) 
                book1.SaveAs("D:/test.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, 
                    Type.Missing, Type.Missing,
false,false,
                    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                    Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
                book1.Close(Type.Missing, 
"D:/test.xlsx", Type.Missing);       
            }

            
catch
            
...{
                app1.Quit();
            }

            
finally
            
...{
                app1.Quit();
            }

        }

    }

}

附完整程序:

按钮1-在内存中建立一个空白Excel文件(界面中第一个按钮)
按钮2-打开一个Excel文件并做写入(界面中第三个按钮)
按钮3-在D:/新建一个Excel 2007格式的空白Excel文件(界面中第二个按钮)

界面:

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值