2

I have a problem with delphi and excel ole automation. Use this code:

uses
ComObj
var
XlApp: Variant;
begin
L_v.Caption: = 'D:\bd1\support\Vi.xls';
XlApp: = CreateOleObject ('Excel.Application');
XLApp.Visible: = True;
XLApp.WorkBooks.Open (L_vi.Caption);
XLApp.Workbooks [1]. Worksheets. [1] Name: = 'Sheet1';
Sheets: = XLApp.Workbooks [1]. Worksheets ['test'];

but I get an error undeclared identifier 'Worksheets' at line .....

I do not understand why.

4

3 回答 3

2

(请参阅下面的我的编辑,标记为已更新:。)

如果这是您的实际代码

XLApp.Workbooks [1]. Worksheets. [1] Name: = 'Sheet1';

你有一个语法错误Worksheets.[1]Name。(.在错误的地方。)它应该是

XLApp.Workbooks[1].Worksheets[1].Name := 'Sheet1';

这在 XE3 中对我来说很好:

var
  XLApp: OleVariant;
begin
  XLApp := CreateOleObject('Excel.Application');
  XLApp.Visible := True;
  XLApp.Workbooks.Open('C:\Test\Test.xls');
  XLApp.Workbooks[1].Worksheets[1].Name := 'New Sheet Name';
end;

更新:在您发表评论后,我想我了解您可能遇到的问题。

我认为Error Insight这让您感到困惑(红色下划线和“未定义标识符”弹出窗口,实际上是错误的)。

我在此处发布的代码在发布之前已在 XE3 中编译并实际执行,并且可以正常工作。但是,如果我将光标放在 上Worksheets,我会看到Undeclared identifier弹出窗口和红色下划线。当然,它在一个方面是正确的——没有Worksheets被宣布。但是,当您使用后期绑定(在运行时使用)时,它不一定是。CreateOleObject

Error Insight是错误的(自从它被添加到 IDE 之后就一直存在)。我做的第一件事(当我看到错误尚未修复时)是关闭它(工具->选项->编辑器选项->代码洞察,取消选中Error Insight右侧)。让真正的编译器来解决问题。

于 2013-03-29T23:40:00.537 回答
0

尝试...

Var 
Sheet: Olevariant;
...
   for possheet := 1 to newWorkbook.Worksheets.Count do
   Begin
        Sheet:=newWorkbook.Sheets[possheet];
        combobox1.Items.Add(Sheet.Name)
   End;
于 2015-11-14T11:44:38.567 回答
0

通过这种方式在 delphi 中使用 excel 有点困难,因为您无法清楚地看到建议。

我将粘贴一些我拥有的代码VariantExcel以便您使用它。

uses
System.Win.ComObj, Excel2010
...

procedure GerarExcel(ADBGrid: TDBGrid; AQuery: TFDQuery);
var
  Excel: Variant;
  linha, coluna, I, X: Integer;
begin
  {Here you create the Excel Application Object}
  Excel := CreateOleObject('Excel.Application');
  {If you want to open the excel you can set this line to True.
  In case you want to save directly without user intervention you can set to False.}
  Excel.Visible :=True;
  Excel.Workbooks.Add;

  {In this case
  Titles begin on the (4) line and different columns (1-7)}
  Excel.WorkBooks[1].Sheets[1].Cells[4,1].Interior.ColorIndex := 55;
  Excel.WorkBooks[1].Sheets[1].Cells[4,1].Font.ColorIndex := 2;
  Excel.WorkBooks[1].Sheets[1].Cells[4,1].Font.Bold := True;
  Excel.WorkBooks[1].Sheets[1].Cells[4,1].font.size := 10;
  Excel.WorkBooks[1].Sheets[1].Cells[4,1] := 'Your Title 2';
  Excel.WorkBooks[1].Sheets[1].Cells[4,1].ColumnWidth := 15;
  
  ...

  Excel.WorkBooks[1].Sheets[1].Cells[4,7].Interior.ColorIndex := 55;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].Font.ColorIndex := 2;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].Font.Bold := True;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].font.size := 10;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7] := 'Your Title 7';
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].ColumnWidth := 17;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].HorizontalAlignment := 4;
  Excel.WorkBooks[1].Sheets[1].Cells[4,7].verticalAlignment   := 1;

  I := 1;
  {In case you want to loop through a TClientDateSet to fill in the cells}
  with AQuery do
  begin
    First;
    while not Eof do
    begin
      {To change to a specific format}
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 1].NumberFormat := 'dd/mm/aaaa';
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 1] := FieldByName(C_DATA).AsDateTime;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 2] := FieldByName(C_CONTA_CREDITO).AsString;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 3] := FieldByName(C_CONTA_DEBITO).AsString;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 4].NumberFormat := 'R$#.##0,00';
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 4] := FieldByName(C_VALOR).AsString;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 5] := FieldByName(C_DESCRICAO).AsString;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 6] := FieldByName(C_NOME).AsString;
      Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 7] := FieldByName(C_NOME_MOVIMENTO).AsString;
      Inc(I);

      Next;
    end;
  end;

  {All these variables configure the for landscape printing.}
  Excel.ActiveSheet.PageSetup.RightFooter := 'Página &P de &N';
  Excel.ActiveSheet.PageSetup.LeftFooter := '&D';
  Excel.ActiveSheet.PageSetup.Orientation := 2;
  Excel.ActiveSheet.PageSetup.PaperSize := xlPaperA4;
  Excel.ActiveSheet.PageSetup.FitToPagesTall := False;
  Excel.ActiveSheet.PageSetup.Zoom := False;
  Excel.ActiveSheet.PageSetup.FitToPagesWide := 1;

  {If you want the user to see the excel being opened and save it wherever the user wants
  You are done here.}

  {Save the file automatically
  If you want the user to NOT see the excel being opened and save the file in a specific folder automatically
  In the beginning you need to set Visible := False and then these lines}
  FilePath := 'C:\...\my_excel_file.xlsx';
  if FileExists(FilePath) then
    DeleteFile(FilePath);
  Excel.WorkBooks[1].Sheets[1].SaveAs(FilePath);

  Excel.Quit;
end;

VBA API Microsoft 文档中,您可以找到更多信息。

于 2020-09-13T14:29:54.997 回答