簡體   English   中英

Powershell 腳本解壓縮 xlsx 並從工作表 xml 文件中讀取內容

[英]Powershell script to unzip xlsx and read contents from a sheet xml file

背景

我正在嘗試設計一個可以在沒有 Excel 安裝或導入模塊/庫的服務器上運行的腳本。 這排除了 COM Excel.Application、ImportExcel 模塊和其他第三方庫。 相反,我將 excel 文件解壓縮到 xml 文件的集合中。 對於跨越多個 Excel 表的給定單元格值范圍,我需要解析 powershell 中的這些 xml 文件。

到目前為止,我已經編寫了一個腳本來檢索 sheetID:

unzip myExcel.xlsx
[xml]$workbookXML = Get-Content xl\workbook.xml
[xml]$sheet = Get-Content xl\worksheets\sheet10.xml

$sheetDictionary = @{}
foreach($sheetChildNode in $workbookXML.workbook.sheets.sheet) {
    $sheetDictionary.add($sheetChildNode.name, $sheetChildNode.sheetId)
}

$sheetDictionary

我可以使用 sheetIDs 在 xl\worksheets\sheet<ID>.xml 下查找各個工作表文件。 我的問題是從這些單獨的工作表文件中解析和檢索值。

樣本輸入

這是 xl\worksheets\sheet10.xml 的示例:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

<worksheet xr:uid="{00000000-0001-0000-0800-000000000000}"
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" 
xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" 
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" 
xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac xr xr2 
xr3" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <dimension ref="A1:L100"/>
     <sheetViews>
          <sheetView workbookViewId="0">
               <selection sqref="A11:B11" activeCell="A11"/>
          </sheetView>
     </sheetViews>
     <sheetFormatPr x14ac:dyDescent="0.35" defaultRowHeight="14.5"/>
     <cols>
          <col customWidth="1" style="32" width="18.81640625" max="1" min="1"/>
          <col style="32" width="8.7265625" max="2" min="2"/>
          <col customWidth="1" style="5" width="14.81640625" max="11" min="11"/>
          <col customWidth="1" style="5" width="12" max="12" min="12"/>
          </cols>
     <sheetData>
          <row r="6" x14ac:dyDescent="0.35" spans="1:12">
               <c r="A6" t="s" s="33">
                    <v>270</v>
               </c>
               <c r="B6" t="s" s="33">
                    <v>271</v>
               </c>
               <c r="K6" t="s" s="5">
                    <v>272</v>
               </c>
               <c r="L6" t="s" s="5">
                    <v>273</v>
               </c>
          </row>
          <row r="7" x14ac:dyDescent="0.35" spans="1:12">
               <c r="A7" t="str" s="32">
                    <f>'All Parameters'!K13</f>
                    <v>UnwantedValue1</v>
               </c>
               <c r="B7" t="str" s="32">
                    <f>'All Parameters'!L13</f>
                    <v>UnwantedValue2</v>
               </c>
               <c r="K7" t="str" s="5">
                    <f ref="K7:K38" t="shared" si="0">IF(AND(NOT($A7=""),NOT($B7="")),A7,CONCATENATE("ParameterNotUsed",ROW()))</f>
                    <v>db.url</v>
               </c>
               <c r="L7" t="str" s="5">
                    <f ref="L7:L38" t="shared" si="1">IF(AND(NOT($A7=""),NOT($B7="")),B7,CONCATENATE("ParameterNotUsed",ROW()))</f>
                    <v>URLValue</v>
               </c>
          </row>
          <row r="8" x14ac:dyDescent="0.35" spans="1:12">
               <c r="A8" t="str" s="32">
                    <f>'All Parameters'!O14</f>
                    <v>UnwantedValue3</v>
               </c>
               <c r="B8" t="str" s="32">
                    <f>'All Parameters'!P14</f>
                    <v>UnwantedValue4</v>
               </c>
               <c r="K8" t="str" s="5">
                    <f t="shared" si="0"/>
                    <v>db.User</v>
               </c>
               <c r="L8" t="str" s="5">
                    <f t="shared" si="1"/>
                    <v>UserName</v>
               </c>
          </row>
     </sheetData>
<pageMargins footer="0.3" header="0.3" bottom="0.75" top="0.75" right="0.7" left="0.7"/>
</worksheet>

我想從這個 xml 文件中提取 K7,L7(db.url 和 urlValue)和 K8,L8(db.User 和 UserName)。 位置在 r 節點和 v 節點中給出。

試圖

不幸的是,我無法從工作表 xml 文件中檢索任何值。 使用這個網站,我試過了

[xml]$sheet = Get-Content xl\worksheets\sheet10.xml

$data = (Select-Xml -xpath "/worksheet/sheetData/row/c[r = '[K-L][7-9]$|[K-L][1-9][0-9]$|[K-L]100']/v" $sheet |
  % {$_.Node.'#text'})
$data

它使用正則表達式來覆蓋 K7:L100,但沒有 output。 我也嘗試了各種其他方法,例如通過 xml 文件進行點綴,但我無法讓它們工作。 我願意接受任何預安裝的 PowerShell 編碼方法來檢索這些值。

非常感謝。

兩件事 - 您的 xpath 表達式必須考慮到此 xml 中命名空間的存在。 此外 - 將正則表達式與 xml 一起使用絕不是一個好主意。

因此,請嘗試以下方法:

$ns = @{ns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"}

$items = Select-Xml -Xml $sheet -XPath '//ns:c[(@r="K7" or @r="L7" or @r="K8" or @r="L8")]//ns:v' -Namespace $ns
$items | Foreach {$_.Node.InnerXml}

Output:

db.url
URLValue
db.User
UserName

編輯:要獲取c屬性的屬性值,請使用:

$items = Select-Xml -Xml $sheet -XPath '//ns:c[@r]/@r' -Namespace $ns
$items | Foreach {$_.Node}

Output:

A6   
B6   
K6   
L6   
A7   
B7   
K7   
L7   
A8   
B8   
K8   
L8   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM