Convert Figma logo to code with AI

dfinke logoImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel

2,567
412
2,567
88

Top Related Projects

A pure PHP library for reading and writing spreadsheet files

A Python module for creating Excel XLSX files.

35,733

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

Quick Overview

ImportExcel is a PowerShell module that provides powerful tools for working with Excel files. It allows users to read, write, and manipulate Excel spreadsheets directly from PowerShell scripts without requiring Microsoft Excel to be installed on the system.

Pros

  • Easy to use and integrate into existing PowerShell scripts
  • Supports both reading from and writing to Excel files
  • Works without requiring Microsoft Excel installation
  • Offers advanced features like pivot tables, charts, and formulas

Cons

  • Limited to Excel file formats (.xlsx, .xlsm)
  • May have performance issues with very large datasets
  • Some advanced Excel features might not be fully supported
  • Learning curve for users unfamiliar with PowerShell

Code Examples

  1. Reading data from an Excel file:
Import-Excel -Path ".\data.xlsx" -WorksheetName "Sheet1"
  1. Creating a new Excel file with data:
$data = @(
    [PSCustomObject]@{Name="John"; Age=30},
    [PSCustomObject]@{Name="Jane"; Age=28}
)
$data | Export-Excel -Path ".\output.xlsx" -WorksheetName "Employees"
  1. Adding a chart to an Excel file:
$data | Export-Excel -Path ".\chart.xlsx" -WorksheetName "Sales" -AutoSize -AutoFilter
Add-ExcelChart -Worksheet (Get-ExcelWorksheet -Path ".\chart.xlsx" -WorksheetName "Sales") -ChartType ColumnClustered -XRange "A2:A5" -YRange "B2:B5" -Title "Sales by Quarter"

Getting Started

  1. Install the module:
Install-Module -Name ImportExcel
  1. Import the module in your PowerShell script:
Import-Module ImportExcel
  1. Start using the module's functions:
$data = Import-Excel -Path ".\data.xlsx"
$data | Where-Object { $_.Status -eq "Active" } | Export-Excel -Path ".\active_users.xlsx"

Competitor Comparisons

A pure PHP library for reading and writing spreadsheet files

Pros of PhpSpreadsheet

  • More comprehensive support for Excel features, including advanced formatting and formulas
  • Better performance when handling large spreadsheets
  • Actively maintained with regular updates and improvements

Cons of PhpSpreadsheet

  • Steeper learning curve due to more complex API
  • Requires more setup and configuration compared to ImportExcel
  • Higher memory usage, especially for large spreadsheets

Code Comparison

ImportExcel (PowerShell):

$excel = Import-Excel -Path "data.xlsx"
$excel | Where-Object { $_.Column1 -eq "Value" } | Export-Excel -Path "output.xlsx"

PhpSpreadsheet (PHP):

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("data.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
$filteredData = array_filter($worksheet->toArray(), function($row) {
    return $row[0] == "Value";
});
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("output.xlsx");

ImportExcel is more concise and easier to use for simple tasks, while PhpSpreadsheet offers more control and advanced features at the cost of verbosity.

A Python module for creating Excel XLSX files.

Pros of XlsxWriter

  • Cross-platform compatibility (Python-based, works on Windows, macOS, and Linux)
  • Extensive documentation and examples
  • Supports a wide range of Excel features, including charts and formulas

Cons of XlsxWriter

  • Requires Python knowledge, which may be a barrier for some users
  • Limited to creating new Excel files, cannot modify existing ones
  • May have a steeper learning curve for users familiar with PowerShell

Code Comparison

XlsxWriter:

import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello Excel!')
workbook.close()

ImportExcel:

$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Cells.Item(1,1) = "Hello Excel!"
$workbook.SaveAs("example.xlsx")
$excel.Quit()

Both libraries allow for creating Excel files programmatically, but XlsxWriter uses Python and offers more advanced features, while ImportExcel leverages PowerShell and COM objects for Excel manipulation. XlsxWriter is more suitable for cross-platform development and complex Excel operations, while ImportExcel is better for Windows-centric environments and users familiar with PowerShell.

35,733

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Pros of SheetJS

  • Cross-platform compatibility: Works in browsers, Node.js, and various JavaScript environments
  • Supports a wide range of spreadsheet formats, including XLS, XLSX, CSV, and more
  • Robust parsing and writing capabilities for complex spreadsheet structures

Cons of SheetJS

  • Steeper learning curve due to its extensive API and features
  • Larger file size, which may impact performance in browser-based applications
  • Commercial use requires a paid license

Code Comparison

ImportExcel (PowerShell):

Import-Excel -Path "data.xlsx" -WorksheetName "Sheet1" |
    Where-Object { $_.Status -eq "Active" } |
    Export-Excel -Path "output.xlsx"

SheetJS (JavaScript):

const workbook = XLSX.readFile('data.xlsx');
const sheet = workbook.Sheets['Sheet1'];
const data = XLSX.utils.sheet_to_json(sheet);
const filtered = data.filter(row => row.Status === 'Active');
XLSX.writeFile(XLSX.utils.json_to_sheet(filtered), 'output.xlsx');

Summary

ImportExcel is a PowerShell-specific module for Excel manipulation, offering a simpler syntax for common tasks. SheetJS provides a more versatile solution for JavaScript environments, supporting multiple formats and platforms. While ImportExcel is free for all uses, SheetJS requires a license for commercial applications. The choice between them depends on the specific programming environment and project requirements.

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

Pros of ClosedXML

  • More comprehensive Excel manipulation capabilities, including advanced formatting and cell styles
  • Better performance for large datasets and complex operations
  • Stronger type safety and compile-time checks

Cons of ClosedXML

  • Steeper learning curve due to more complex API
  • Requires more code to perform simple tasks compared to ImportExcel
  • Less integrated with PowerShell environment

Code Comparison

ImportExcel:

$excel = Import-Excel -Path "data.xlsx"
$excel | Export-Excel -Path "output.xlsx" -AutoSize

ClosedXML:

using (var workbook = new XLWorkbook("data.xlsx"))
{
    var worksheet = workbook.Worksheet(1);
    worksheet.Columns().AdjustToContents();
    workbook.SaveAs("output.xlsx");
}

ClosedXML offers more granular control over Excel operations but requires more verbose code. ImportExcel provides a simpler, PowerShell-native approach for basic tasks. ClosedXML is better suited for complex Excel manipulations in C# applications, while ImportExcel excels in PowerShell scripting scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PowerShell and Excel


Has the ImportExcel module helped you?

  • Made you look good to the boss?
  • Saved you time?
  • Made you more productive?

Consider donating. Thank you!

Donate



Follow on Twitter Subscribe on YouTube

Donate

Overview

Automate Excel with PowerShell without having Excel installed. Works on Windows, Linux and Mac. Creating Tables, Pivot Tables, Charts and much more just got a lot easier.

Examples ✨

Check out the more than 100 examples on ways to create amazing reports as well as make you more productive with PowerShell and Excel.

Basic Usage

Installation

Install-Module -Name ImportExcel

Create a spreadsheet

Here is a quick example that will create spreadsheet file from CSV data. Works with JSON, Databases, and more.

$data = ConvertFrom-Csv @"
Region,State,Units,Price
West,Texas,927,923.71
North,Tennessee,466,770.67
East,Florida,520,458.68
East,Maine,828,661.24
West,Virginia,465,053.58
North,Missouri,436,235.67
South,Kansas,214,992.47
North,North Dakota,789,640.72
South,Delaware,712,508.55
"@

$data | Export-Excel .\salesData.xlsx

Read a spreadsheet

Quickly read a spreadsheet document into a PowerShell array.

$data = Import-Excel .\salesData.xlsx

$data
Region State        Units Price
------ -----        ----- -----
West   Texas        927   923.71
North  Tennessee    466   770.67
East   Florida      520   458.68
East   Maine        828   661.24
West   Virginia     465   053.58
North  Missouri     436   235.67
South  Kansas       214   992.47
North  North Dakota 789   640.72
South  Delaware     712   508.55

Add a chart to spreadsheet

Chart generation is as easy as 123. Building charts based on data in your worksheet doesn't get any easier.

Plus, it is automated and repeatable.

$data = ConvertFrom-Csv @"
Region,State,Units,Price
West,Texas,927,923.71
North,Tennessee,466,770.67
East,Florida,520,458.68
East,Maine,828,661.24
West,Virginia,465,053.58
North,Missouri,436,235.67
South,Kansas,214,992.47
North,North Dakota,789,640.72
South,Delaware,712,508.55
"@

$chart = New-ExcelChartDefinition -XRange State -YRange Units -Title "Units by State" -NoLegend

$data | Export-Excel .\salesData.xlsx -AutoNameRange -ExcelChartDefinition $chart -Show

Add a pivot table to spreadsheet

Categorize, sort, filter, and summarize any amount data with pivot tables. Then add charts.

$data = ConvertFrom-Csv @"
Region,State,Units,Price
West,Texas,927,923.71
North,Tennessee,466,770.67
East,Florida,520,458.68
East,Maine,828,661.24
West,Virginia,465,053.58
North,Missouri,436,235.67
South,Kansas,214,992.47
North,North Dakota,789,640.72
South,Delaware,712,508.55
"@

$data | Export-Excel .\salesData.xlsx -AutoNameRange -Show -PivotRows Region -PivotData @{'Units'='sum'} -PivotChartType PieExploded3D

Convert Excel data to other formats

Create a separate CSV file for each Excel sheet

Do you have an Excel file with multiple sheets and you need to convert each sheet to CSV file?

Problem Solved

The yearlyRetailSales.xlsx has 12 sheets of retail data for the year.

This single line of PowerShell converts any number of sheets in an Excel workbook to separate CSV files.

(Import-Excel .\yearlyRetailSales.xlsx *).GetEnumerator() |
ForEach-Object { $_.Value | Export-Csv ($_.key + '.csv') }

Additional Resources

Videos

More Videos

Articles

TitleAuthorTwitter
More tricks with PowerShell and ExcelJames O'Neill@jamesoneill
Using the Import-Excel module: Part 1 ImportingJames O'Neill@jamesoneill
Using the Import Excel module part 2: putting data into .XLSx filesJames O'Neill@jamesoneill
Using the import Excel Module: Part 3, Pivots and charts, data and calculationsJames O'Neill@jamesoneill
Export AdventureWorksDW2017 to Excel for a Power BI Demo with Export-ExcelAaron Nelson@sqlvariant
Creating beautiful Powershell Reports in ExcelDoug Finke@dfinke
PowerShell Excel and Conditional FormattingDoug Finke@dfinke
Learn to Automate Excel like a Pro with PowerShellDoug Finke@dfinke

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

Original README.md

NPM DownloadsLast 30 Days