How to insert value into the Excel file using ADO?

房东的猫 提交于 2019-12-12 03:41:55

问题


I'm using the following code to insert value in the Excel file:

sAppend:='INSERT INTO ["Excel 8.0;Database=' + Edit1.Text + ';"].[Sheet1$] (d) ' + FormatDateTime('d/m/yyyy', now)+';';
AdoQuery1.SQL.Text:=sAppend;
AdoQuery1.ExecSQL;

Previous routine is completely shown.

Connection to the Excel file

procedure TForm1.ConnectToExcel;  
var strConn :  widestring;
begin
  strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
   'Data Source=' + Edit1.Text + ';' +
   'Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";Persist Security Info=False';

  AdoConnection1.Connected:=False;
  AdoConnection1.ConnectionString:=strConn;
  try
    AdoConnection1.Open;
    AdoConnection1.GetTableNames(ComboBox1.Items,True);
  except
    ShowMessage('Unable to connect to Excel, make sure the workbook ' + Edit1.Text +     'exist!');
    raise;
  end;
end;(*ConnectToExcel*)

procedure TForm1.FetchData;
begin
  StatusBar1.SimpleText:='';

  if not AdoConnection1.Connected then ConnectToExcel;

  AdoQuery1.Close;
  AdoQuery1.SQL.Text:=Edit2.Text;
  try
    AdoQuery1.Open;
  except
    ShowMessage('Unable to read data from Excel, make sure the query ' + Edit1.Text + ' is meaningful!');
    raise;
  end;
end;

Can anyone tell me what am I doing wrong ?

Error is "Syntax error in INSERT INTO statement".


回答1:


Found solution:

sAppend:='INSERT INTO [Sheet1$] (d) values ("' + DateToStr(now) +'")';
AdoQuery1.SQL.Text:=sAppend;
AdoQuery1.ExecSQL;


来源:https://stackoverflow.com/questions/11231845/how-to-insert-value-into-the-excel-file-using-ado

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!