提取XML文件节点并制成表格展示
问题思路
问题:需要提取xml文件(流程图)中的某个节点的属性值并制成表格汇报。
思路:xml是一种树状结构的文件,提取子节点需要用到微软准备好的sdk(System.Xml)。除此之外,xml文件有很多个,但是报表只能有单个。考虑使用System.IO进行文件操作,把每个xml文件的输出做成一个分表。最后操作表格需要用到Nuget包OpenXML。
代码
string path= @"C:\Users\liuzh\Documents\WeChat Files\wxid_rfdiwplc9no22\FileStorage\File\2021-12\101";
SpreadsheetDocument myDocument = SpreadsheetDocument.Create($@"F:\my c sharp data\ExcelHandleWithNet\一期流程图点位.xlsx", SpreadsheetDocumentType.Workbook);
WorkbookPart workbookPart = myDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
Sheets sheets = myDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
DirectoryInfo folder = new DirectoryInfo(path);
foreach (FileInfo file in folder.GetFiles("*.htm"))
{
var filename = file.Name;
filename = filename.Substring(0, filename.Length - 4);
UInt32Value i = 2;
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
Sheet sheet = new Sheet()
{
Id = myDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = i-1,
Name = $"{filename}"
};
sheets.Append(sheet);
worksheetPart.Worksheet = new Worksheet(new SheetData());
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load($@"C:\Users\liuzh\Documents\WeChat Files\wxid_rfdiwplc9no22\FileStorage\File\2021-12\101\{filename}_files\DS_datasource1.dsd");
var nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("UWDataSource", "x-schema:DataSourceSchema.xdr");
XmlNodeList nodes = xmlDocument.SelectNodes(@"//UWDataSource:DataObjects/UWDataSource:TimeSeriesDataObject/UWDataSource:TimeSeriesValuesParameters", nsmgr);
Row headrow = new Row() { RowIndex = 1 };
Cell head1 = new Cell()
{
CellValue = new CellValue("图中点位"),
DataType = new EnumValue<CellValues>(CellValues.String),
CellReference = "A1"
};
Cell head2 = new Cell()
{
CellValue = new CellValue("实际DCS点位"),
DataType = new EnumValue<CellValues>(CellValues.String),
CellReference = "B1"
};
headrow.Append(head1);
headrow.Append(head2);
sheetData.Append(headrow);
foreach (XmlNode node in nodes)
{
string value = ((XmlElement)node).GetAttribute("Item");
int real = 0; int count_first = 2; int end = value.Length - 1;
for (int j = 0; j < value.Length && count_first > 0 ; j++)
{
if (value[j] == '_')
{
count_first--;
real = j + 1;
}
}
for (int x = real; x < value.Length; x++)
{
if ((value[x] > 47 && value[x] < 58) || (value[x] > 64 && value[x] < 91)||value[x]=='_')
continue;
end = x;
break;
}
Row tempRow = new Row() { RowIndex = i };
Cell tempCel = new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(CellValues.String),
CellReference = "B" + i.ToString()
};
Cell tempcellreal = new Cell()
{
DataType = new EnumValue<CellValues>(CellValues.String),
CellReference = "A" + i.ToString()
};
if (end > 0)
tempcellreal.CellValue = new CellValue(value.Substring(real, end - real));
tempRow.Append(tempcellreal);
tempRow.Append(tempCel);
sheetData.Append(tempRow);
i++;
}
worksheetPart.Worksheet.Save();
workbookPart.Workbook.Save();
}
myDocument.Close();

浙公网安备 33010602011771号