joinColumns.bat- <#*,:&cls
- @echo off
- pushd "%~dp0"
- Powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- $outfile = Join-Path -Path ($pwd.Path) -ChildPath "$($pwd.Path|Split-Path -Leaf).xlsx"
- try {
- $oExcelApp = New-Object -ComObject Excel.Application
- }
- catch {
- try {
- $oExcelApp = New-Object -ComObject ET.Application
- }
- catch {
- throw
- }
- }
- function Open-Text {
- param (
- [string]$Path
- )
- try {
- $oExcelApp.Workbooks.OpenText($Path, 936, 1, 1, -4142, $false, $false, $false, $true, $false, $false, $null, @(@(1, 2), @(2, 2)))
- return $true
- }
- catch {
- return $false
- }
- }
- try {
- $oExcelApp.AutomationSecurity = 3
- $oExcelApp.DisplayAlerts = $false
- $oExcelApp.Interactive = $false
- $oExcelApp.Visible = $false
- # open data.0 as the base workbook
-
- if (Open-Text -Path (Join-Path -Path $pwd.Path -ChildPath "data.0")) {
- $oWorkbook = $oExcelApp.Workbooks.Item("data.0")
- $oWorksheet = $oWorkbook.Worksheets.Item(1)
- }
- else {
- Write-Error -Message "Can't open data.0"
- throw
- }
-
- Convert-Path "data?*.0" | Sort-Object -Property { (Split-Path $_ -Leaf).Remove(0, 4).Split(".")[0] -as [int] } | ForEach-Object -Begin {
- $column = 3
- } -Process {
- if ( Open-Text -Path $_) {
- $oWorkbook1 = $oExcelApp.Workbooks.Item((Split-Path $_ -Leaf))
- $oWorkbook1.Worksheets.Item(1).UsedRange.Columns.Item(2).Copy($oWorksheet.Cells.Item(1, $column++))
- $oWorkbook1.Close()
- Write-Host "Add $_ success" -ForegroundColor Green
- }
- else {
- Write-Error -Message "Can't open $_"
- }
- } -End {
- $oWorkbook.SaveAs($outfile, 51, $null, $null, $false, $false, 3, 2, $false, $null, $null, $true)
- }
- }
- catch {
- $_ | Out-String | Write-Host -ForegroundColor Red
- }
- finally {
- $oExcelApp.Workbooks.Close()
- $oExcelApp.Quit()
- [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcelApp)
- Remove-Variable oExcelApp
- }
复制代码
|