如何使用 PowerShell 将 CSV 导出到 Excel

  1. 使用 Excel COM 对象 PowerShell 导出 CSV 文件到 Excel 文件
  2. 使用 Excel COM 对象 PowerShell 导出 CSV 文件到 Excel 文件
  3. 使用 PSExcel PowerShell 导出 CSV 文件到 Excel 文件
  4. 结论
如何使用 PowerShell 将 CSV 导出到 Excel

CSV(逗号分隔值)是一种以单纯文本格式存储数据的文件,采用表格形式。它包含由逗号或其他分隔符(如分号)分隔的数据列表。

CSV 文件的扩展名为 .csv,可以使用电子表格程序查看,如 Microsoft Excel。扩展名为 .xlsx 的文件是 Microsoft Excel 工作表文件。

本文深入探讨了使用 PowerShell 将 CSV 文件导出到 Excel 的具体细节,涵盖了三种不同的方法:Excel COM 对象、ImportExcel 模块和 PSExcel 模块。每种方法适用于不同的场景和需求,提供独特的优势。

使用 Excel COM 对象 PowerShell 导出 CSV 文件到 Excel 文件

Excel COM 对象方法在处理 Windows 环境中的数据转换和报告时特别有用。这种方法非常适合需要自动将 CSV 数据转换为更用户友好的 Excel 格式的用户,这在商业和数据分析中被广泛使用。

脚本:

 powershellCopy# Define the CSV file path and the Excel file path
$csvPath = "C:\path\file.csv"
$excelPath = "C:\path\file.xlsx"

# Start Excel application
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.worksheets.Item(1)

# Import CSV data into the worksheet
$row = 1
Import-Csv $csvPath | ForEach-Object {
    $col = 1
    foreach ($value in $_.PSObject.Properties.Value) {
        $worksheet.Cells.Item($row, $col) = $value
        $col++
    }
    $row++
}

# Save and close Excel workbook
$workbook.SaveAs($excelPath)
$excel.Quit()

在这个脚本中,我们首先定义源 CSV 文件和目标 Excel 文件的路径。然后,我们初始化一个 Excel 应用程序实例,并添加一个带有单个工作表的新工作簿。

Import-Csv 命令读取 CSV 文件,我们使用 ForEach-Object 遍历每一条记录。在循环内部,我们将每个值分配给 Excel 工作表中相应的单元格。

在用 CSV 文件中的所有数据填充工作表后,我们将工作簿保存到指定路径,并关闭 Excel 应用程序。

输出:

使用 PowerShell 导出 CSV 到 Excel - 输出 1

使用 Excel COM 对象 PowerShell 导出 CSV 文件到 Excel 文件

Excel COM 对象方法在处理 Windows 环境中的数据转换和报告时特别有用。这种方法非常适合需要自动将 CSV 数据转换为更用户友好的 Excel 格式的用户,这在商业和数据分析中被广泛使用。

首先,如果您尚未安装 ImportExcel 模块,则需要安装它。

 powershellCopyInstall-Module -Name ImportExcel -Force -Scope CurrentUser

脚本:

 powershellCopy# Install the ImportExcel module if not already installed
# Install-Module -Name ImportExcel -Scope CurrentUser

# Define the CSV and Excel file paths
$csvPath = "C:\path\file.csv"
$excelPath = "C:\path\file.xlsx"

# Import data from CSV and export to Excel
Import-Csv -Path $csvPath | Export-Excel -Path $excelPath

# Print completion message
Write-Host "CSV data has been exported to Excel at: $excelPath"

在这个脚本中,我们首先检查 ImportExcel 模块是否已安装。此模块默认不包含在 PowerShell 中,可能需要通过 Install-Module 从 PowerShell Gallery 安装。

接下来,我们定义源 CSV 文件和目标 Excel 文件的文件路径。Import-Csv 命令读取 CSV 文件,管道(|)将导入的数据直接传递给 Export-Excel 命令,该命令在指定位置创建 Excel 文件。

这个脚本旨在简单高效,即使是对 PowerShell 脚本不熟悉的人,也能轻松访问。它有效地演示了使用 PowerShell 将数据从 CSV 转换为 Excel 格式的简易性。

输出:

使用 PowerShell 将 CSV 导出到 Excel - 输出 2

使用 PSExcel PowerShell 导出 CSV 文件到 Excel 文件

PSExcel 模块利用 PowerShell 的能力与 Excel 文件进行交互,而无需在系统上安装 Excel。这使得它在无法安装 Excel 的环境中特别有用,比如服务器或自动化系统。

PSExcel 广泛用于报告生成、数据分析以及将数据转换为更具可呈现性和可访问性的格式,如 Excel。

首先,如果您尚未安装 PSExcel 模块,则需要安装它。

 powershellCopyInstall-Module -Name PSExcel -Force -Scope CurrentUser

脚本:

 powershellCopy#Define the CSV and Excel file paths
$csvPath = "C:\path\file.csv"
$excelPath = "C:\path\file.xlsx"

# Import data from CSV
$csvData = Import-Csv -Path $csvPath

# Export data to Excel
$csvData | Export-Excel -Path $excelPath

# Print completion message
Write-Host "CSV data has been exported to Excel at: $excelPath"

在这个脚本中,我们首先确保 PSExcel 模块已安装。它不包含在 PowerShell 的默认安装中,可以从 PowerShell Gallery 安装。

然后,我们定义源 CSV 文件和目标 Excel 文件的路径。Import-Csv 命令用于将 CSV 文件读取到变量中。

此导入的数据接着被传递到 Export-Excel 命令,该命令在指定位置创建 Excel 文件。

输出:

使用 PowerShell 导出 CSV 到 Excel - 输出 3

结论

这份关于使用 PowerShell 将 CSV 文件导出到 Excel 的综合指南展示了 PowerShell 在处理数据转换任务中的灵活性和强大功能。每种方法——Excel COM 对象、ImportExcel 模块和 PSExcel 模块——都提供了独特的好处,适应不同的环境和需求。

Excel COM 对象是与 Windows 环境中 Excel 直接交互的强大选择,适合复杂的数据处理任务。ImportExcelPSExcel 模块则提供了另一种方法,扩展了 PowerShell 的功能,让用户在没有 Excel 软件时也能轻松处理 Excel 文件。

这些方法不仅强调了 PowerShell 的多功能性,还提供了自动化和简化数据转换任务的实用解决方案。无论您是 PowerShell 脚本的新手还是经验丰富的专业人士,这些方法都为您增强数据处理和报告能力提供了有价值的工具,适用于各种场景。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

相关文章 - PowerShell Export