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

痞子三分冷 提交于 2020-08-09 10:50:09

问题


Background

I am trying to design a script that I can run on a server without an Excel installation or importing modules/libraries. This rules out COM Excel.Application, the ImportExcel module and other 3rd party libraries. Instead, I unzip the excel file into a collection of xml files. I need to parse these xml files in powershell for a given range of cell values spanning multiple Excel sheets.

So far, I have written a script to retrieve the sheetIDs:

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

I can use the sheetIDs to find the individual sheet files under xl\worksheets\sheet<ID>.xml. My problem is parsing and retrieving values from these individual sheet files.

Sample Input

Here is a sample of 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>

I would like to extract K7,L7 (db.url and urlValue) and K8,L8 (db.User and UserName) from this xml file. The location is given in the r node and the value in the v node.

Attempt

Unfortunately, I am unable to retrieve any values from the sheet xml files. Using this site, I tried

[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

which uses a RegEx to cover K7:L100, but there's no output. I have tried various other methods as well, such as dotting through the xml file, but I could not get them to work. I am open to any pre-installed PowerShell coding approach to retrieve these values.

Thanks very much.


回答1:


Two things - your xpath expression has to take into account the existence of namespaces in this xml. Also - it's never a good idea to use regex with xml.

So try something along these lines:

$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

Edit: To get the attribute values of the c attributes, use:

$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   


来源:https://stackoverflow.com/questions/62498682/powershell-script-to-unzip-xlsx-and-read-contents-from-a-sheet-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!