微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerShell与Excel NumberFormat

如何解决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 举报,一经查实,本站将立刻删除。