Excel

Import and Export from XLSX files

Use the @rowsncolumns/toolkit package to read, write and download Excel files from SheetData object

Import from Excel

Converts an excel file to sheets, sheetData which can be used in the CanvasGrid.

Note: To generate patches, undo/redo when an excel file is uploaded, use onInsertFile function from useSpreadsheetStatehook

import { createRowDataFromExcelFile } from "@rowsncolumns/toolkit";

const App = () => {
  const { onInsertFile } = useSpreadsheetState()
  const handleChange = async (e) => {
    const file = e.target.files[0]
    
    // Option 1: To let useSpreadsheetState hook to manage collab, undo/redo state
    onInsertFile(file, sheetId, activeCell)
    
    // Option 2: To manually update state
    const { sheets, sheetData, tables } = await createRowDataFromExcelFile(file)
    
    onChange(sheets)
    onChangSheetData(sheetData)
    onChangeTables(tables)
    
    // Call calculateNow to trigger a full-recalc if you are using
    // useSpreadsheetState hook
  }
  return (
    <div>
      <input type="file" onChange={handleChange} />
    </div>
  )
}

Importing large excel files

createRowDataFromExcelFile when run on the main thread can block UI interactions. You can use importExcelFile to import excel file using a web worker.

Read Excel in Node.js

The browser worker (libs/toolkit/excel-parser/worker.ts) demonstrates the low-level primitives that parse .xlsx files. You can reuse the same helpers on the server to read workbooks, stream rows, and fetch metadata such as conditional formatting, named ranges, fonts, drawings, charts, and data validations.

Key steps:

  • read the file with Node APIs (e.g. fs.readFile, S3 SDK) to obtain a Buffer

  • convert the Buffer into an ArrayBuffer before calling readExcelFile

  • reuse the worker helpers (WorkBook, getSheets, getConditionalFormatting, getDrawings, getCharts, getDataValidations, processSheetData) to assemble everything you need server-side

Streaming rows with processSheetData

Signature:

  • workbook: the shared WorkBook instance with load() already called.

  • chunkSize: how many logical rows you want in each yielded chunk (mirrors the worker's paging behavior).

  • enableCellXfsRegistry: set to true if you need the cellXfs map populated so you can reuse deduped cell formats outside the worker.

Result type (from libs/toolkit/excel-parser/helpers.ts):

cellXfs is the same memoized format registry the worker uses (a map keyed by the serialized cell format) so you can reconcile styles outside the worker.

Example: accumulate totals per sheet while keeping the formatting registry alive for later use.

Export to Excel

Toolkit provides createExcelFile and exportToExcel functions to either create a excel ArrayBuffer or create a downloadable excel file.

Last updated

Was this helpful?