将 CSV 导入现有 XLSM 工作表

发布于 2025-01-16 16:22:17 字数 839 浏览 4 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文