Delphi - 设置Excel单元格背景颜色渐变
问题描述:
Delphi西雅图,Excel 2013.我需要设置一个单元格的背景为渐变。如果它是单色,我可以设置背景颜色,但我无法获得渐变的语法。部分挑战在于细胞的渐变是一个IDispatch。以下代码将设置单个背景颜色。Delphi - 设置Excel单元格背景颜色渐变
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宏设置我想gradiant(线性,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?
谢谢
答
使用延迟绑定访问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;
+0
就是这样。我不得不使用迟绑定。 – user1009073
您是否尝试将.Gradient分配给OleVariant,然后使用迟绑定调用?例如。 'vGradient.Degree:= 0' – MartynA