Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于解压缩xlsx并从工作表xml文件读取内容的Powershell脚本_Xml_Powershell_Xml Parsing_Openxml_Xlsx - Fatal编程技术网

用于解压缩xlsx并从工作表xml文件读取内容的Powershell脚本

用于解压缩xlsx并从工作表xml文件读取内容的Powershell脚本,xml,powershell,xml-parsing,openxml,xlsx,Xml,Powershell,Xml Parsing,Openxml,Xlsx,背景 我正在尝试设计一个脚本,可以在服务器上运行,而无需安装Excel或导入模块/库。这排除了COM Excel.Application、ImportExcel模块和其他第三方库。相反,我将excel文件解压到一组xml文件中。我需要在powershell中为跨越多个Excel工作表的给定单元格值范围解析这些xml文件 到目前为止,我已经编写了一个脚本来检索SheetID: unzip myExcel.xlsx [xml]$workbookXML = Get-Content xl\workboo

背景

我正在尝试设计一个脚本,可以在服务器上运行,而无需安装Excel或导入模块/库。这排除了COM Excel.Application、ImportExcel模块和其他第三方库。相反,我将excel文件解压到一组xml文件中。我需要在powershell中为跨越多个Excel工作表的给定单元格值范围解析这些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
我可以使用SheetID在xl\worksheets\sheet.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>
它使用正则表达式覆盖K7:L100,但没有输出。我也尝试过各种其他方法,比如在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}
输出:

db.url
URLValue
db.User
UserName
A6   
B6   
K6   
L6   
A7   
B7   
K7   
L7   
A8   
B8   
K8   
L8   
编辑: 要获取
c
属性的属性值,请使用:

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

db.url
URLValue
db.User
UserName
A6   
B6   
K6   
L6   
A7   
B7   
K7   
L7   
A8   
B8   
K8   
L8   

'K7:L100'-match'[K-L][7-9]$|[K-L][1-9][0-9]$|[K-L]100'
返回匹配值
L100
--这是您想要的吗?--您可以实时测试regex,验证ethanks@ninMonkey的有用链接非常有用。我无法按预期将正则表达式用于XML命令,但后来我在脚本中使用了该站点。很高兴知道。这很好,谢谢!有没有办法提取r的值?我尝试了-XPath'//ns:c[@r]'和-XPath'//ns:c[1],但它返回了f子元素。我试图将所有的r值放入一个变量或数组中,作为不使用正则表达式的解决方法。