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宏观设置我想要的渐变(线性,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
我应该在德尔福中做的是...
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。我如何设置其他"子专业",例如学位和配色?
谢谢
在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;