通过powershell将csv导入xls时进行编码

时间:2015-08-21 14:02:05

标签: excel powershell csv encoding

我使用此代码(在某处找到)将csv转换为xls。没有国家字符时,它可以很好地工作。 当我尝试使用ISO 2859-2编码转换csv文件时,我有一些文本错误。知道如何在这个脚本中处理这个字符集吗?

### Set input and output path
$inputCSV = "C:\tmp\test.CSV"
$outputXLSX = "C:\tmp\test.xls"

### Create a new Excel Workbook with one empty sheet which name is the file
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$worksheet.name = "$((GCI $inputCSV).basename)"

### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)

### Set the delimiter ( , or ; ) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)

### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
### this options don't seems necessary
$query.TextFileParseType  = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
### change decimal separator as "." (can be ",")
$query.TextFileDecimalSeparator = "."
$query.AdjustColumnWidth = 1

### Execute & delete the import query
# using my_output avoid having an outuput that display true
$my_output = $query.Refresh()
$query.Delete()

### Save & close the Workbook as XLS.
$Workbook.SaveAs($outputXLSX,56)
$excel.Quit()

1 个答案:

答案 0 :(得分:0)

据我所知,在Excel中无法做到这一点。这是解决方法,导出到UnicodeText,它基本上是TAB分隔的csv。

导出-ExcelCSV.ps1

param(
    [string] $Path
)

if (!(Test-Path $Path)) { throw Path not found: $Path }

$Path = Resolve-Path $Path
$excel = New-Object -COM "Excel.Application"
if (!($excel)) {throw "Can not create Excel COM object" }

$workbook = $excel.Workbooks.Open($Path)
$worksheets = $workbook.Worksheets


$base_name = $path -replace '.xlsx$'
$worksheets | % {
    $sheet = $_
    $csv_name = $base_name + '_' + $sheet.name + '.csv'
    if (Test-Path $csv_name) {  rm $csv_name }
    $sheet.SaveAs($csv_name, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlUnicodeText) ; # xlCSVWindows, xlCSV, xlUnicodeText
}

$workbook.Saved = $true
$workbook.close()
$excel.quit()
#[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) > $null
#[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) > $null

ps excel | kill     #for some reason Excel stays, this will also kill any other running excels not related to this script.
ls *.csv | % { (Get-Content $_) -replace '\t',',' | Set-Content $_ -Encoding utf8 }
相关问题