从Delphi更新Excel ADO的单元格查询 [英] Update cell query for Excel ADO from Delphi

查看:235
本文介绍了从Delphi更新Excel ADO的单元格查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码打开我的Excel工作表,使用TADOConnection和TADOQuery:

I have the following code to open my Excel Worksheet, using TADOConnection and TADOQuery:

conExcel.ConnectionString := 'Provider=Microsoft.JET.OLEDB.4.0;Data Source="D:\temp\file.xls";Extended Properties="Excel 8.0;HDR=No"';
conExcel.Connected := true;

当我运行以下代码:

queryExcel1.SQL.Text := 'SELECT * FROM [Hoja1$]';
queryExcel1.Open;
while not queryExcel1.eof do
begin
  showmessage(queryExcel1.Fields.Fields[0].AsString);
  queryExcel1.Next;
end;

我得到文件的每一行,所以它工作正常,但我想更新一个单元格。
我使用这个代码:

I get each line of the file, so it is working ok, but I want to update a cell. I am using this code:

queryExcel2.SQL.Text := 'UPDATE [Hoja1$] SET F1 = 555555';
queryExcel2.ExecSQL;

不知何故将工作表上的每个A单元格更新为555555,但我真正想要的是只需将A1单元格设置为555555.有关如何包括查询的WHERE部分的任何提示。谢谢。

Which somehow updates every "A" cell on the worksheet to 555555, but what I really want is to just set the A1 cell to 555555. Any hints on how to include the WHERE section of the query. Thanks.

推荐答案

如果要使用Adoquery,您将必须将ParamCheck设置为false,因为您必须使用为您要更新的范围。

另一个选项是直接使用连接。

在下面的示例中范围B2:B2与F1一起使用,F1是第一列的默认名称Jet。 (B2是我们要更新的单元格)

If you want to use an Adoquery you will have to set ParamCheck to false, since you will have to use : for the range you want to update.
An other option would be to use the connection directly.
In the example below the range B2:B2 is used with F1 which is the default name of Jet for the first column. (B2 is the cell we want to update)

  Q.SQL.Text := 'UPDATE [Hoja1$B2:B2] SET [F1] = 555555';
  Q.ExecSQL;
  // or:
  //AdoConnection1.Execute('UPDATE [Hoja1$B2:B2] SET [F1] = 555555'); 

这篇关于从Delphi更新Excel ADO的单元格查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