2

I'm reading some value from XML:

<DocumentSetNumber>20151027301060001</DocumentSetNumber>

$DSN = $xml.metaBI.DocumentSetNumber

and then writing to Excel using PowerShell:

$ExcelWorkSheet.Cells.Item($Row,1).Value2 = $DSN 

But in Excel I'm getting this number: 2.0151E+16

If I change Excel format I can get this: 20151027301060000

It's not the same number, and I need to get exactly the same.

I tried to play with Excel formats and also use Tostring() on variable, but without luck.

What also can I try?

PS. If I copy and paste this value manually it works only I use

match destination formatting

option in Excel.

How I can set this option from PowerShell?

4

1 回答 1

3

您必须将这个长 DSN 作为字符串放入单元格中。Excel 具有15 位精度,并将任何超过 15 位的数字四舍五入到最近的占位符。

鉴于您的代码,似乎$DSN以单引号(例如'ASCII 039)作为Range.PrefixCharacter 属性的前缀就足够了。或者,您可以将接收单元格的数字格式更改为文本,但我认为强制前缀字符是最好的。由于单引号(也称为tick)在 PowerShell 中用作引用运算符,因此我们需要将其作为[char]39.

$tic = [char]39
$DSN = $xml.metaBI.DocumentSetNumber
$ExcelWorkSheet.Cells.Item($Row,1).Value2 = $tic + $DSN
# write-host $tic$dsn <~~ this outputs correctly but apparently not correct for XL cell value. see comments.

这应该更改20151027301060001'20151027301060001Excel 会将其作为字符串文本接受,而不会尝试将其强制转换为数字。

于 2015-10-29T21:55:33.597 回答