2016-12-16 526 views
1

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?

謝謝

+1

您是否嘗試將.Gradient分配給OleVariant,然後使用遲綁定調用?例如。 'vGradient.Degree:= 0' – MartynA

回答

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; 
+0

就是這樣。我不得不使用遲綁定。 – user1009073

相關問題