Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/

Excel Quick Start

Edit on GitHub

Create an Excel workbook with a typed table and a chart, then read it back, with OfficeIMO.Excel, PSWriteOffice, or the officeimo CLI.

This page creates a workbook from a list of objects, adds a native Excel table and a chart, saves it, and reads the rows back. Nothing else needs to be installed: OfficeIMO.Excel writes the .xlsx package directly.

dotnet add package OfficeIMO.Excel

Create a workbook with a table and a chart

AddTable writes a header row from the property names, typed cells, and a table style. AddChart plots the same data and anchors the chart at a row and column.

C#

using OfficeIMO;
using OfficeIMO.Excel;

Order[] orders = [
    new("Contoso", "North", 12_400m),
    new("Fabrikam", "South", 8_900m),
    new("Northwind", "North", 15_100m)
];

using var book = ExcelDocument.Create("Orders.xlsx");
ExcelSheet sheet = book.AddSheet("Orders");

sheet.AddTable(orders, new OfficeTableOptions { Name = "Orders" });
sheet.AddChart(OfficeChartType.Column, orders, nameof(Order.Customer),
    new ExcelChartOptions { Title = "Revenue by customer", Row = 2, Column = 6 }, nameof(Order.Total));
sheet.AutoFitColumns();

book.Save();

record Order(string Customer, string Region, decimal Total);

PowerShell

$orders = @(
    [pscustomobject]@{ Customer = 'Contoso';   Region = 'North'; Total = 12400 }
    [pscustomobject]@{ Customer = 'Fabrikam';  Region = 'South'; Total = 8900 }
    [pscustomobject]@{ Customer = 'Northwind'; Region = 'North'; Total = 15100 }
)

New-OfficeExcel -Path .\Orders.xlsx {
    ExcelSheet 'Orders' {
        ExcelTable $orders -Name Orders
        ExcelChart -Table Orders -Category Customer -Value Total -Address F2 -Type Column -Title 'Revenue by customer'
    }
}

For a one-line export of pipeline output, use Export-OfficeExcel:

Get-Process | Select-Object Name, Id, CPU | Export-OfficeExcel -Path .\Processes.xlsx -TableName Processes

Read the rows back

RowsAs<T> maps columns to properties or constructor parameters by header name.

C#

using OfficeIMO.Excel;

using var book = ExcelDocument.Load("Orders.xlsx");
foreach (Order order in book["Orders"].RowsAs<Order>()) {
    Console.WriteLine($"{order.Customer}: {order.Total:N0}");
}

record Order(string Customer, string Region, decimal Total);

PowerShell

Import-OfficeExcel -Path .\Orders.xlsx -Sheet Orders | Where-Object Region -eq 'North'

CLI

officeimo tabular sheets Orders.xlsx
officeimo tabular schema Orders.xlsx --sheet Orders
officeimo tabular convert Orders.xlsx orders.csv --sheet Orders

Next steps

Limits and reference

Limits: see the Excel package row and the Excel format fidelity card in the compatibility matrix.

Reference: OfficeIMO.Excel API

API used on this page

Example source