将 CSV 导入现有 XLSM 工作表
我正在尝试将 CSV 文件导入到现有的 Excel 工作表中。我可以让它与 .xlsx 文件一起使用,但我无法使用启用宏的工作表来弄清楚它。启用宏的工作表的原因是我希望能够在导入数据后使用宏,这样我就可以创建一个数据透视表。
我正在运行另一个 powershell 脚本,收集用户和组信息,并且我想创建一个易于阅读的 Excel 工作表。
$csv = 'C:\\test\CSV\ADusers.csv'
$extemp = 'C:\test\Temp\WithVBA.xlsm'
$ex = 'C:\test\Pivot.xlsm'
$macro = 'add'
# Adding header to CSV
$filedata = import-csv $csv -Header "UserID,GroupID"
$filedata | Export-csv -Path $csv
# copy temp with VBA to new sheet
Copy-Item -Path $extemp -Destination $ex
# start Excel and open file
$excel = New-Object -comobject Excel.Application
$workbook = $excel.Workbooks.Open($ex)
# Run excel hidden
$excel.Visible = $false
# Import CSV here
# Open excel and run saved macro
$app = $excel.Application
$app.Run($macro)
# Save and close Excel
$workbook.save()
$workbook.close()
$excel.Quit()
I am trying to import a CSV file in to an existing excel sheet. I can get it to work with an .xlsx file, but I can't figure it out with a macro enabled sheet. The reason for the macro enabled sheet, is that I want to be able to use macros, after the data has imported, so I can create a PIVOT table.
I have another powershell script running, collecting users and group information, and I want to create an easy to read excel sheet.
$csv = 'C:\\test\CSV\ADusers.csv'
$extemp = 'C:\test\Temp\WithVBA.xlsm'
$ex = 'C:\test\Pivot.xlsm'
$macro = 'add'
# Adding header to CSV
$filedata = import-csv $csv -Header "UserID,GroupID"
$filedata | Export-csv -Path $csv
# copy temp with VBA to new sheet
Copy-Item -Path $extemp -Destination $ex
# start Excel and open file
$excel = New-Object -comobject Excel.Application
$workbook = $excel.Workbooks.Open($ex)
# Run excel hidden
$excel.Visible = $false
# Import CSV here
# Open excel and run saved macro
$app = $excel.Application
$app.Run($macro)
# Save and close Excel
$workbook.save()
$workbook.close()
$excel.Quit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论