简体   繁体   English

Powershell 导出 csv 结果到 excel 工作簿中的多个工作表

[英]Powershell export csv results to multiple sheets in an excel workbook

I run a lot of scans against our AD looking for deficiencies (ie users without an email, user ID blank, on-leave but AD account not disabled, etc) and all my results are exported to csv files.我对我们的 AD 进行了大量扫描以寻找缺陷(即没有 email 的用户、用户 ID 为空白、休假但 AD 帐户未禁用等),我的所有结果都导出到 csv 文件。 I'd like each csv result stored on a sheet in a single excel workbook.我希望将每个 csv 结果存储在单个 excel 工作簿中的工作表上。 I've found a lot of sites showing how to convert a csv to an xls or how to export to a single xls sheet but I can't find anything else meeting my needs.我发现很多网站展示了如何将 csv 转换为 xls 或如何导出到单个 xls 表,但我找不到任何其他满足我需求的东西。 I don't have the ability to import the Import-Excel / Export-Excel modules from the PS gallery.我无法从 PS 库中导入 Import-Excel / Export-Excel 模块。 Any suggestions on how I can export 3 CSV files (actually array objects - not sure they need to be exported to CSV first) to three sheets in a workbook?关于如何将 3 个 CSV 文件(实际上是数组对象 - 不确定它们是否需要先导出到 CSV)导出到工作簿中的三个工作表的任何建议?

What I've found so far:到目前为止我发现了什么:

# Create Excel object
$excel = new-object -comobject Excel.Application
# Create a new workbook
$workbook = $excel.workbooks.add()
# Name a worksheet
$workbook.WorkSheets.Item(1).Name = "Users"
# Add data from a csv to the current sheet
$workbook = $excel.workbooks.add(“C:\export_users.csv”)

What I can't figure out is how to add data from additional csv files onto additional sheets in the workbook.我不知道如何将其他 csv 文件中的数据添加到工作簿的其他工作表中。

Add another worksheet with another Add().使用另一个 Add() 添加另一个工作表。

$excel = New-Object -ComObject Excel.Application
$wb1 = $excel.workbooks.add()
$wb1.WorkSheets.Item(1).Name = 'Users 1'
$wb1.Worksheets.Item(1).Name
$wb1.Worksheets.Count
$wb1.Worksheets.Add() | Out-Null
$wb1.WorkSheets.Item(2).Name = 'Users 2'
$wb1.Worksheets.Item(2).Name
$wb1.Worksheets.Count

I have had some success using the ImportExcel module for PowerShell.我已经使用 PowerShell 的ImportExcel模块取得了一些成功。

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

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