<properties>
<entry key="type">exSafe:Nocontract</entry>
<entry key="aspects">cm:version,cm:title,cm:own,cm:author</entry>
<entry key="cm:creator">1122334455</entry>
<entry key="cm:autoVersion">true</entry>
<entry key="cm:title">XXXXXXXXXXXXXXX</entry>
<entry key="cm:modifier">1122334455</entry>
<entry key="cm:created">2013-07-28T08:30:51.000+01:00</entry>
<entry key="gwSafe:contractCustVend">XXXXXXXXXXXXXXXXXXX999</entry>
</properties>
我有以下XML文件,我想通过PowerShell将它们转换为Excel文件,有人可以帮助我吗?
这是我到目前为止所做的:
[xml]$inputFile = Get-Content "k1.xml"
#export xml as csv
$inputFile.Sites.ChildNodes | Export-Csv "k1.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8
答案 0 :(得分:2)
我已经写了一个小样本来帮助你入门。这包含基本逻辑。 我没有包含添加标题的代码(因为这很难完成,这也留下了一些事情要做)
[xml]$XML = @'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="type">exSafe:Nocontract</entry>
<entry key="aspects">cm:version,cm:title,cm:own,cm:author</entry>
<entry key="cm:creator">1122334455</entry>
<entry key="cm:autoVersion">true</entry>
<entry key="cm:title">XXXXXXXXXXXXXXX</entry>
<entry key="cm:modifier">1122334455</entry>
<entry key="cm:created">2013-07-28T08:30:51.000+01:00</entry>
<entry key="gwSafe:contractCustVend">XXXXXXXXXXXXXXXXXXX999</entry>
</properties>
'@
## Create Excel COM Object
$excel = New-Object -ComObject Excel.Application
# Set the sheet to visible so you can actually see it (for testing purposes)
$excel.Visible = $true
# Create a workbook
$workbook = $excel.Workbooks.Add()
# Get sheet 1
$sheet = $workbook.Worksheets.Item(1)
# Set a counter
$x = 1;
# here we loop through the XML data and add this to the excel sheet
foreach ($entry in $XML.properties.entry)
{
$sheet.Cells.Item($x,1) = $entry.Key
$sheet.Cells.Item($x,2) = $entry.'#text'
$x++
}