C# 在OpenXML C中添加条件格式#
如何使用C#.Net在OpenXML中添加条件格式。我希望适用以下条件: =间接(“D”&ROW())=“未批准”,则规则应适用于:=$1:$3,$N$4:$XFD$4,$5:$1048576 我的函数设置如下:C# 在OpenXML C中添加条件格式#,c#,.net,openxml,C#,.net,Openxml,如何使用C#.Net在OpenXML中添加条件格式。我希望适用以下条件: =间接(“D”&ROW())=“未批准”,则规则应适用于:=$1:$3,$N$4:$XFD$4,$5:$1048576 我的函数设置如下: using (SpreadsheetDocument document = SpreadsheetDocument.Open(openFileDialog1.FileName, true)) { // apply conditions here } 要添加条件格式,需要将实例添
using (SpreadsheetDocument document = SpreadsheetDocument.Open(openFileDialog1.FileName, true))
{
// apply conditions here
}
要添加条件格式,需要将实例添加到
工作表中。此对象将保存条件格式应应用于的引用列表。ConditionalFormatting
实例需要一个来定义要使用的规则,而该规则又通过一个来定义
为了使条件格式对电子表格产生影响,还需要定义要使用的样式。这需要添加到,然后再添加到
以下代码将获取现有文档并添加您所追求的条件格式:
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 https://stackoverflow.com/questions/25398450/why-appending-autofilter-corrupts-my-excel-file-in-this-example/25410242#25410242
//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);
}
使用(SpreadsheetDocument=SpreadsheetDocument.Open(filename,true))
{
WorkbookPart WorkbookPart=document.WorkbookPart;
//得到正确的表格
Sheet Sheet=workbookPart.Workbook.Subjections()。其中(s=>s.Name==sheetName.First();
WorksheetPart WorksheetPart=workbookPart.GetPartById(sheet.Id)作为工作表部件;
SheetData SheetData=worksheetPart.Worksheet.Elements().First();
//抓取stylesPart,以便我们可以添加要应用的样式(如果还不存在样式,则创建一个)
WorkbookStylesPart-stylesPart=document.WorkbookPart.GetPartSoftType().FirstOrDefault();
if(stylesPart==null)
{
stylesPart=workbookPart.AddNewPart();
stylesPart.Stylesheet=新样式表();
}
//创建一个填充对象来保持我们要应用的背景色
填充=新填充(){Count=1U};
//抓取“差异格式”部分,以便我们可以添加要应用的样式(如果还没有样式,请创建一个)
bool adddifferentialsformats=false;
DifferentingFormats differentingFormats=stylesPart.Stylesheet.GetFirstChild();
if(differentingformats==null)
{
DifferentingFormats=新的DifferentingFormats(){Count=1U};
addDifferentialsFormats=true;
}
//创建条件格式引用
ConditionalFormatting ConditionalFormatting=新的ConditionalFormatting()
{
SequenceOfReferences=新列表值()
{
InnerText=“A1:XFD3 N4:XFD4 A5:XFD1048576”
}
};
//创建要指定给条件格式的样式
DifferentingFormat DifferentingFormat=新的DifferentingFormat();
填充=新填充();
PatternFill PatternFill=新的PatternFill();
BackgroundColor BackgroundColor=new BackgroundColor(){Rgb=new HexBinaryValue(){Value=“0000ff00”};
patternFill.Append(背景色);
填充。追加(填充图案);
格式。追加(填充);
differentingformats.Append(differentingformat);
//创建公式
公式公式1=新公式();
公式1.Text=“间接(\“D\”和ROW())=\“不批准”;
//使用表达式类型创建新的条件格式规则
ConditionalFormattingRule ConditionalFormattingRule=新的ConditionalFormattingRule()
{
类型=条件格式值。表达式,
FormatId=0U,
优先级=1
};
//将公式附加到规则
附加条件格式规则(公式1);
//将格式规则附加到格式集合
附加(conditionalFormatting规则);
//将格式化集合添加到工作表中
//注意,排序很重要;这里还有其他元素需要检查。
//请参阅所有这些的规格,并参阅https://stackoverflow.com/questions/25398450/why-appending-autofilter-corrupts-my-excel-file-in-this-example/25410242#25410242
//有关订购的详细信息,请参阅
PageMargins=worksheetPart.Worksheet.GetFirstChild();
如果(页边距!=null)
工作表部分。工作表。插入之前(条件格式,页边距);
其他的
工作表部分工作表附加(条件格式);
//如果样式表不存在,则将差异格式添加到样式表中
如果(添加格式)
stylesPart.Stylesheet.Append(格式);
}
请注意,OpenXml对元素的顺序很挑剔,因此(尤其是在处理现有文档时),您需要确保在树中的正确位置添加元素。太好了,代码似乎可以工作,但我不知道如何根据需要设置formula1.Text。我只想选择包含“#”字符的单元格。你能帮助我吗?