【发布时间】:2015-02-13 23:49:14
【问题描述】:
我想自动化数据→导入文本文件→文本导入向导的Excel流程
如何找到这些菜单的 PowerShell 名称?
这是我的代码。我希望它在文件夹中找到 .txt 文件(有分隔符)并将它们导入到单独的 Excel 文件中。
#需要 2 个或更多文件才能工作。 rubbish.txt 必须与其他文件在文件夹中。
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$path = "S:\DATA EXCHANGE\Testing" #where the original files are
$path2="S:\DATA EXCHANGE\Resurgent\fromSFG\test" #where you want the new files to go
Set-Location $path
$a= Get-ChildItem $path -recurse -include *.txt #only finds the text files
$numOfFiles= $a.count
For ($n=$numOfFiles-1; $n -gt -1; $n--)
{$b=$a[$n].name ;
$z=$path+"/"+$b #finds original file for import
$wb = $excel.Workbooks.OpenText( #the import
$z, # file to open
[Microsoft.Office.Interop.Excel.XlPlatform]::xlWindows,
1, # start from row 1
[Microsoft.Office.Interop.Excel.XlTextParsingType]::xlDelimited,
[Microsoft.Office.Interop.Excel.XlTextQualifier]::xlTextQualifierDoubleQuote,
$false, # Consecutive Delimiter
$true, # tab
$false, # semicolon
$true, # comma
$false, # space
$true, # use other
'|')
$y= $b -replace ".txt.*" #gets filename without extension
$y= $path2+"/"+$y #adds path to filename
#$y= $y+".xlsx" #adds xlsx to filename
if ($b -eq "rubbish.txt") {$Excel.ActiveWorkbook.Close()}
$Excel.ActiveWorkbook.SaveAs("$y")
#$Excel.ActiveWorkbook.Close()
}
#$Excel.Workbooks.Close()
#$Excel.Quit()
【问题讨论】:
标签: excel powershell methods