2012-06-27 41 views
0

我使用下面的代碼在Excel文件中插入值:如何使用ADO將值插入到Excel文件中?

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

原先的程序是完全顯示。

連接到Excel文件

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; 

誰能告訴我,我究竟做錯了什麼?

錯誤是「INSERT INTO語句中的語法錯誤」。

+3

請後的完整源代碼,其中包括正在使用的連接字符串和你收到錯誤。 – RRUZ

回答

1

實測溶液:

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

並祈禱這將與使用不同於您的區域設置的用戶協同工作。 –

相關問題