npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

to-xlsx

v0.0.5

Published

A powerful JavaScript/TypeScript library for exporting data to Excel XLSX files with advanced formatting, grouping, and calculation features.

Readme

to-xlsx

A powerful JavaScript/TypeScript library for exporting data to Excel XLSX files with advanced formatting, grouping, and calculation features.

npm version License: MIT

✨ Features

  • 📊 Export Arrays to Excel - Convert JavaScript/TypeScript arrays to XLSX files
  • 🎨 Advanced Styling - Customize colors, fonts, backgrounds, and borders
  • 📝 Custom Headers & Titles - Add custom column headers and report titles
  • 📏 Column Management - Set custom widths, reorder, merge, or exclude columns
  • 🗂️ Data Grouping - Group data with custom conditions and styled subtitles
  • 📄 Multi-Sheet Support - Split data across multiple worksheets
  • 🧮 Calculations - Automatic subtotals and grand totals with multiple operations
  • 🎯 TypeScript Support - Full type safety and IntelliSense support

📦 Installation

# npm
npm install to-xlsx

# pnpm
pnpm add to-xlsx

# yarn
yarn add to-xlsx

🚀 Quick Start

import { exportToXlsx } from "to-xlsx";

const data = [
    { name: "John", age: 28, department: "IT", salary: 45000 },
    { name: "Jane", age: 32, department: "HR", salary: 55000 },
    { name: "Bob", age: 25, department: "IT", salary: 35000 },
];

// Basic export
exportToXlsx({
    data,
    fileName: "employees",
});

📚 Usage Examples

🎨 Styled Export with Custom Headers

exportToXlsx({
    data: employees,
    fileName: "employee-report",
    title: {
        text: "Employee Directory",
        bg: "4472C4",
        color: "FFFFFF",
        fontSize: 16,
        border: {
            all: { style: "thick", color: "000000" },
        },
    },
    columnHeaders: {
        name: "Full Name",
        age: "Age",
        department: "Department",
        salary: "Annual Salary",
    },
    columnSizes: {
        name: 25,
        age: 10,
        department: 20,
        salary: 15,
    },
    columnsStyle: {
        bg: "70AD47",
        color: "FFFFFF",
        fontSize: 12,
    },
});

🗂️ Data Grouping with Subtotals

exportToXlsx({
    data: employees,
    fileName: "employees-by-age-group",
    groupBy: {
        conditions: [
            {
                label: "Young (Under 30)",
                filter: (item) => item.age < 30,
            },
            {
                label: "Mid-Career (30-40)",
                filter: (item) => item.age >= 30 && item.age < 40,
            },
            {
                label: "Senior (40+)",
                filter: (item) => item.age >= 40,
            },
        ],
        subtitleStyle: {
            bg: "BDD7EE",
            color: "000000",
            fontSize: 14,
            border: {
                bottom: { style: "medium", color: "0070C0" },
            },
        },
        showSubtotals: true,
        subtotalStyle: {
            bg: "E6F3FF",
            color: "000000",
            fontSize: 11,
            border: {
                all: { style: "thin", color: "0070C0" },
            },
        },
    },
    totals: {
        columns: ["salary"],
        showGrandTotal: true,
        subtotalLabel: "Group Subtotal",
        grandTotalLabel: "Total Company Payroll",
        operations: {
            salary: "sum",
        },
        grandTotalStyle: {
            bg: "4472C4",
            color: "FFFFFF",
            fontSize: 13,
            border: {
                all: { style: "thick", color: "000000" },
            },
        },
    },
});

🧮 Advanced Calculations

exportToXlsx({
    data: salesData,
    fileName: "sales-analysis",
    totals: {
        columns: ["quantity", "revenue", "profit"],
        showGrandTotal: true,
        operations: {
            quantity: "sum", // Total units sold
            revenue: "sum", // Total revenue
            profit: "avg", // Average profit margin
        },
        grandTotalLabel: "SUMMARY TOTALS",
    },
});

🔗 Column Merging

exportToXlsx({
    data: employees,
    columnsMerge: [
        {
            keys: {
                startColumn: "firstName",
                endColumn: "lastName",
            },
            columnName: "Personal Info",
        },
        {
            keys: {
                startColumn: "department",
                endColumn: "salary",
            },
            columnName: "Work Details",
        },
    ],
});

📄 Multi-Sheet Export

