Delphi导出到Excel-IDE中的可编译代码错误

问题描述

在Delphi 10.3.3中为Excel编码时,我在IDE中由于编译和工作正常的代码而出现了错误

错误消息...

Undeclaired identifier 'Range' at line xxx
Undeclaired identifier 'Cells' at line xxx

...每次出现两个excel变量。

这是完整的代码

procedure tmainform.ExportToExcel;
var
  xls,wb,MyRange,CompletionRow: OLEVariant;
  arrData: Variant;
  ColCount,CurrRow,CurrCol: Integer;
begin
  ColCount:= 6;
  arrData:= VararrayCreate([1,TotOrders+1,1,ColCount],varVariant);

  for CurrCol:= 1 to ColCount do case currcol of
     1: arrData[1,1]:= 'ORDER#';
     2: arrData[1,2]:= 'DATE';
     3: arrData[1,3]:= 'ITEM';
     4: arrData[1,4]:= 'QTY';
     5: arrData[1,5]:= 'COST';
     6: arrData[1,6]:= 'RECEIVED?';
  end;

  for CurrRow:= 1 to TotOrders do begin
     for CurrCol:= 1 to ColCount do begin
        case currcol of
           1: arrData[CurrRow+1,1]:= Orders[1].Number;
           2: arrData[CurrRow+1,2]:= Orders[2].Date;
           3: arrData[CurrRow+1,3]:= Orders[3].Name;
           4: arrData[CurrRow+1,4]:= Orders[4].Qty;
           5: arrData[CurrRow+1,5]:= Orders[5].Cost;
           6: arrData[CurrRow+1,6]:= Orders[6].Received;
        end;
     end;
  end;

  xls:= CreateOLEObject('Excel.Application');
  wb:= xls.Workbooks.Add;
{THIS LINE}  MyRange:= wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1,1],wb.WorkSheets[1].Cells[TotOrders+1,ColCount]];     // THIS LINE
  MyRange.Value:= arrData;
  MyRange.EntireColumn.Autofit;
{THIS LINE}  CompletionRow:= wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1,wb.WorkSheets[1].Cells[1,ColCount]];     // THIS LINE
  CompletionRow.font.bold := true;
{THIS LINE}  CompletionRow:= wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[2,5],5]];     // THIS LINE
  CompletionRow.NumberFormat:= '$#,##0.00';
  xls.Visible:= True;
  xls.ActiveWindow.Activate;
end;

全局变量是:

  Orders: array of ordertype;
  TotOrders: integer;

同样,这段代码可以正常运行,但是我想摆脱那些总是显示在左上方的假错误

fake errors - code compiles

解决方法

这是Delphi中的一个已知错误。除了升级到版本“ 10.4 Sydney”(或更高版本)Reference {},您无能为力。