Delphi Seattle,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 Macro设置我想要的渐变色(线性,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。如何设置其他“子属性”,如Degree和Colorstops?
由于
答案 0 :(得分:2)
使用后期绑定访问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;