简体   繁体   English

PowerShell - 导入 Excel 然后导出 CSV 而不使用 Excel 或 COM

[英]PowerShell - Import Excel then Export CSV without using Excel or COM

I am developing a PowerShell script to import an Excel file and output the data to a flat file.我正在开发一个 PowerShell 脚本来导入 Excel 文件并将数据输出到平面文件。 The code that I have below works fine except that it fails to preserve leading zeros;我下面的代码工作正常,只是它无法保留前导零; when the CSV file is opened in a text editor, the leading zeros are not present.在文本编辑器中打开 CSV 文件时,前导零不存在。 (Leading zeros are necessary for certain ID numbers, and the ID numbers are stored in Excel using a custom format.) Does anyone have any thoughts on how to get the ImportExcel module to preserve the leading zeros, or, perhaps another way of getting to the same goal? (某些 ID 号需要前导零,并且 ID 号使用自定义格式存储在 Excel 中。)有没有人对如何让 ImportExcel 模块保留前导零有任何想法,或者,也许是另一种获取方式相同的目标? I would like to do this without using the COM object and without having to install Excel on the server;我想在不使用 COM 对象且无需在服务器上安装 Excel 的情况下执行此操作; that's why I've been trying to make the ImportExcel module work.这就是为什么我一直试图使 ImportExcel 模块工作的原因。

$dataIn = filename.xlsx ; $dataOut = filename.csv
Import-Excel -Path $dataIn | Export-Csv -Path $dataOut

I presume you're using the ImportExcel module?我想您正在使用 ImportExcel 模块?

I just did this and it worked.我只是这样做了,它奏效了。 I created a spreadsheet like:我创建了一个电子表格,如:

Name         ID1          ID2
Steven  00012345     00012346

I gave them a custom number format of 00000000 then ran:我给了他们一个自定义数字格式 00000000 然后运行:

Import-Excel .\Book1.xlsx | Export-Csv .\book1.csv

When looking at the csv file I have both ID numbers as quoted strings:在查看 csv 文件时,我将两个 ID 号都作为带引号的字符串:

"Name","ID1","ID2"
"Steven","00012345","00012346"

Is there anything else I need to do to reproduce this?我还需要做些什么来重现这个吗? Can you give the specifics of the custom number format?你能给出自定义数字格式的细节吗?

Also withstanding your answer to above.也经得起你对上面的回答。 You can modify the properties of each incoming object by converting them to strings.您可以通过将每个传入对象转换为字符串来修改它们的属性。 Assuming there's a fixed number of digits you can use the string format with the .ToString() method like:假设有固定数量的数字,您可以将字符串格式与 .ToString() 方法一起使用,例如:

(12345).ToString( "00000000" ) 

This will return "00012345"...这将返回“00012345”...

So redoing my test with regular numbers (no custom format):所以用常规数字(无自定义格式)重做我的测试:

$Input = @(Import-Excel \\nynastech1\adm_only\ExAdm\Temp\Book1.xlsx)
$Input | 
ForEach{
    $_.ID1 = $_.ID1.ToString( "00000000" )
    $_.ID2 = $_.ID2.ToString( "00000000" )
}

This will convert ID1 & ID2 into "00012345" & "00012345" respectively.这会将 ID1 和 ID2 分别转换为“00012345”和“00012345”。

You can also use Select-Object, but you might need to rename the properties.您也可以使用 Select-Object,但您可能需要重命名属性。 If you are interested I can demo that approach.如果您有兴趣,我可以演示该方法。

Note: the @() wrapping in my example is because I only have the 1 object, and is partly force of habit.注意:我示例中的 @() 包装是因为我只有 1 个对象,并且部分是习惯的力量。

Let me know how it goes.让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM