本帖最后由 newswan 于 2021-6-4 20:21 编辑
- $fileTxt = "1.txt"
- $fileExcel = "1.xlsx"
-
- $excel = New-Object -ComObject Excel.Application
- $excel.Visible = $true
- $excel.displayAlerts = $true
- $wb = $excel.Workbooks.add()
-
- $filec = Get-Content -Path $fileTxt
- $wb.sheets(1).columns(1).numberformatlocal= "general"
-
- $c = 0,1,4,6,8
-
- for ( $i = 1 ; $i -lt $filec.Count ; $i++ )
- {
- $s = $fileC[$i] -replace "\s+",","
- $sa = ("," + $s).Split(",")
- for ( $j = 1 ; $j -lt $c.count ; $j++ )
- {
- $wb.sheets(1).cells($i,$j)=$sa[$c[$j]]
- }
- }
-
- $wb.Saveas($fileExcel)
- $excel.Quit()
- $excel = $null
- [GC]::Collect()
复制代码 powershell 还是比较方便,vba 做这个也方便
excel 行列从 1 开始 ,数组是从 0 开始,麻烦 |