使用 PowerShell 脚本导出 Excel 文件为文本文件 Txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# export excel file to txt file

$files =
"aura",
"spell",
"missile"

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $False
$excel.DisplayAlerts = $False
$xlFormat = "Microsoft.Office.Interop.Excel.XlFileFormat" -as [type]

Set-Location -Path .

for ($i = 0; $i -lt $files.Length; ++$i)
{
    $fileName = "./" + $files[$i] + ".xlsx"
    Write-Output "Export File "$fileName

    $filePath = Resolve-Path $fileName
    $newFilePath = [System.IO.Path]::ChangeExtension($filePath, ".txt")

    if (Test-Path -Path $newFilePath)
    {
        Set-ItemProperty $newFilePath -Name IsReadOnly -Value $False
    }

    $workbook = $excel.workbooks.Open($filePath)
    $sheet1 = $workbook.worksheets.Item(1)
    $sheet1.SaveAs($newFilePath, $xlFormat::xlUnicodeText)
    $workbook.Close()
}

$excel.Quit()

Read-Host -Prompt "Press Enter to Exit"