XmlDocument xml = new XmlDocument();
xml.Load(Way);//获取相对路径
var Exports = xml.GetElementsByTagName("Export");//获取所有Export节点
var WorkSheets = xml.GetElementsByTagName("WorkSheet");//获取所有WorkSheet节点
for (int i = 0; i < Exports.Count; i++)
{
if (Exports[i].Attributes["type"].InnerText == "AMeterCostAnnual_Scale")//匹配type属性的的节点
{
Exports[i].Attributes["FileName"].InnerText = SYear + "至" + EYear + "年水电费同比环统计.xls";//修改文件名
}
if (WorkSheets[i].Attributes["SqlId"].InnerText == "Business.AMeterCostAnnual")//匹配SqlId属性的节点
{
XmlNode root = xml.GetElementsByTagName("WorkSheet")[i];//获取要修改的WorkSheet节点
for (int j = 0; j < dt.Columns.Count; j++)
{
string Name = dt.Columns[j].ColumnName;
string Num = Convert.ToChar(‘A‘ + j).ToString();
string Title = string.Empty;
string status = string.Empty;
if (j == 0)
{
int ChildNum = root.ChildNodes.Count;
for (int k = 0; k < ChildNum; k++)//第一次进入,循环移除所有子节点、属性
{
root.RemoveChild(root.FirstChild);
}
Title = "区域";//修改导出文件中的标题
}
else
{
status = Name.Substring(4, 1);
if (status == "P")
{
Title = Name.Substring(0, 4) + "年" + Action + "电费";
}
else
{
Title = Name.Substring(0, 4) + "年" + Action + "同比增幅";
}
}
XmlElement elem = xml.CreateElement("Column");
elem.AddAttriubte("ConvertType", "Normal");//设置属性
elem.AddAttriubte("Required", "true");
elem.AddAttriubte("ColumnName", Num);
elem.AddAttriubte("ColumnTitleName", Title);
elem.AddAttriubte("Type", "System.String");
elem.AddAttriubte("DefaultValue", "");
elem.AddAttriubte("Field", Name);
root.AppendChild(elem);
}
}
}
xml.Save(Way);