首页 > 解决方案 > 将字头表导出到excel powershell

问题描述

请你能帮我从一个单词中获取标题表并导出到excel文档吗?

目标点是让每个字段都有他的值来知道所有的字段和他的值。

这是标题的示例:

在此处输入图像描述

这是目标:

在此处输入图像描述

这是使用的代码,但返回给我一个文本,而不是一个表格:

$docPath = "C:\Users\Dummy\Desktop\"
$outputfile = "C:\Users\Dummy\Desktop\header_out.csv"
if(Test-Path -path $outputfile) { Remove-Item -path $outputfile }

$all_docs = Get-ChildItem $docPath -filter "n.docx"
$word = New-Object -comobject "Word.Application"
$word.Visible = $False
$all_items = @()

foreach ( $document in $all_docs)
{

 $item = New-Object System.Object
 $doc = $word.Documents.Open($document.FullName);
 
 $header = $doc.Sections.Item(1).Headers.Item(1).Range.Text

 
 $item | Add-Member -type NoteProperty -name Name -value $document.FullName
 $item | Add-Member -type NoteProperty -name Header -value $doc.Sections.Item(1).Headers.Item(1).Range.Text
 $all_items += $item
 $doc.Close()
 
}
$word.Quit()
Remove-Variable doc
Remove-Variable word
$all_items | Export-CSV  $outputfile

这是结果:

感谢您的帮助和BR!

标签: powershell

解决方案


推荐阅读