PowerShell与Excel NumberFormat

问题描述

我想问问你

我有匈牙利语Excel(64位,版本:14.0,Microsoft Office Professional 2010)

我想格式化PowerShell中的日期单元格。我认为这很容易。我用VisualBasic录制,并检查它是否正常工作:

Cells.Item(1,1).Numberformat = 'yyyy-mm-dd' 要么 Cells.Item(1,1).NumberformatLocal = 'éééé-hh-nn'

两者都能在VB中正常工作。

然后,基于此我创建了一个PowerShell脚本。但是在两个版本中,格式字符串都必须使用 local 语言。我不明白,为什么!

PowerShell版本为5.1.18362.145。以BOM表格式以UTF-8保存的文件

这是示例脚本:

** numberformat_issue.ps1 **

Set-StrictMode -Version 2.0

[System.Threading.Thread]::CurrentThread.CurrentCulture
[System.Threading.Thread]::CurrentThread.CurrentUICulture

$excel = New-Object -ComObject Excel.Application
if (!$excel) { "Not created"; exit } else { "Created" }

$excel.Visible = $True
$excel.displayAlerts = $True

$app = $excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
$cells = $worksheet.Cells

(1..5) | % { $cells.Item($_,1) = Get-Date }
$cells.Columns(1).EntireColumn.AutoFit()

Write-Host '1,1:' $cells.Item(1,1).NumberFormat $cells.Item(1,1).NumberFormatLocal

$cells.Item(2,1).NumberFormat = 'yyyy-mm-dd'
Write-Host '2,1:' $cells.Item(2,1).NumberFormatLocal

$cells.Item(3,1).NumberFormat = 'éééé-hh-nn'
Write-Host '3,1:' $cells.Item(3,1).NumberFormatLocal

$cells.Item(4,1).NumberFormatLocal = 'yyyy-mm-dd'
Write-Host '4,1:' $cells.Item(4,1).NumberFormat

$cells.Item(5,1).NumberFormatLocal = 'éééé-hh-nn'
Write-Host '5,1:' $cells.Item(5,1).NumberFormat

**输出**

LCID             Name             displayName
----             ----             -----------
1038             hu-HU            Hungarian (Hungary)
1033             en-US            English (United States)
Created
True
1,1: éééé.hh.nn ó:pp éééé.hh.nn ó:pp --> First should be "yyyy.mm.dd h:mm"
2,1: yyyy-mm-dd                      --> Should be "éééé.hh.nn ó:pp"
3,1: éééé-hh-nn                      --> Should be wrong! Local format used for non-local
4,1: yyyy-mm-dd                      --> Should be wrong! Non-local format used for local
5,1: éééé-hh-nn                      --> Should be "yyyy.mm.dd"

enter image description here

在所有情况下,只要使用 local 语言格式并且输入错误,然后使用“ normal”格式,就可以使用该格式!

我应该应用一些语言设置吗?我怎么了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)