首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell数据处理

Powershell数据处理
EN

Stack Overflow用户
提问于 2021-02-15 17:43:39
回答 1查看 50关注 0票数 0

亲爱的Stack Overflow社区:

我想你可以猜到,休斯顿有个问题。背景:我们的公司受到黑客的攻击,我们丢失了大量的数据。现在,一切都恢复正常了(或多或少),我们的服务器上有成千上万的旧.xls和.doc文档,在重新进入我们全新的服务器结构之前,这些文档必须转换为新的格式。

目前,我正在尝试编写一个将XLS转换为XLSX的脚本。问题:我擅长批处理编程,但在使用Powershell时却完全迷失了方向。这是我从头开始写它后的第三次尝试。它做了它应该做的事情,但我不知道如何在PS中做适当的数据处理。

它应该做的是:询问用户在哪里可以找到XLS文件。询问用户将新的XLSX文件存储在何处。从目录(和子文件夹)中取出每个.xls文件,打开它并将其作为.xlsx保存到给定的路径。

到目前为止它所做的事情:询问用户在哪里查找XLS文件。询问用户将新的XLSX文件存储在何处。完全打乱路径+无法打开文件。

当我修改脚本以向它提供完整的路径时,它能够将.xls转换为.xlsx。因此,Excel部分似乎是正确的。我知道我的数据处理部分是非常错误的。现在我试着重写了很多次。我把它留在里面了,这样你就可以看看我想做什么了。

代码语言:javascript
复制
$LookXLS = Read-Host -Prompt 'Where do I look for XLS?' 
$TargetpathXLSX = Read-Host -Prompt 'Where to save the XLSX?'

Write-Host $LookXLS

cd $LookXLS # Change Directory to given path

Get-ChildItem -Path .\ -Filter *.xls -Recurse -File -Name| ForEach-Object {
    [System.IO.Path]::GetFileName($_)


$excel=New-Object -comobject Excel.Application
$excel.Visible =$TRUE
$excel.DisplayAlerts = $FALSE
$wb = $excel.Workbooks.Open($LookXLS)
$LookXLS=$TargetpathXLSX
$typ=".xlsx"
$TargetPathXLSX="$TargetpathXLSX\$_$typ"
echo $TargetPathXLSX
$excel.ActiveWorkbook.SaveAs("$TargetPathXLSX",51)

$excel.ActiveWorkbook.Close()

$excel.Quit()

}


echo Process finished.

pause

输出看起来像这样:(我的评论是因为它有一部分是德语)

代码语言:javascript
复制
PS C:\Users\USER\Desktop\Testordner\Userverzeichnis> C:\Users\USER\Desktop\new 1.ps1

Where do I look for XLS?: C:\Users\USER\Desktop\Testordner\Userverzeichnis

Where to save the XLSX?: C:\Users\USER\Desktop\Testordner\Ziel

C:\Users\USER\Desktop\Testordner\Userverzeichnis
Microsoft Excel-Arbeitsblatt (neu) (2).xls
Wir konnten 'Arbeit\Microsoft Excel-Arbeitsblatt (neu) (2).xls' nicht finden. Wurde das Objekt vielleicht verschoben, umbenannt oder gelöscht? **#Can´t find it**
In C:\Users\USER\Desktop\new 1.ps1:17 Zeichen:1
+ $wb = $excel.Workbooks.Open($_)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
 
C:\Users\USER\Desktop\Testordner\Ziel\Arbeit\Microsoft Excel-Arbeitsblatt (neu) (2).xls.xlsx
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat. 

**#NULL (obviously, the path is screwed up)**
In C:\Users\USER\Desktop\new 1.ps1:22 Zeichen:1
+ $excel.ActiveWorkbook.SaveAs("$TargetPathXLSX",51)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\Users\USER\Desktop\new 1.ps1:24 Zeichen:1
+ $excel.ActiveWorkbook.Close()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Microsoft Excel-Arbeitsblatt (neu).xlsx
Wir konnten 'Arbeit\Microsoft Excel-Arbeitsblatt (neu).xlsx' nicht finden. Wurde das Objekt vielleicht verschoben, umbenannt oder gelöscht?
In C:\Users\USER\Desktop\new 1.ps1:17 Zeichen:1
+ $wb = $excel.Workbooks.Open($_)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
 
C:\Users\USER\Desktop\Testordner\Ziel\Arbeit\Microsoft Excel-Arbeitsblatt (neu) (2).xls.xlsx\Arbeit\Microsoft Excel-Arbeitsblatt (neu).xlsx.xlsx 

**#It gets worse every time it loops.**
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\Users\USER\Desktop\new 1.ps1:22 Zeichen:1
+ $excel.ActiveWorkbook.SaveAs("$TargetPathXLSX",51)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\Users\USER\Desktop\new 1.ps1:24 Zeichen:1
+ $excel.ActiveWorkbook.Close()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我希望你能以某种方式帮助我。我不是PS的人,但遗憾的是Batch不能与Excel一起工作。我不认为我离解决方案太远了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-15 18:38:59

试试这个-

代码语言:javascript
复制
$LookXLS = Read-Host -Prompt 'Where do I look for XLS?' 
$TargetpathXLSX = Read-Host -Prompt 'Where to save the XLSX?'

Write-Host "$($LookXLS) is where we look" -ForegroundColor cyan

cd $LookXLS # Change Directory to given path

Get-ChildItem -Path .\ -Filter *.xls -Recurse | ForEach-Object {
    
    $path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
    $excel = New-Object -comobject Excel.Application
    $excel.Visible = $TRUE
    $excel.DisplayAlerts = $FALSE
    $wb = $excel.Workbooks.Open($_.FullName)
    
    $path += ".xlsx"
    $wb.saveas($path, 51)
    $wb.close()
    $excel.Quit()

}

上面的代码将xls文件更改为xlsx。您需要添加用于将xlsx文件复制到目标文件夹的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66205730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档