Delphi - 设置 Excel 单元格背景颜色渐变

Delphi - Setting Excel Cell Background Color Gradient

Delphi 西雅图,Excel 2013。我需要将单元格的背景设置为渐变。如果它是单一颜色,我可以设置背景颜色,但我无法获得适合渐变的语法。部分挑战在于单元格的渐变是 IDispatch。以下代码将设置单一背景色。

    procedure TForm1.GradientTestClick(Sender: TObject);
var
  oExcel : ExcelApplication;
  RawDataSheet :_Worksheet;
  ThisCell : ExcelRange;
begin
    oExcel := CreateOleObject('Excel.Application') as ExcelApplication;
    oExcel.Visible[LOCALE_USER_DEFAULT] := True;

   // Add a New Workbook, with a single sheet
   oExcel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
   // Get the handle to the active Sheet, and insert some dummy data
   RawDataSheet :=  oExcel.ActiveSheet as _Worksheet;
   ThisCell := RawDataSheet.Range['A1', EmptyParam];
   ThisCell.Value2 := 10;


   // To set ONE Color
   ThisCell.Interior.Pattern := xlSolid;
   ThisCell.Interior.ColorIndex := 3;

   // To Set Gradient...


end;

当我录制一个 EXCEL 宏设置我想要的渐变(线性,2 色,绿色到黄色)时,宏是

Sub Macro1()
'
' Macro1 Macro
'

'
    With Selection.Interior
        .Pattern = xlPatternLinearGradient
        .Gradient.Degree = 0
        .Gradient.ColorStops.Clear
    End With
    With Selection.Interior.Gradient.ColorStops.Add(0)
        .Color = 5296274
        .TintAndShade = 0
    End With
    With Selection.Interior.Gradient.ColorStops.Add(1)
        .Color = 65535
        .TintAndShade = 0
    End With
End Sub

我在 Delphi 中应该能够做的是...

  ThisCell.Interior.Pattern := xlPatternLinearGradient;
  ThisCell.Interior.Gradient.Degree := 0;
  ThisCell.Interior.Gradient.ColorStops.Clear;
  ThisCell.Interior.Gradient.ColorStops.Add[0].Color := 5296274;
  ThisCell.Interior.Gradient.ColorStops.Add[1].Color := 65535;

我的挑战是 ThisCell.Interior.Gradient 是一个 IDispatch。如何设置其他 "sub-properties",例如 Degree 和 Colorstops?

谢谢

使用后期绑定访问 IDispatch 界面上的 methods/properties。

  ...
  Gradient: OleVariant;
begin
   ....

   // To Set Gradient...

  ThisCell.Interior.Pattern := xlPatternLinearGradient;
  Gradient := ThisCell.Interior.Gradient;
  Gradient.Degree := 45;
  Gradient.ColorStops.Clear;
  Gradient.ColorStops.Add(0).Color := 5296274;
  Gradient.ColorStops.Add(1).Color := 65535;