问题描述
想要访问Excel中名为“转入”的sheet,写了一段往Excel添加数据的程序,总是提示,总是提示“INSERTINTO语句的语法错误”,想请明白的给指点一下,谢谢。PublicClassComeInPublicstrConnAsString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource='"&MainForm.fileName&"';ExtendedProperties=Excel8.0;"PublicComeIn_DSAsNewDataSetPublicComeIN_ADPAsNewOleDb.OleDbDataAdapter("SELECT*FROM[转入$]",strConn)PrivateSubComeIn_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadMe.MdiParent=MainForm'声明ListForm是MainForm的子窗口ComeIN_ADP.AcceptChangesDuringFill=TrueDimcmdBuilder_CIAsOleDb.OleDbCommandBuilder=NewOleDb.OleDbCommandBuilder(ComeIN_ADP)ComeIN_ADP.Fill(ComeIn_DS,"ComeInTable")EndSubPrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.ClickMe.Close()EndSubPrivateSubAddComeIn()Dimds_CiAsDataSet=ComeIn_DSDimnewRowAsDataRownewRow=ds_Ci.Tables("ComeInTable").NewRow()newRow("构件名称")=TB_Name.TextnewRow("构件编号")=TB_No.TextnewRow("铆焊日期")=TB_Date.TextnewRow("铆焊班组")=TB_Group.TextnewRow("经办人")=TB_Worker.TextnewRow("构件数量")=TB_Number.TextnewRow("铆焊量统计日期")=TB_TJDate.TextnewRow("铆焊量统计数量")=TB_TJNumber.Textds_Ci.Tables("ComeInTable").Rows.Add(newRow)EndSubPrivateSubSaveToDB()DimupdateDS1AsDataSetTryupdateDS1=ComeIn_DS.GetChangesIfNotupdateDS1IsNothingThenComeIN_ADP.Update(updateDS1,"ComeInTable")ComeIn_DS.AcceptChanges()EndIfCatchexAsExceptionMessageBox.Show(ex.Message.ToString,"更新数据库有错误")EndTryEndSubPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickAddComeIn()SaveToDB()EndSubEndClass
解决方案
本帖最后由 wlawei 于 2013-12-25 21:38:35 编辑
解决方案二:
你这里代码并没有“INSERTINTO语句阿
解决方案三:
看这问题是新手,我也是新手,我不帮你帮谁啊!不过VB操作EXCEL,但是老鸟了,用了6年了!不要把EXCEL当数据库使,这样虽在能用,但效果太差了!不是代码有问题,ExceL受字段格式的影响,做数据库出划率非常高VB.NET引用对象操作Excel才是正道!【项目】→『填写引用』→〖.NET选项卡〗选择[Microsoft.Office.Interop.Excel]以下是一个用OpenFileDialog打开一个EXCEL文件,写文件名以textbox1,并读取工作名称到LISTBOX1的例子ImportsExcel=Microsoft.Office.Interop.Excel''添加EXCEL引用PublicClassForm1PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click'====启动打开文件对话框OpenFileDialog1.FileName=""'OpenFileDialog1.InitialDirectory=Application.StartupPathOpenFileDialog1.Filter="MsExcel文件|*.xls*"OpenFileDialog1.FilterIndex=2OpenFileDialog1.RestoreDirectory=TrueIfOpenFileDialog1.ShowDialog()=DialogResult.OKThenTextBox1.Text=OpenFileDialog1.FileName'====启ExcelDimxlAppAsNewExcel.Application()xlApp.DisplayAlerts=FalseDimxlWorkBookAsExcel.Workbook'创建工作簿对象DimxlWorkSheetAsExcel.Worksheet'创建工作表xlWorkBook=xlApp.Workbooks.Open(TextBox1.Text)ListBox1.Items.Clear()Fori=1ToxlWorkBook.Sheets.CountxlWorkSheet=DirectCast(xlWorkBook.Worksheets(i),Excel.Worksheet)ListBox1.Items.Add(xlWorkSheet.Name)NextListBox1.SelectedIndex=0xlWorkBook.Close()xlApp.Quit()KillExcel(xlApp)xlApp=NothingEndIfEndSubEndClassForm1
给单元格赋值代码:xlWorkBook.Worksheets("Sheet1").Range("A5").Value=22保存写入并关闭Excel文件xlWorkBook.savexlWorkBook.close其他的问百度或查帮助!