添加条件格式 OpenXML C#
Add Conditional Formatting OpenXML C#
如何使用 C# .Net 在 OpenXML 中添加条件格式。我想应用以下条件:
=INDIRECT("D"&ROW())="Disapproved" 那么规则应该适用于:=$1:$3,$N$4:$XFD$4,$5:$1048576
我的函数设置如下:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(openFileDialog1.FileName, true))
{
// apply conditions here
}
要添加条件格式,您需要添加一个ConditionalFormatting
instance to the Worksheet
. This object will hold the list of references that the conditional format should be applied to. The ConditionalFormatting
instance needs a ConditionalFormattingRule
to define the rule to be used which in turn is defined via a Formula
。
为了使条件格式对电子表格产生影响,您还需要定义要使用的样式。这需要添加到 DifferentialFormat
, which in turn is added to a DifferentialFormats
.
以下代码将采用现有文档并添加您所追求的条件格式:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename, true))
{
WorkbookPart workbookPart = document.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
//grab the stylesPart so we can add the style to apply (create one if one doesn't already exist)
WorkbookStylesPart stylesPart = document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
if (stylesPart == null)
{
stylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = new Stylesheet();
}
//create a fills object to hold the background colour we're going to apply
Fills fills = new Fills() { Count = 1U };
//grab the differential formats part so we can add the style to apply (create one if one doesn't already exist)
bool addDifferentialFormats = false;
DifferentialFormats differentialFormats = stylesPart.Stylesheet.GetFirstChild<DifferentialFormats>();
if (differentialFormats == null)
{
differentialFormats = new DifferentialFormats() { Count = 1U };
addDifferentialFormats = true;
}
//create the conditional format reference
ConditionalFormatting conditionalFormatting = new ConditionalFormatting()
{
SequenceOfReferences = new ListValue<StringValue>()
{
InnerText = "A1:XFD3 N4:XFD4 A5:XFD1048576"
}
};
//create a style to assign to the conditional format
DifferentialFormat differentialFormat = new DifferentialFormat();
Fill fill = new Fill();
PatternFill patternFill = new PatternFill();
BackgroundColor backgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "0000ff00" } };
patternFill.Append(backgroundColor);
fill.Append(patternFill);
differentialFormat.Append(fill);
differentialFormats.Append(differentialFormat);
//create the formula
Formula formula1 = new Formula();
formula1.Text = "INDIRECT(\"D\"&ROW())=\"Disapproved\"";
//create a new conditional formatting rule with a type of Expression
ConditionalFormattingRule conditionalFormattingRule = new ConditionalFormattingRule()
{
Type = ConditionalFormatValues.Expression,
FormatId = 0U,
Priority = 1
};
//append the formula to the rule
conditionalFormattingRule.Append(formula1);
//append th formatting rule to the formatting collection
conditionalFormatting.Append(conditionalFormattingRule);
//add the formatting collection to the worksheet
//note the ordering is important; there are other elements that should be checked for here really.
//See the spec for all of them and see
//for more details on ordering
PageMargins margins = worksheetPart.Worksheet.GetFirstChild<PageMargins>();
if (margins != null)
worksheetPart.Worksheet.InsertBefore(conditionalFormatting, margins);
else
worksheetPart.Worksheet.Append(conditionalFormatting);
//add the differential formats to the stylesheet if it didn't already exist
if (addDifferentialFormats)
stylesPart.Stylesheet.Append(differentialFormats);
}
请注意,OpenXml 对元素的顺序很挑剔,因此(尤其是在处理现有文档时)您需要确保在树中的正确位置添加元素。
如何使用 C# .Net 在 OpenXML 中添加条件格式。我想应用以下条件:
=INDIRECT("D"&ROW())="Disapproved" 那么规则应该适用于:=$1:$3,$N$4:$XFD$4,$5:$1048576
我的函数设置如下:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(openFileDialog1.FileName, true))
{
// apply conditions here
}
要添加条件格式,您需要添加一个ConditionalFormatting
instance to the Worksheet
. This object will hold the list of references that the conditional format should be applied to. The ConditionalFormatting
instance needs a ConditionalFormattingRule
to define the rule to be used which in turn is defined via a Formula
。
为了使条件格式对电子表格产生影响,您还需要定义要使用的样式。这需要添加到 DifferentialFormat
, which in turn is added to a DifferentialFormats
.
以下代码将采用现有文档并添加您所追求的条件格式:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename, true))
{
WorkbookPart workbookPart = document.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
//grab the stylesPart so we can add the style to apply (create one if one doesn't already exist)
WorkbookStylesPart stylesPart = document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
if (stylesPart == null)
{
stylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = new Stylesheet();
}
//create a fills object to hold the background colour we're going to apply
Fills fills = new Fills() { Count = 1U };
//grab the differential formats part so we can add the style to apply (create one if one doesn't already exist)
bool addDifferentialFormats = false;
DifferentialFormats differentialFormats = stylesPart.Stylesheet.GetFirstChild<DifferentialFormats>();
if (differentialFormats == null)
{
differentialFormats = new DifferentialFormats() { Count = 1U };
addDifferentialFormats = true;
}
//create the conditional format reference
ConditionalFormatting conditionalFormatting = new ConditionalFormatting()
{
SequenceOfReferences = new ListValue<StringValue>()
{
InnerText = "A1:XFD3 N4:XFD4 A5:XFD1048576"
}
};
//create a style to assign to the conditional format
DifferentialFormat differentialFormat = new DifferentialFormat();
Fill fill = new Fill();
PatternFill patternFill = new PatternFill();
BackgroundColor backgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "0000ff00" } };
patternFill.Append(backgroundColor);
fill.Append(patternFill);
differentialFormat.Append(fill);
differentialFormats.Append(differentialFormat);
//create the formula
Formula formula1 = new Formula();
formula1.Text = "INDIRECT(\"D\"&ROW())=\"Disapproved\"";
//create a new conditional formatting rule with a type of Expression
ConditionalFormattingRule conditionalFormattingRule = new ConditionalFormattingRule()
{
Type = ConditionalFormatValues.Expression,
FormatId = 0U,
Priority = 1
};
//append the formula to the rule
conditionalFormattingRule.Append(formula1);
//append th formatting rule to the formatting collection
conditionalFormatting.Append(conditionalFormattingRule);
//add the formatting collection to the worksheet
//note the ordering is important; there are other elements that should be checked for here really.
//See the spec for all of them and see
//for more details on ordering
PageMargins margins = worksheetPart.Worksheet.GetFirstChild<PageMargins>();
if (margins != null)
worksheetPart.Worksheet.InsertBefore(conditionalFormatting, margins);
else
worksheetPart.Worksheet.Append(conditionalFormatting);
//add the differential formats to the stylesheet if it didn't already exist
if (addDifferentialFormats)
stylesPart.Stylesheet.Append(differentialFormats);
}
请注意,OpenXml 对元素的顺序很挑剔,因此(尤其是在处理现有文档时)您需要确保在树中的正确位置添加元素。