C# 操作 Excel

首先将excel.exe copy 到 ..\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin目录下
利用.net 中带的工具在命令提示符下执行tlbimp excel.exe.这样就不会因为你的Excel是xp或2000的不同要去找不同的*.olb文件,还有一点就是因为在2000以后的版本中没有了excel9.olb这个文件了。

通过执行tlbimp excel.exe后我们会得到excel.dll文件。

只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。
下面就让我们具体看看C#是如何使用这些东东吧。
1. 创建一个新Excel的Application:

Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}

2. 让这个工程可见:
exc.set_Visible(0, true);
3. 获取WorkBooks集合:
Workbooks workbooks = exc.Workbooks;
4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
5. 获取WorkSheets集合:

_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}
6. 给单元格设置变量:

 

None.gif Range range1  =  worksheet.get_Range( " C1 " ,Missing.Value);
None.gif
if  (range1  ==   null
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifConsole.WriteLine (
"ERROR: range == null");
ExpandedBlockEnd.gif}

None.gif
const   int  nCells  =   1 ;
None.gifObject[] args1 
=   new  Object[ 1 ];
None.gifargs1[
0 =  nCells;
None.gifrange1.GetType().InvokeMember(
" Value " ,BindingFlags.SetProperty,  null , range1, args1);

 

例程:

 

None.gif using  System;
None.gif
using  System.Reflection; 
None.gif
using  System.Runtime.InteropServices; 
None.gif
using  Excel;
ExpandedBlockStart.gifContractedBlock.gif
class  Excel  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
public static int Main() dot.gif{
InBlock.gifApplication exc 
= new Application();
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (exc == nulldot.gif{
InBlock.gifConsole.WriteLine(
"ERROR: EXCEL couldn't be started!");
InBlock.gif
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gifexc.set_Visible(
0true); 
InBlock.gifWorkbooks workbooks 
= exc.Workbooks;
InBlock.gif_Workbook workbook 
= workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 
InBlock.gifSheets sheets 
= workbook.Worksheets;
InBlock.gif_Worksheet worksheet 
= (_Worksheet) sheets.get_Item(1);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (worksheet == nulldot.gif{
InBlock.gifConsole.WriteLine (
"ERROR: worksheet == null");
ExpandedSubBlockEnd.gif}

InBlock.gifRange range1 
= worksheet.get_Range("C1", Missing.Value);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (range1 == nulldot.gif{
InBlock.gifConsole.WriteLine (
"ERROR: range == null");
ExpandedSubBlockEnd.gif}

InBlock.gif
const int nCells = 1;
InBlock.gifObject[] args1 
= new Object[1];
InBlock.gifargs1[
0= nCells;
InBlock.gifrange1.GetType().InvokeMember(
"Value", BindingFlags.SetProperty, null,range1, args1);
InBlock.gif
return 100;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}


现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;

None.gif const   int  nCell  =   5 ;
None.gifRange range2 
=  worksheet.get_Range( " A1 " " E1 " );
None.gif
int [] array2  =   new   int  [nCell];
None.gif
for  ( int  i = 0 ; i  <  array2.GetLength( 0 ); i ++
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifarray2[i] 
= i+1;
ExpandedBlockEnd.gif}

None.gifObject[] args2 
=   new  Object[ 1 ];
None.gifargs2[
0 =  array2;
None.gifrange2.GetType().InvokeMember(
" Value " , BindingFlags.SetProperty,  null , range2, args2);


  大家需要了解Tlbimp这个工具的使用啊:)这个东东很有用,可以将普通Win32程序移植到.Net下面来:)
如果操作的excel的格式很简单,就是一般的表的结构,那么其实操作EXCEL文件跟操作ACCESS数据库文件的方法几乎一样。
需要注意的地方就是,1、程序会把EXCLE表中的第一行记录作为列名;2、在使用EXCLE表的时候, 要在表名后面加上符号$

下面,我给你帖一段如何连接和读取EXCEL文件的代码吧:


 

None.gif DataSet ds  =   new  DataSet();
None.gifOleDbDataAdapter ad;
None.gif
None.gif
None.gif
string  strDbPath  =   " ./code.xls " ;
None.gif
string  strConn  =   " Provider=Microsoft.Jet.OleDb.4.0; Data Source= " + Server.MapPath(strDbPath) + " ; Extended Properties=Excel 8.0; " ;
None.gif
None.gifOleDbConnection Conn 
=   new  OleDbConnection(strConn);
None.gif
None.gifConn.Open();
None.gif
None.gif
string  strSQL  =   " select * from [股票代码$] " ;
None.gif
None.gif
None.gifad 
=   new  OleDbDataAdapter(strSQL, Conn);
None.gifad.Fill(ds);
None.gif
None.gifdg1.DataSource 
=  ds.Tables[ 0 ].DefaultView;   // dg1是一个DataGrid控件
None.gif
dg1.DataBind();   // 将EXCLE中股票代码中的记录棒定到DataGrid控件上
None.gif

None.gif



如果是在asp.net 下使用的话,要记得在  <system.web>中添加<identity impersonate="true"/>
否则就会出现 “异常详细信息: System.UnauthorizedAccessException: 拒绝访问“。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值