登 录
谢谢!
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. 给单元格设置变量:
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);
例程:
using System; 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; const int nCell = 5; 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下面来:)
或者看看有没有Excel10.olb这个文件,试试TlbImp Excel10.olb Excel.dll (这个是我猜的,我没用过officexp)
下面,我给你帖一段如何连接和读取EXCEL文件的代码吧:
DataSet ds = new DataSet(); 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控件上
一.程序设计及运行环境
(1).微软视窗2000 服务器版
(2)..Net Framework SDK Beta 2
(3).Microsoft Data Access Component 2.6以上版本(MDAC2.6)
(4).Office 2000套件 二.Visual C#读取Excel表格中的数据:
本节将通过一个程序来介绍Visual C#读取Excel表格中的数据,并把数据以DataGrid的形式显示出来。
(1).如何读取数据:
其实读取Excel表格中的数据和读取数据库中的数据是非常类似的,因为在某种程度上Excel表格可以看成是一张一张的数据表。其二者的主要区别在于所使用的数据引擎不一样。在本文的程序中,通过下列代码实现读取Excel表格数据,具体如下:
//创建一个数据链接 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c://sample.xls;Extended Properties=Excel 8.0" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM [Sheet1$] " ; myConn.Open ( ) ; file://打开数据链接,得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://创建一个 DataSet对象 myDataSet = new DataSet ( ) ; file://得到自己的DataSet对象 myCommand.Fill ( myDataSet , "[Sheet1$]" ) ; file://关闭此数据链接 myConn.Close ( ) ;
怎么样读取Excel表格中的数据其实和读取数据库中的数据没有什么实质上的区别。
注释:这里读取的是C盘根目录下的"Sample.xls"文件。
(2).用DataGrid来显示得到的数据集:
在得到DataSet对象后,只需要通过下列二行代码,就可以把数据集用DataGrid显示出来了:
DataGrid1.DataMember= "[Sheet1$]" ; DataGrid1.DataSource = myDataSet ;
(3).用Visual C#读取Excel表格,并用DataGrid显示出来的程序代码(Read.cs)和程序运行的界面:
掌握了上面二点,水到渠成就可以得到以下代码:
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Data.OleDb ; public class Form1 : Form { private Button button1 ; private System.Data.DataSet myDataSet ; private DataGrid DataGrid1 ; private System.ComponentModel.Container components = null ;
public Form1 ( ) { file://初始化窗体中的各个组件 InitializeComponent ( ) ; file://打开数据链接,得到数据集 GetConnect ( ) ; } file://清除程序中使用过的资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; }
private void GetConnect ( ) { file://创建一个数据链接 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c://sample.xls;Extended Properties=Excel 8.0" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM [Sheet1$] " ; myConn.Open ( ) ; file://打开数据链接,得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://创建一个 DataSet对象 myDataSet = new DataSet ( ) ; file://得到自己的DataSet对象 myCommand.Fill ( myDataSet , "[Sheet1$]" ) ; file://关闭此数据链接 myConn.Close ( ) ; } private void InitializeComponent ( ) { DataGrid1 = new DataGrid ( ) ; button1 = new Button ( ) ; SuspendLayout ( ) ; DataGrid1.Name = "DataGrid1"; DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ;
button1.Location = new System.Drawing.Point ( 124 , 240 ) ; button1.Name = "button1" ; button1.TabIndex = 1 ; button1.Text = "读取数据" ; button1.Size = new System.Drawing.Size (84 , 24 ) ; button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ; this.Controls.Add ( button1 ) ; this.Controls.Add ( DataGrid1 ) ; this.Name = "Form1" ; this.Text = "读取Excle表格中的数据,并用DataGrid显示出来!" ; this.ResumeLayout ( false ) ;
} private void button1_Click ( object sender , System.EventArgs e ) { DataGrid1.DataMember= "[Sheet1$]" ; DataGrid1.DataSource = myDataSet ;
} static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } }
下图是程序编译后,运行结果:
http://www.yesky.com/20020313/jt-2002-3-13-image001.jpg
图01:用Visual C#读取"c:/sample.xls"的运行界面 (4).总结:
以上只是读取了Excel表格中"Sheet1"中的数据,对于其他"Sheet"中的内容,可以参照读取"Sheet1"中的程序,只作一点修改就可以了,譬如要读取"Sheet2"中的内容,只需要把"Read.cs"程序中的"Sheet1$"改成"Sheet2$"就可以了。
在Visual C#中调用Excel表格,并不像读取Excel表格中的数据那么容易了,因为在Visual C#中调用Excel表格要使用到Excel的COM组件。如果你安装Office套件在"C"盘,那么在"C:/Program Files/Microsoft Office/Office"可以找到这个COM组件"EXCEL9.OLB",在《Visual C#如何使用Active X组件》一文中,这些COM组件都是非受管代码的,要在Visual C#中使用这些非受管代码的COM组件,就必须把他们转换成受管代码的类库。所以在用Visual C#调用Excel表格之前,必须完成从COM组件的非受管代码到受管代码的类库的转换。
(1).非受管代码COM组件转换成受管代码的类库:
首先把COM组件"EXCEL9.OLB"拷贝到C盘的根目录下,然后输入下列命令:
tlbimp excel9.olb
这样在C盘的根目录下面就产生了三个DLL文件:"Excel.dll"、"Office.dll"、"VBIDE.dll"。在产生了上面的三个文件后,这种转换就成功完成了。在下面的程序中,就可以利用这转换好的三个类库编写和Excel表格相关的各种操作了。
(2).Visual C#打开Excel表格:
在"Excel.dll"中定义了一个命名空间"Excel",在差命名空间中封装了一个类"Application",这个类和启动Excel表格有非常重要的关系,在Visual C#中,只需要下列三行代码就可以完成打开Excel表格的工作,具体如下:
Excel.Application excel = new Excel.Application ( ) ; excel.Application.Workbooks.Add ( true ) ;
excel.Visible = true ;
但此时的Excel表格是一个空的表格,没有任何内容,下面就来介绍如何往Excel表格中输入数据。
(3).往Excel表格中输入数据:
在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个下单元。通过给差"Cell"赋值,从而实现往Excel表格中输入相应的数据,下列代码功能是打开Excel表格,并且往表格输入一些数据。
Excel.Application excel = new Excel.Application ( ) ; excel.Application.Workbooks.Add ( true ) ; excel.Cells[ 1 , 1 ] = "第一行第一列" ; excel.Cells[ 1 , 2 ] = "第一行第二列" ; excel.Cells[ 2 , 1 ] = "第二行第一列" ; excel.Cells[ 2 , 2 ] = "第二行第二列" ; excel.Cells[ 3 , 1 ] = "第三行第一列" ; excel.Cells[ 3 , 2 ] = "第三行第二列" ; excel.Visible = true ;
(4). Visual C#调用Excel表格,并在Excel表格中存储数据的程序代码(Excel.cs):
了解了上面的这些知识,得到完成上述功能的程序代码就显得比较容易了,具体如下:
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Data.SqlClient ; public class Form1 : Form { private Button button1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { file://初始化窗体中的各个组件 InitializeComponent ( ) ; } file://清除程序中使用的各个资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose( disposing ) ; } private void InitializeComponent ( ) { button1 = new Button ( ) ; SuspendLayout ( ) ; button1.Location = new System.Drawing.Point ( 32 , 72 ) ; button1.Name = "button1" ; button1.Size = new System.Drawing.Size ( 100 , 30 ) ; button1.TabIndex = 0 ; button1.Text = "调用Excel文件!" ; button1.Click += new System.EventHandler ( button1_Click ) ;
AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ; this.Controls.Add ( button1 ) ; this.Name = "Form1" ; this.Text = "如何用Visual C#调用Excel表格!" ; this.ResumeLayout ( false ) ;
} static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { Excel.Application excel = new Excel.Application ( ) ; excel.Application.Workbooks.Add ( true ) ; excel.Cells[ 1 , 1 ] = "第一行第一列" ; excel.Cells[ 1 , 2 ] = "第一行第二列" ; excel.Cells[ 2 , 1 ] = "第二行第一列" ; excel.Cells[ 2 , 2 ] = "第二行第二列" ; excel.Cells[ 3 , 1 ] = "第三行第一列" ; excel.Cells[ 3 , 2 ] = "第三行第二列" ; excel.Visible = true ; } }
(5).编译源程序和程序运行界面:
在经过了下列命令编译后:
Csc.exe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:excel.dll /r:office.dll /r:vbide.dll excel.cs
就可以得到"Excel.exe",运行后界面如下
http://www.yesky.com/20020313/jt-2002-3-13-image003.jpg
图02:Visual C#调用Excel表格,并存储数据的程序运行界面
四.Visual C#处理Office套件中的其他成员程序:
本文虽然只介绍了Visual C#在处理Excel表格中经常遇到的一些问题的解决方法,但其实对Office套件的其他成员也有很强的借鉴意义,譬如Visual C#来处理Word文档,在调用Word文档的时候,必须先完成COM组件从非受管代码到受管代码的转换,Word的COM组件位"MSWORD9.OLB",经过转换后也会产生三个DLL文件,但分别是"Word.dll"、"Office.dll"、"VBIDE.dll"。其实在Visual C#中调用Word,也非常容易。只需要把调用Excel表格中的代码换成调用Word的代码就可以了,具体如下:
Word.Application word = new Word.Application ( ) ; word.Application.Visible = true ;
不信你试一下,看看是否达到你的要求。对于针对Word的其他的操作,总体来说和对Excel表格的操作相类似。由于针对Word只是一个文档,程序对Word进行的操作是比较少的,所以就不一一介绍了。
五.总结:
本文介绍Visual C#来处理Excel表格的几种最常遇到的情况,虽然针对的只是Excel表格,但对其他Office套件中的成员也具有十分的借鉴意义。
PIA下载地址:
http://www.microsoft.com/downloads/details.aspx?familyid=c41bd61e-3060-4f71-a6b4-01feba508e52&displaylang=en
异常详细信息: System.UnauthorizedAccessException: 拒绝访问。
ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}/ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。
若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
源错误:
行 21: private void Page_Load(object sender, System.EventArgs e) 行 22: { 行 23: Application exc = new Application(); 行 24: if (exc == null) 行 25: {
请问应该怎样设置???
第二个,在命令行运行Dos命令:dcomcnfg 在[Application]项目下选择[Microsoft Excel应用程序],点击[属性]按钮,在应用程序属性对话框 下选择第三项[Security],在三个单选钮上都选择[Use custom access permission],分别点击下 面的[Edit]按钮,增加用户[Everyone](本级)和[ASPNET],并设置权限为[Allow Access],
异常详细信息: System.Runtime.InteropServices.COMException: 服务器出现意外情况。
行 28: 行 29: Workbooks workbooks = exc.Workbooks; 行 30: _Workbook workbook=workbooks.Add(XlWBATemplate.xlWBATWorksheet); 行 31: //exc.Cells[1,1]="aaaaaaaa"; 行 32: exc.Visible=true;
源文件: c:/inetpub/wwwroot/tttest/webform1.aspx.cs 行: 30
不知道是哪里的问题??????
抱歉!评论已关闭.