Excel导入SQLServer的三种方案_第1页
Excel导入SQLServer的三种方案_第2页
Excel导入SQLServer的三种方案_第3页
Excel导入SQLServer的三种方案_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、方案一:通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQLServeropenFileDialog=newOpenFileDialog();openFileDialog.Filter=Excelfiles(*.xls)|*.xls;if(openFileDialog.ShowDialog()=DialogResult.OK)FileInfofileInfo=newFileInfo(openFileDialog.FileName);stringfilePath=fileInfo.FullName;stringconnExcel=Provider=Microsoft.J

2、et.OLEDB.4.0;DataSource=+filePath+;ExtendedProperties=Excel8.0;tryOleDbConnectionoleDbConnection=newOleDbConnection(connExcel);oleDbConnection.Open();/获取excel表DataTabledataTable=oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);/获取sheet名,其中01.N:按名称排列的表单元素stringtableName=dataTable.Rows

3、02.ToString().Trim();tableName=+tableName.Replace(,)+;利用SQL语句从Excel文件里获取数据/stringquery=SELECTclassDate,classPlace,classTeacher,classTitle,classIDFROM+tableName;stringquery=SELECT日期,开课城市,讲师,课程名称,持续时间FROM+tableName;dataSet=newDataSet();/OleDbCommandoleCommand=newOleDbCommand(query,oleDbConnection);/Ol

4、eDbDataAdapteroleAdapter=newOleDbDataAdapter(oleCommand);OleDbDataAdapteroleAdapter=newOleDbDataAdapter(query,connExcel);oleAdapter.Fill(dataSet,gch_Class_Info);/dataGrid1.DataSource=dataSet;/dataGrid1.DataMember=tableName;dataGrid1.SetDataBinding(dataSet,gch_Class_Info);从excel文件获得数据后,插入记录到SQLServer

5、的数据表DataTabledataTable1=newDataTable();SqlDataAdaptersqlDA1=newSqlDataAdapter(SELECTclassID,classDate,classPlace,classTeacher,classTitle,durativeDateFROMgch_Class_Info,sqlConnection1);SqlCommandBuildersqlCB1=newSqlCommandBuilder(sqlDA1);sqlDA1.Fill(dataTable1);foreach(DataRowdataRowindataSet.Tablesg

6、ch_Class_Info.Rows)DataRowdataRow1=dataTable1.NewRow();dataRowlclassDate=dataRow日期;dataRowlclassPlace=dataRow开课城市;dataRow1classTeacher=dataRow讲师;dataRow1classTitle=dataRow课程名称;dataRow1durativeDate=dataRow持续时间;dataTable1.Rows.Add(dataRow1);Console.WriteLine(新插入+dataTable1.Rows.Count.ToString()+条记录);s

7、qlDA1.Update(dataTable1);oleDbConnection.Close();catch(Exceptionex)Console.WriteLine(ex.ToString();方案二:直接通过SQL语句执行SQLServer的功能函数将Excel文件转换到SQLServer数据库OpenFileDialogopenFileDialog=newOpenFileDialog();openFileDialog.Filter=Excelfiles(*.xls)|*.xls;SqlConnectionsqlConnection1=null;if(openFileDialog.Sho

8、wDialog()=DialogResult.OK)stringfilePath=openFileDialog.FileName;sqlConnection1=newSqlConnection();sqlConnection1.ConnectionString=server=(local);integratedsecurity=SSPI;initialcatalog=Library;/importexcelintoSQLServer2000/*stringimportSQL=SELECT*intolive41FROMOpenDataSource+(Microsoft.Jet.OLEDB.4.0

9、,DataSource=+E:022n.xls+;UserID=;Password=;Extendedproperties=Excel5.0).Sheet1$;*/exportSQLServer2000intoexcelstringexportSQL=EXECmaster.xp_cmdshellbcpLibrary.dbo.live41out+filePath+-c-q-S+-U+-P+;trysqlConnection1.Open();/SqlCommandsqlCommand1=newSqlCommand();/sqlCommand1.Connection=sqlConnection1;/

10、sqlCommand1.CommandText=importSQL;/sqlCommand1.ExecuteNonQuery();/MessageBox.Show(importfinish!);SqlCommandsqlCommand2=newSqlCommand();sqlCommand2.Connection=sqlConnection1;sqlCommand2.CommandText=exportSQL;sqlCommand2.ExecuteNonQuery();MessageBox.Show(exportfinish!);catch(Exceptionex)MessageBox.Sho

11、w(ex.ToString();if(sqlConnection1!=null)sqlConnection1.Close();sqlConnection1=null;方案三:通过到入Excel的VBAdll,通过VBA接口获取Excel数据到DataSetOpenFileDialogopenFile=newOpenFileDialog();openFile.Filter=Excelfiles(*.xls)|*.xls;ExcelIOexcelio=newExcelIO();if(openFile.ShowDialog()=DialogResult.OK)if(excelio!=null)exc

12、elio.Close();excelio=newExcelIO(openFile.FileName);object,range=excelio.GetRange();excelio.Close();DataSetds=newDataSet(xlsRange);intx=range.GetLength(0);inty=range.GetLength(1);DataTabledt=newDataTable(xlsTable);DataRowdr;DataColumndc;ds.Tables.Add(dt);for(intc=1;c=y;c+)dc=newDataColumn();dt.Columns.Add(dc);objecttemp=ne

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论