Delphi和Excel.FormatConditions如何实现复杂数据条件格式化?

2026-04-10 03:141阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计305个文字,预计阅读时间需要2分钟。

Delphi和Excel.FormatConditions如何实现复杂数据条件格式化?

我在使用早期Excel 2010时,从Delphi XE2设置条件格式格式时遇到问题。我尝试使用以下代码:

vbaSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:==6

但遇到宏无法正常工作的宏。

我在使用早期绑定Excel 2010从Delphi XE2设置条件格式时遇到问题

我试图重现的宏如下:

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=6" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent6 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False

尝试我似乎无法访问相当于Selction.FormatConditions(1)的工作

我最接近的是使用以下代码:

XR := Xlapp.Range(...) XR.FormatConditions.Delete; XR.FormatConditions.Add(xlCellValue, xlGreater, '=6', EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);

哪个有效.当我尝试定义颜色时,我遇到了问题

FC := XR.FormatConditions[1]; FC.SetFirstPriority; with FC.Interior do begin PatternColorIndex := xlAutomatic; ThemeColor := xlThemeColorAccent6; end;

但是这一直告诉我XR.FormatConditions(1)和IDispatch因此与FormatCondition赋值不兼容

我究竟做错了什么?

Delphi和Excel.FormatConditions如何实现复杂数据条件格式化?

您需要使用Selection作为ExcelRange. Excel XP还要求第二个和第三个参数是OleVariant,所以这应该工作(无论如何编译):

var Sel: ExcelRange; Op, Formula: OleVariant; Condition: FormatCondition; begin Sel := ExcelApplication1.Selection[1] as ExcelRange; Op := xlGreater; Formula := '=6'; Sel.FormatConditions.Add(xlCellValue, Op, Formula, EmptyParam); Condition := Sel.FormatConditions[1] as FormatCondition; Condition.Interior.PatternColorIndex := xlAutomatic; // Do whatever else end;

本文共计305个文字,预计阅读时间需要2分钟。

Delphi和Excel.FormatConditions如何实现复杂数据条件格式化?

我在使用早期Excel 2010时,从Delphi XE2设置条件格式格式时遇到问题。我尝试使用以下代码:

vbaSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:==6

但遇到宏无法正常工作的宏。

我在使用早期绑定Excel 2010从Delphi XE2设置条件格式时遇到问题

我试图重现的宏如下:

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=6" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent6 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False

尝试我似乎无法访问相当于Selction.FormatConditions(1)的工作

我最接近的是使用以下代码:

XR := Xlapp.Range(...) XR.FormatConditions.Delete; XR.FormatConditions.Add(xlCellValue, xlGreater, '=6', EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);

哪个有效.当我尝试定义颜色时,我遇到了问题

FC := XR.FormatConditions[1]; FC.SetFirstPriority; with FC.Interior do begin PatternColorIndex := xlAutomatic; ThemeColor := xlThemeColorAccent6; end;

但是这一直告诉我XR.FormatConditions(1)和IDispatch因此与FormatCondition赋值不兼容

我究竟做错了什么?

Delphi和Excel.FormatConditions如何实现复杂数据条件格式化?

您需要使用Selection作为ExcelRange. Excel XP还要求第二个和第三个参数是OleVariant,所以这应该工作(无论如何编译):

var Sel: ExcelRange; Op, Formula: OleVariant; Condition: FormatCondition; begin Sel := ExcelApplication1.Selection[1] as ExcelRange; Op := xlGreater; Formula := '=6'; Sel.FormatConditions.Add(xlCellValue, Op, Formula, EmptyParam); Condition := Sel.FormatConditions[1] as FormatCondition; Condition.Interior.PatternColorIndex := xlAutomatic; // Do whatever else end;