问题描述
代码还是有点问题,程序不报错,但是数据导入不成功,我是仿照sqlserver写的,不知哪里有问题?PublicSharedFunctionLoadDataFromExcel(ByValdatagridviewAsDataGridView)AsSystem.Data.DataTableLoadDataFromExcel=NothingTryDimOpenFileDialog1AsNewOpenFileDialogOpenFileDialog1.Filter="Excel文件|*.xls;*.xlsx"'"Excel文件(*.xls)|*.xls;*.xlsx|"IfOpenFileDialog1.ShowDialog()=DialogResult.OKThenDimfilePathAsString=OpenFileDialog1.FileNameIfNotSystem.IO.Path.GetExtension(filePath)Like".xls*"ThenMessageBox.Show("导入Excel失败!失败原因:选择的不是Excel文件","错误提示")EndIfDimstrConnAsString'Excel07及以下版本'strConn="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+filePath+";ExtendedProperties='Excel8.0;HDR=False;IMEX=1'"'Excel2010版本及以下strConn="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="+filePath+";ExtendedProperties='Excel8.0;HDR=False;IMEX=1'"DimOleConnAsNewOleDbConnection(strConn)OleConn.Open()DimsqlAsString="SELECT*FROM[Sheet1$]"DimOleDaExcelAsNewOleDb.OleDbDataAdapter(Sql,strConn)DimOleDsExcleAsNewDataSetOleDaExcel.Fill(OleDsExcle,"Sheet1")OleConn.Close()DimmyconnAsNewOracleConnection(myCnn)DimmystrsqlAsString="Select*from登陆用户数"DimmyoledaAsNewOracleDataAdapter(mystrsql,myconn)DimmycbAsOracleCommandBuilderIfOleDsExcle.Tables.Item(0).Rows.Count=0ThenMessageBox.Show("导入Excel失败!失败原因:选择的Excel中没有数据","错误提示")ElseIfMessageBox.Show("是否需要预览导入的表格内容?","提示信息",MessageBoxButtons.YesNo)=Windows.Forms.DialogResult.YesThenmycb=NewOracleCommandBuilder(myoleda)LoadDataFromExcel=OleDsExcle.Tables.Item(0)myoleda.InsertCommand=mycb.GetInsertCommand()myoleda.Fill(OleDsExcle,"登陆用户数")datagridview.DataSource=OleDsExcle.Tables(0)myoleda.Update(OleDsExcle,"登陆用户数")EndIfEndIfEndIfCatchexAsExceptionMessageBox.Show("数据绑定Excel失败!失败原因:"+ex.Message,"错误提示")EndTryEndFunction