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

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; 

当我录制EXCELmacros设置我想要的灰度(线性,2色,绿色到黄色),macros是

 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。 如何设置其他“子属性”,如Degree和Colorstops?

谢谢

使用延迟绑定访问IDispatch接口上的方法/属性。

  ... 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;