DEV Community

Cover image for How to Export Real Excel Workbooks in JavaScript Using Connected Workbooks
Meni Kroizer
Meni Kroizer

Posted on

How to Export Real Excel Workbooks in JavaScript Using Connected Workbooks

if you're exporting data to Excel today, chances are you're generating CSV files or using a basic XLSX utility. Those approaches work—but they hit limits quickly:

  • CSV can’t preserve data types
  • No formulas, formatting, or tables
  • No Power Query or refresh logic
  • No way to integrate with Excel Online When developers want actual Excel behavior, not just a file with .xlsx at the end, CSV falls short. That’s where Connected Workbooks comes in. This guide walks you through what the library is, how it works, and how to use it to export real Excel workbooks directly from your JavaScript or TypeScript application.

1. Introduction

CSV exports are simple but limited. They can’t express:

  • Rich tables
  • Metadata
  • Styling
  • Reusable worksheets
  • Query connections
  • Real Excel structures

For teams building dashboards, admin tools, or analytics views, CSV‑only exports become frustrating. What you really want is an Excel workbook that behaves like something produced by Excel—tables, queries, refresh logic, templates.
Connected Workbooks gives you that with a few lines of code.


2. What Is Connected Workbooks?

Connected Workbooks is an open-source JavaScript/TypeScript library maintained by Microsoft.
Repo: https://github.com/microsoft/connected-workbooks
Its purpose is simple:

Take data in your web or backend application and generate a real Excel (.xlsx) workbook—complete with tables, queries, or templates.

It acts as a bridge between your app and Excel:

  • Your app provides HTML tables, arrays, grid data, and Power Query query
  • Connected Workbooks turns that into structured Excel tables
  • Produces a Blob
  • You can download it or open it instantly in Excel Online

This makes exporting Excel files feel like generating PDFs—simple, declarative, reliable.


3. How Connected Workbooks Works

The mental model is straightforward:
Your Application → Connected Workbooks → Excel Workbook (.xlsx)
Data sources you provide

  • HTML <table> elements
  • Grid or array data
  • Power Query mashups
  • Excel templates with named tables

What the library produces

  • Excel tables (not static pasted values)
  • Workbooks with sheets, metadata, and table definitions
  • Optional Power Query connections
  • A final .xlsx Blob/Buffer

Static exports vs. Connected Workbooks

CSV/Basic XLSX Connected Workbooks
Raw values Real Excel tables
No metadata Column types, names,formats
No refresh logic Optional Power Query refreah
No structual awareness Template, table injection

4. Installation

npm install @microsoft/connected-workbooks
Enter fullscreen mode Exit fullscreen mode

5. Practical Walkthrough: Exporting an HTML Table

Let’s build the simplest useful example:
Take an existing <table> from the DOM and export it as a real Excel table.
for example


<table id="salesTable">
  <thead>
    <tr>
      <th>Product</th>
      <th>Units</th>
      <th>Revenue</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>Keyboard</td><td>12</td><td>480</td></tr>
    <tr><td>Mouse</td><td>18</td><td>360</td></tr>
  </tbody>
</table>

Enter fullscreen mode Exit fullscreen mode

Generate a workbook from that table


import { WorkbookManager } from "@microsoft/connected-workbooks";

const workbookManager = new WorkbookManager();

async function exportTable() {
  const tableElement = document.getElementById("salesTable");

  const blob = await workbookManager.generateTableWorkbookFromHtml(tableElement);

  // Option 1 — download locally
  workbookManager.download(blob, "sales.xlsx");

  // Option 2 — open directly in Excel Online
  workbookManager.openInExcelWeb(blob, "sales.xlsx", true);
}

Enter fullscreen mode Exit fullscreen mode

This instantly gives your users a professional, Excel‑native export—without building any XLSX structures by hand.


Real-World Use Cases

Here are places where Connected Workbooks becomes a superpower:
Analytics dashboards
Export charts, tables, and raw data into a workbook analysts can modify.
Reporting tools
Monthly or weekly reports based on templates with prebuilt formulas.
Admin / internal systems
Simple "Export to Excel" buttons that create real Excel tables—not plain CSVs.
These scenarios benefit from structure, formatting, Power Query, or templates.


Conclusion

If you’ve outgrown CSV exports or want to give your users a native Excel experience, Connected Workbooks is a practical, developer-friendly option. It keeps your code simple while producing real, structured Excel workbooks that users can trust.
Explore more patterns, examples, and utility functions here:
https://github.com/microsoft/connected-workbooks

Top comments (0)