欢迎来到天天文库
浏览记录
ID:22679628
大小:51.00 KB
页数:7页
时间:2018-10-30
《c操作excel全解代码》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、C#操作EXCEL全解(代码)提示:运行之前必须先引用Interop.Excel.dll模块usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Windows.Forms;usingSystem.Reflection;usingExcel;namespaceAnalysisSystem.DB{publicclassExcelOper
2、ation{privatestring_fileName;//保存路径名publicExcelOperation(stringfileName){_fileName=fileName;}privateOleDbConnectionGetConnection(){stringconnectString=string.Format("Provider=Microsoft.Jet.OLEDB.4.0;DataSource={0};ExtendedProperties=Excel8.0",_fileName);
3、OleDbConnectionmyConn=newOleDbConnection(connectString);//建立链接returnmyConn;}publicSystem.Data.DataTableExecuteTableResult(stringstrSql){System.Data.DataTabledt=newSystem.Data.DataTable();try{OleDbConnectionconn=this.GetConnection();OleDbDataAdapterda=new
4、OleDbDataAdapter(strSql,conn);//执行相关SQL语句da.Fill(dt);}catch(System.Exceptionex){//donothing}returndt;}publicDataSetExecuteSetResult(stringstrSql,stringtable_name){DataSetds=newDataSet();stringtemp_name="["+table_name+"$]";try{OleDbConnectionconn=this.Get
5、Connection();OleDbDataAdapterda=newOleDbDataAdapter(strSql,conn);da.Fill(ds,temp_name);}catch(System.Exceptionex){//donothing}returnds;}publicstringExecuteOneResult(stringstrSql){stringresult="";System.Data.DataTabledt=newSystem.Data.DataTable();try{OleD
6、bConnectionconn=this.GetConnection();OleDbDataAdapterda=newOleDbDataAdapter(strSql,conn);da.Fill(dt);}catch(System.Exceptionex){//donothing}if(dt!=null&&dt.Rows.Count>0){result=dt.Rows[0][0].ToString();}returnresult;}publicvoidExecuteNonResult(stringstrS
7、ql){try{OleDbConnectionconn=this.GetConnection();OleDbCommandcmd=newOleDbCommand(strSql,conn);cmd.ExecuteNonQuery();}catch(System.Exceptionex){//donothing}}private_WorkbookW_B(Excel.Applicationapp){Workbooksworkbooks=app.Workbooks;_Workbookworkbook=workb
8、ooks.Add(XlWBATemplate.xlWBATWorksheet);returnworkbook;}private_WorksheetW_S(_Workbookwork_book){Sheetssheets=work_book.Worksheets;_Worksheetworksheet=(_Worksheet)sheets.get_Item(1);//获取选择第一个表,本身默认名称为Sheet1returnworksheet;
此文档下载收益归作者所有