带纯色填充的C#EPPlus数据库条件格式
我正在生成包含多个百分比数据列的excel报告。由于这些报告是用于演示目的的,所以我希望通过使用带有实心填充的数据库格式化百分比数据,使它们看起来更漂亮。不知何故,这被证明是极其困难的,因为在EPPlus中没有直接设置solid fill for databar,但是我在这篇文章中找到了答案: 然而,无论我如何努力地编辑我的应用程序的代码,我只有一列以实心填充结束,其余为渐变。即使我将问题中的节点更改为节点列表,如下所示:带纯色填充的C#EPPlus数据库条件格式,c#,excel,xml,epplus,conditional-formatting,C#,Excel,Xml,Epplus,Conditional Formatting,我正在生成包含多个百分比数据列的excel报告。由于这些报告是用于演示目的的,所以我希望通过使用带有实心填充的数据库格式化百分比数据,使它们看起来更漂亮。不知何故,这被证明是极其困难的,因为在EPPlus中没有直接设置solid fill for databar,但是我在这篇文章中找到了答案: 然而,无论我如何努力地编辑我的应用程序的代码,我只有一列以实心填充结束,其余为渐变。即使我将问题中的节点更改为节点列表,如下所示: var cfNodes = xdoc.SelectNo
var cfNodes = xdoc.SelectNodes("/default:worksheet/default:conditionalFormatting/default:cfRule", nsm);
foreach(XmlNode cfNode in cfNodes)
{
cfNode.AppendChild(extLstCf);
}
对于工作表元素:
var wsNodes = xdoc.SelectNodes("/default:worksheet", nsm);
foreach(XmlElement wsNode in wsNodes)
{
wsNode.AppendChild(extLstWs);
}
我还尝试过使用xml更改
参数,但这仍然不能覆盖我的所有数据库列。我认为xml中必须有我可以改变的东西来实现我想要的,但我不知道要寻找什么…好了,伙计们,我花了几天时间,但我终于找到了答案。也许有一种更简单的方法可以做到这一点,但到目前为止,我就是这样让它为我工作的:
工作表xml扩展列表节点需要附加在工作表级别节点上,该节点包括工作表包含的数字数据条元素,对于实心填充I,每个元素都需要具有gradient=0
。例如,我的工作表包含两个数据条,因此我的工作表如下所示:
var extLstWs = xdoc.CreateNode(XmlNodeType.Element, "extLst", xdoc.DocumentElement.NamespaceURI);
extLstWs.InnerXml = @"<ext uri=""{78C0D931-6437-407d-A8EE-F0AAD7539E65}""
xmlns:x14=""http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"">
<x14:conditionalFormattings>
<x14:conditionalFormatting xmlns:xm=""http://schemas.microsoft.com/office/excel/2006/main"">
<x14:cfRule type=""dataBar"" id=""{3F3F0E19-800E-4C9F-9CAF-1E3CE014ED86}"">
<x14:dataBar minLength=""0"" maxLength=""100"" gradient=""0"">
<x14:cfvo type=""num"">
<xm:f>0</xm:f>
</x14:cfvo>
<x14:cfvo type=""num"">
<xm:f>100</xm:f>
</x14:cfvo>
<x14:negativeFillColor rgb=""FFFF0000""/><x14:axisColor rgb=""FF000000""/>
</x14:dataBar>
</x14:cfRule>
<xm:sqref>A1:A20</xm:sqref>
</x14:conditionalFormatting>
<x14:conditionalFormatting xmlns:xm=""http://schemas.microsoft.com/office/excel/2006/main"">
<x14:cfRule type=""dataBar"" id=""{3F3F0E19-800E-4C9F-9CAF-1E3CE014ED86}"">
<x14:dataBar minLength=""0"" maxLength=""100"" gradient=""0"">
<x14:cfvo type=""num"">
<xm:f>0</xm:f>
</x14:cfvo><x14:cfvo type=""num"">
<xm:f>200</xm:f>
</x14:cfvo><x14:negativeFillColor rgb=""FFFF0000""/>
<x14:axisColor rgb=""FF000000""/>
</x14:dataBar>
</x14:cfRule>
<xm:sqref>B1:B20</xm:sqref>
</x14:conditionalFormatting>
</x14:conditionalFormattings>
</ext>";
var wsNode = xdoc.SelectSingleNode("/default:worksheet", nsm);
wsNode.AppendChild(extLstWs);
var extLstWs=xdoc.CreateNode(XmlNodeType.Element,“extLst”,xdoc.DocumentElement.NamespaceURI);
extLstWs.InnerXml=@”
0
100
A1:A20
0
200
B1:B20
";
var wsNode=xdoc.SelectSingleNode(“/default:worksheet”,nsm);
AppendChild(extLstWs);
注意我是如何得到两个子节点的
,每个数据库一个子节点
其次,需要在
节点下追加另一个条件格式规则节点的扩展列表,每个数据库也有一个扩展列表。我能够使用foreach循环查找工作表中的所有数据库,并将相同的xml附加到每个数据库中,如下所示:
var cfNodes = xdoc.SelectNodes("/default:worksheet/default:conditionalFormatting/default:cfRule", nsm);
foreach (XmlNode cfnode in cfNodes)
{
var extLstCfNormal = xdoc.CreateNode(XmlNodeType.Element, "extLst", xdoc.DocumentElement.NamespaceURI);
extLstCfNormal.InnerXml = @"<ext uri=""{B025F937-C7B1-47D3-B67F-A62EFF666E3E}""
xmlns:x14=""http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"">
<x14:id>{3F3F0E19-800E-4C9F-9CAF-1E3CE014ED86}</x14:id></ext>";
cfnode.AppendChild(extLstCfNormal);
}
var cfNodes=xdoc.SelectNodes(“/default:worksheet/default:conditionalFormatting/default:cfRule”,nsm);
foreach(cfNodes中的XmlNode cfnode)
{
var extLstCfNormal=xdoc.CreateNode(XmlNodeType.Element,“extLst”,xdoc.DocumentElement.NamespaceURI);
extLstCfNormal.InnerXml=@”
{3F3F0E19-800E-4C9F-9CAF-1E3CE014ED86}”;
cfnode.AppendChild(extLstCfNormal);
}
在完成上述操作后,我终于能够用实心填充显示我的所有数据库 这是我的版本,为那些未能应用徐奥伟的解决方案的人(如我……)提供如何使数据库可靠填充的方法
public static Random Rnd=new Random();
公共静态字符串GenerateXLID()
{
//{29BD882A-B741-482B-9067-72CC5D939236}
string id=string.Empty;
对于(int i=0;i<32;i++)
如果(Rnd.NextDouble()<0.5)
id+=Rnd.Next(0,10);
其他的
id+=(char)Rnd.Next(65,91);
id=id。插入(8,“-”;
id=id。插入(13,“-”;
id=id。插入(18,“-”;
id=id。插入(23,“-”;
返回id;
}
公共静态void FixDatabarsAtWorksheet(OfficeOpenXml.ExcelWorksheet eworksheet)
{
System.Xml.XmlNodeList dataBar=eworksheet.WorksheetXml.GetElementsByTagName(“dataBar”);
如果(databar.Count>0)
{
string conditional_formattings_str=string.Empty;
for(int i=0;ipublic static Random Rnd = new Random();
public static string GenerateXlsId()
{
//{29BD882A-B741-482B-9067-72CC5D939236}
string id = string.Empty;
for (int i = 0; i < 32; i++)
if (Rnd.NextDouble() < 0.5)
id += Rnd.Next(0, 10);
else
id += (char)Rnd.Next(65, 91);
id = id.Insert(8, "-");
id = id.Insert(13, "-");
id = id.Insert(18, "-");
id = id.Insert(23, "-");
return id;
}
public static void FixDatabarsAtWorksheet(OfficeOpenXml.ExcelWorksheet eworksheet)
{
System.Xml.XmlNodeList databars = eworksheet.WorksheetXml.GetElementsByTagName("dataBar");
if (databars.Count > 0)
{
string conditional_formattings_str = string.Empty;
for (int i = 0; i < databars.Count; i++)
{
string temp_databar_id = GenerateXlsId();
databars[i].ParentNode.InnerXml += @"<extLst>
<ext uri=""{B025F937-C7B1-47D3-B67F-A62EFF666E3E}"" xmlns:x14=""http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"">
<x14:id>{" + temp_databar_id + @"}</x14:id>
</ext>
</extLst>";
//--
string temp_sqref = databars[i].ParentNode.ParentNode.Attributes["sqref"].Value;
string left_type = string.Empty;
string left_val = string.Empty;
string right_type = string.Empty;
string right_val = string.Empty;
string color = string.Empty;
Color databar_fill_color = Color.Empty;
Color databar_border_color = Color.Empty;
for (int j = 0; j < databars[i].ChildNodes.Count; j++)
if (databars[i].ChildNodes[j].LocalName == "cfvo" && databars[i].ChildNodes[j].Attributes["type"] != null)
{
if (string.IsNullOrEmpty(left_type))
left_type = databars[i].ChildNodes[j].Attributes["type"].Value;
else if (string.IsNullOrEmpty(right_type))
right_type = databars[i].ChildNodes[j].Attributes["type"].Value;
if (databars[i].ChildNodes[j].Attributes["val"] != null)
if (string.IsNullOrEmpty(left_val))
left_val = databars[i].ChildNodes[j].Attributes["val"].Value;
else if (string.IsNullOrEmpty(right_val))
right_val = databars[i].ChildNodes[j].Attributes["val"].Value;
}
else if (databars[i].ChildNodes[j].LocalName == "color")
{
color = databars[i].ChildNodes[j].Attributes["rgb"].Value;
int argb = Int32.Parse(color, System.Globalization.NumberStyles.HexNumber);
databar_fill_color = Color.FromArgb(argb);
databar_border_color = Color.FromArgb(255,
databar_fill_color.R - 50 < 0 ? databar_fill_color.R + 50 : databar_fill_color.R - 50,
databar_fill_color.G - 50 < 0 ? databar_fill_color.R + 50 : databar_fill_color.G - 50,
databar_fill_color.B - 50 < 0 ? databar_fill_color.R + 50 : databar_fill_color.B - 50);
}
string temp_conditional_formatting_template = @"<x14:conditionalFormatting xmlns:xm=""http://schemas.microsoft.com/office/excel/2006/main"">
<x14:cfRule type=""dataBar"" id=""{" + temp_databar_id + @"}"">
<x14:dataBar minLength=""" + (string.IsNullOrEmpty(left_val) ? "0" : left_val) + "\" maxLength=\"" + (string.IsNullOrEmpty(right_val) ? "100" : right_val) + "\" gradient=\"0\" " + (databar_border_color.IsEmpty ? string.Empty : "border = \"1\"") + ">";
temp_conditional_formatting_template += Environment.NewLine + "<x14:cfvo type=\"" + (left_type.ToLower() == "min" ? "autoMin" : left_type) + "\" />";
temp_conditional_formatting_template += Environment.NewLine + "<x14:cfvo type=\"" + (right_type.ToLower() == "max" ? "autoMax" : right_type) + "\" />";
if (!databar_border_color.IsEmpty)
temp_conditional_formatting_template += Environment.NewLine + "<x14:borderColor rgb=\"" + BitConverter.ToString(new byte[] { databar_border_color.A, databar_border_color.R, databar_border_color.G, databar_border_color.B }).Replace("-", "") + "\" />";
temp_conditional_formatting_template += Environment.NewLine + @"</x14:dataBar>
</x14:cfRule>
<xm:sqref>" + temp_sqref + @"</xm:sqref>
</x14:conditionalFormatting>";
conditional_formattings_str += temp_conditional_formatting_template;
}
databars[0].ParentNode.ParentNode.ParentNode.InnerXml += @"<extLst>
<ext uri=""{78C0D931-6437-407d-A8EE-F0AAD7539E65}"" xmlns:x14=""http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"">
<x14:conditionalFormattings>" + conditional_formattings_str + @"
</x14:conditionalFormattings>
</ext>
</extLst>";
}
}