code ole自动化excel错误

时间:2013-03-31 00:12:01

标签: delphi delphi-xe3

此代码给出了错误

ex.Cells [3, 2]. HorizontalAlignment: = xlright;

ex.Cells [row, 1]. NumberFormat: = 'dd / mm / yy, @';

我该如何解决这个问题?

感谢

1 个答案:

答案 0 :(得分:1)

根据您的其他问题,您使用后期绑定(使用Variant)而不是早期绑定(使用VCL TExcelApplication和其他组件)。这意味着未定义xlRight等特定类型,因为您尚未使用提供它们的类型库导入。你必须自己定义它们:

var
  ...
  NewAlign: OleVariant;
const
  xlBottom = $FFFFEFF5;
  xlLeft = $FFFFEFDD;
  xlRight = $FFFFEFC8;
  xlTop = $FFFFEFC0;
  xlHAlignCenter = $FFFFEFF4;
  xlHAlignCenterAcrossSelection = $00000007;
  xlHAlignDistributed = $FFFFEFEB;
  xlHAlignFill = $00000005;
  xlHAlignGeneral = $00000001;
  xlHAlignJustify = $FFFFEFDE;
  xlHAlignLeft = $FFFFEFDD;
  xlHAlignRight = $FFFFEFC8;
begin
  // XLApp is the instance of Excel you set up from your previous
  // questions
  XLApp.Workbooks[1].Worksheets[1].Range['C1:C5'].NumberFormat := 'dd/mm/yy';
  NewAlign := xlHAlignRight;
  XLApp.Workbooks[1].Worksheets[1].Range['C1:C5'].HorizontalAlignment := NewAlign;
end;

分配dd/mm/yy @没有任何意义,因为它是日期格式dd/mm/yy或文字@,但不是两者。