首先将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. 给单元格设置变量:
if (range1 == null )
{
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1 ;
Object[] args1 = new Object[ 1 ];
args1[ 0 ] = nCells;
range1.GetType().InvokeMember( " Value " ,BindingFlags.SetProperty, null , range1, args1);
例程:
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}
exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}
现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
Range range2 = worksheet.get_Range( " A1 " , " E1 " );
int [] array2 = new int [nCell];
for ( int i = 0 ; i < array2.GetLength( 0 ); i ++ )
{
array2[i] = i+1;
}
Object[] args2 = new Object[ 1 ];
args2[ 0 ] = array2;
range2.GetType().InvokeMember( " Value " , BindingFlags.SetProperty, null , range2, args2);
大家需要了解Tlbimp这个工具的使用啊:)这个东东很有用,可以将普通Win32程序移植到.Net下面来:)
如果操作的excel的格式很简单,就是一般的表的结构,那么其实操作EXCEL文件跟操作ACCESS数据库文件的方法几乎一样。
(需要注意的地方就是,1、程序会把EXCLE表中的第一行记录作为列名;2、在使用EXCLE表的时候, 要在表名后面加上符号$)
下面,我给你帖一段如何连接和读取EXCEL文件的代码吧:
OleDbDataAdapter ad;
string strDbPath = " ./code.xls " ;
string strConn = " Provider=Microsoft.Jet.OleDb.4.0; Data Source= " + Server.MapPath(strDbPath) + " ; Extended Properties=Excel 8.0; " ;
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
string strSQL = " select * from [股票代码$] " ;
ad = new OleDbDataAdapter(strSQL, Conn);
ad.Fill(ds);
dg1.DataSource = ds.Tables[ 0 ].DefaultView; // dg1是一个DataGrid控件
dg1.DataBind(); // 将EXCLE中股票代码中的记录棒定到DataGrid控件上
如果是在asp.net 下使用的话,要记得在 <system.web>中添加<identity impersonate="true"/>
否则就会出现 “异常详细信息: System.UnauthorizedAccessException: 拒绝访问“。