下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年财务预测与模型企业制定与实施新质生产力战略分析研究报告
- 自动结算系统行业跨境出海战略分析报告
- 2025-2030年中国钛合金合金应用研究行业前景趋势预测及发展战略咨询报告
- 保安制度及相关处罚
- 软件开发文档模板
- GBT 23132-2024 电动剃须刀标准立项发展报告
- 人教版高中生物必修一全套教案
- GBT 25304-2025《非血管自扩张金属支架系统》标准立项发展报告
- GBT 33017.6-2024 高效能大气污染物控制装备评价技术要求 第6部分:湿式电除尘器标准立项发展报告
- 危险化学品事故管理制度
- 2025-2026学年人教鄂教版(2024)小学科学三年级上册教学计划及进度表
- 刑事科学技术授课
- 县级医院胸痛中心建设汇报总结
- 2025年高中英语教师教材教法考试测试卷及参考答案
- 海底设施的智能化决策支持系统研究-洞察阐释
- CJ/T 475-2015微孔曝气器清水氧传质性能测定
- 唐宋八大家文学精讲
- 临床液体外渗的预防与处理要点
- 临时占地合同范例
- 全国人教版高中信息技术必修一第1章1.3数据科学与大数据1.3.2《大数据及其应用》说课稿
- DB11∕T 2114-2023 水利工程施工质量验收管理规程
评论
0/150
提交评论