當通過OLE從我的Delphi程序自動化Excel和嘗試一個單元格的NumberFormat屬性設置爲「一般」,並編寫公式,Excel是提高例外與此通知:如何使用Delphi正確設置公式的Excel NumberFormat屬性?
項目X.exe引發的異常類EVariantInvalidOpError與消息'變體操作無效'。
換句話說我的問題是: 哪個NumberFormat是包含文本函數(如concatenate)的公式單元格的最佳格式?
這裏是我的示例代碼:
USE System.Win.ComObj;
procedure TForm1.Btn_1Click(Sender: TObject);
var
XL: variant;
Sheet,
WorkBook: variant;
begin
try
XL := GetActiveOleObject('Excel.Application');
except
XL := CreateOleObject('Excel.Application');
end;
XL.Visible := False;
WorkBook:=XL.WorkBooks.Add;
Sheet:=XL.WorkBooks[1].WorkSheets.Add;
Sheet.Name := 'Test-Sheet';
Sheet.Cells[1, 1]:='col1';
Sheet.Cells[1, 1].Font.Bold := True;
Sheet.Cells[1, 1].Font.Size := 10;
Sheet.Cells[1, 2]:='col2';
Sheet.Cells[1, 2].Font.Bold := True;
Sheet.Cells[1, 2].Font.Size := 10;
Sheet.Cells[1, 3]:='Basic Formula';
Sheet.Cells[1, 3].Font.Bold := True;
Sheet.Cells[1, 3].Font.Size := 10;
Sheet.Cells[1, 3].numberformat := 'General';
Sheet.Cells[1, 4]:='Text function ';
Sheet.Cells[1, 4].Font.Bold := True;
Sheet.Cells[1, 4].Font.Size := 10;
Sheet.Cells[1, 4].numberformat := 'General';
Sheet.Cells[2, 1].Value := '1000';
Sheet.Cells[2, 2].Value := '500'; // or := 'abcxyz' for concat
Sheet.Cells[2, 3].Value := '=A2+B2'; //this one is ok
Sheet.Cells[2, 4].Value := '=CONCATENATE(A2;B2)'; //this one raised exception...
XL.WorkBooks[XL.WorkBooks.Count].SaveAs( ExtractFileDir(Application.ExeName) +'\Test.xls' ,51);
XL.ActiveWorkbook.Close(0);
XL.Quit;
end;
你試過':'而不是';'或''= A2&B2''? – pnuts
@pnuts:不,我沒有嘗試過,但你說的方式正常工作... 非常感謝你... – AliDehban