exportToXlsx({
    data: employees,
    sheetsBy: {
        key: "department",
        namePattern: "$key Department", // Creates sheets like "IT Department", "HR Department"
    },
});

📖 API Reference

exportToXlsx(props: Props<T>)

Main function to export data to Excel.

Props

| Property | Type | Description | Default | | ---------------- | ------------------------ | ------------------------------------ | --------------- | | data | T[] | Array of data objects to export | Required | | fileName | string | Output file name (without extension) | "ExportSheet" | | columnHeaders | Record<string, string> | Custom column headers | null | | columnSizes | Record<string, number> | Column widths | null | | columnsStyle | ColumnsStyleType | Header row styling | null | | columnsOrder | string[] | Custom column order | null | | columnsMerge | ColumnsMergeType | Merge column headers | null | | excludeColumns | string[] | Columns to exclude | null | | sheetsBy | SheetsByType | Split into multiple sheets | null | | title | TitleType | Report title configuration | null | | groupBy | GroupByType<T> | Data grouping configuration | null | | totals | TotalsType | Calculations configuration | null |

Type Definitions

TitleType

{
    text: string;
    bg?: string;           // Background color (hex)
    color?: string;        // Text color (hex)
    fontSize?: number;     // Font size
    border?: BorderType;   // Border styling
}

GroupByType<T>

{
    conditions: GroupCondition<T>[];
    subtitleStyle?: {
        bg?: string;
        color?: string;
        fontSize?: number;
        border?: BorderType;
    };
    showSubtotals?: boolean;      // Enable subtotal rows
    subtotalStyle?: {             // Subtotal row styling
        bg?: string;
        color?: string;
        fontSize?: number;
        border?: BorderType;
    };
}

TotalsType

{
    columns: string[];                    // Columns to calculate
    showGrandTotal?: boolean;            // Show grand total row
    subtotalLabel?: string;              // Subtotal row label
    grandTotalLabel?: string;            // Grand total row label
    operations?: {                       // Calculation operations
        [columnName: string]: 'sum' | 'avg' | 'count' | 'min' | 'max';
    };
    grandTotalStyle?: {                  // Grand total styling
        bg?: string;
        color?: string;
        fontSize?: number;
        border?: BorderType;
    };
}

BorderType

{
    top?: BorderStyleType;
    left?: BorderStyleType;
    bottom?: BorderStyleType;
    right?: BorderStyleType;
    all?: BorderStyleType;    // Shorthand for all borders
}

BorderStyleType

{
    style?: 'thin' | 'dotted' | 'dashDot' | 'hair' | 'dashDotDot' |
            'slantDashDot' | 'mediumDashed' | 'mediumDashDotDot' |
            'mediumDashDot' | 'medium' | 'double' | 'thick';
    color?: string;           // Border color (hex)
}

🎨 Styling Guide

Colors

Use hex color codes without the # symbol:

  • "FF0000" for red
  • "00FF00" for green
  • "0000FF" for blue
  • "FFFFFF" for white
  • "000000" for black

Border Styles

Available border styles in order of thickness:

  • hairthinmediumthick
  • dotted, dashDot, dashDotDot for patterns
  • double for double lines

🧮 Calculation Operations

| Operation | Description | Example Use Case | | --------- | ------------------------- | ----------------------- | | sum | Addition of all values | Total sales, quantities | | avg | Average of all values | Average price, rating | | count | Count of non-empty values | Number of items | | min | Minimum value | Lowest price | | max | Maximum value | Highest score |

🔧 Advanced Features

Data Filtering for Groups

Use custom filter functions for flexible grouping:

groupBy: {
    conditions: [
        {
            label: "High Performers",
            filter: (employee) => employee.rating >= 4.5 && employee.salary > 60000,
        },
        {
            label: "New Hires",
            filter: (employee) => new Date(employee.hireDate) > new Date("2024-01-01"),
        },
    ];
}

Multiple Calculation Types

Different operations on different columns:

totals: {
    columns: ["quantity", "price", "rating"],
    operations: {
        quantity: "sum",    // Total units
        price: "avg",       // Average price
        rating: "max"       // Best rating
    }
}

🛠️ Dependencies

🤝 Contributing

Contributions are welcome! Please see our Contributing Guide for details.

📋 Code of Conduct

This project follows our Code of Conduct. Please read it before contributing.

📄 License

Licensed under the MIT License.