【问题标题】:encoding while importing csv to xls via powershell通过powershell将csv导入xls时进行编码
【发布时间】:2015-08-21 14:02:05
【问题描述】:

我使用此代码(在某处找到)将 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()

【问题讨论】:

  • 能否提供错误信息。
  • 我建议converting the CSV file to UTF-8。如果生成的 Unicode 也会被破坏,则问题与 Excel 无关(可能是输入文件编码被破坏)。
  • 这不是错误,这些只是在没有正确登录字符集时使用的随机字符,默认情况下在 excel 中使用。我会先尝试将此 csv 转换为 UTF8。
  • 转换为 UTF8 没有帮助。当我在 excel 中打开此 CSV 文件时,我看到如下文本中的错误:“Wła¶ciciel”“S±d”。要在 excel 中正确查看文本,我必须将编码设置为 ISO 8859-2 的文本导入。如何在这个 powershell 脚本中做到这一点?

标签: excel powershell csv encoding


【解决方案1】:

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

导出-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 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2016-12-08
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多