Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Examples

ABCrimson edited this page Mar 2, 2026 · 8 revisions

Examples

Common patterns and recipes for modern-xlsx.

Quick Start

import { initWasm, Workbook } from 'modern-xlsx';
await initWasm();
const wb = new Workbook();
const ws = wb.addSheet('Hello');
ws.cell('A1').value = 'Hello, World!';
ws.cell('A2').value = 42;
await wb.toFile('hello.xlsx');

Reading and Modifying

import { readFile } from 'modern-xlsx';
const wb = await readFile('input.xlsx');
const ws = wb.getSheet('Sheet1');
if (ws) {
 console.log(ws.cell('A1').value);
 ws.cell('A1').value = 'Updated!';
 await wb.toFile('output.xlsx');
}

Styling Cells

const headerStyle = wb.createStyle()
 .font({ name: 'Arial', size: 14, bold: true, color: 'FFFFFF' })
 .fill({ patternType: 'solid', fgColor: '4472C4' })
 .alignment({ horizontal: 'center', vertical: 'center' })
 .border({ bottom: { style: 'medium', color: '000000' } })
 .build(wb.styles);
const headers = ['Name', 'Age', 'City'];
headers.forEach((h, i) => {
 const cell = ws.cell(`${String.fromCharCode(65 + i)}1`);
 cell.value = h;
 cell.styleIndex = headerStyle;
});

Formulas

ws.cell('A1').value = 100;
ws.cell('A2').value = 200;
ws.cell('A3').value = 300;
ws.cell('A4').formula = 'SUM(A1:A3)';
ws.cell('B1').formula = 'AVERAGE(A1:A3)';

Merged Cells

ws.cell('A1').value = 'Spans three columns';
ws.addMergeCell('A1:C1');

Column Widths and Row Heights

ws.setColumnWidth(1, 30); // Column A
ws.setRowHeight(1, 40); // Row 1
ws.setRowHidden(5, true); // Hide row 5

Frozen Panes

// Freeze top row
ws.frozenPane = { row: 1, column: 0, topLeftCell: 'A2' };
// Freeze first column
ws.frozenPane = { row: 0, column: 1, topLeftCell: 'B1' };
// Freeze both
ws.frozenPane = { row: 1, column: 1, topLeftCell: 'B2' };

Data Validation

// Dropdown list
ws.addValidation('B2', {
 validationType: 'list',
 formula1: '"Yes,No,Maybe"',
 showDropDown: true,
 prompt: 'Select an option',
 promptTitle: 'Choice',
});
// Number range
ws.addValidation('C2', {
 validationType: 'whole',
 operator: 'between',
 formula1: '1',
 formula2: '100',
 errorTitle: 'Invalid',
 error: 'Enter a number between 1 and 100',
});

Hyperlinks

ws.cell('A1').value = 'Visit Example';
ws.addHyperlink('A1', 'https://example.com', {
 display: 'Visit Example',
 tooltip: 'Opens example.com',
});

Comments

ws.cell('A1').value = 'Hover for comment';
ws.addComment('A1', 'Author Name', 'This is a comment on A1');

Named Ranges

wb.addNamedRange('SalesTotal', 'Data!$A1ドル:$A2ドル');
const range = wb.getNamedRange('SalesTotal');

Document Properties

wb.docProperties = {
 title: 'Sales Report Q4',
 creator: 'Finance Team',
 description: 'Quarterly sales data',
 created: '2026-01-01T00:00:00Z',
};

Rich Text

import { RichTextBuilder } from 'modern-xlsx';
const richText = new RichTextBuilder()
 .bold('Important: ')
 .text('Normal text. ')
 .colored('Red text', 'FF0000')
 .styled('Custom', { bold: true, italic: true, fontSize: 14 })
 .build();

Sheet Conversions

import { aoaToSheet, jsonToSheet, sheetToJson, sheetToCsv } from 'modern-xlsx';
// Array of arrays → rows
const rows = aoaToSheet([['Name', 'Age'], ['Alice', 30]]);
// JSON → rows
const rows = jsonToSheet([{ name: 'Alice', age: 30 }]);
// Rows → JSON
const data = sheetToJson(ws.rows);
// Rows → CSV
const csv = sheetToCsv(ws.rows);

Auto Filter

ws.autoFilter = 'A1:B3';

Page Setup

ws.pageSetup = {
 orientation: 'landscape',
 paperSize: 1,
 fitToWidth: 1,
 fitToHeight: 0,
};

Sheet Protection

ws.sheetProtection = {
 sheet: true,
 selectLockedCells: false,
 selectUnlockedCells: false,
};

Date Handling

import { dateToSerial, serialToDate, isDateFormatCode } from 'modern-xlsx';
dateToSerial({ year: 2026, month: 3, day: 1 }); // 46113
serialToDate(46113); // { year: 2026, month: 3, day: 1 }
isDateFormatCode('yyyy-mm-dd'); // true

Cell Reference Utilities

import { columnToLetter, letterToColumn, decodeCellRef, encodeCellRef } from 'modern-xlsx';
columnToLetter(1); // 'A'
letterToColumn('A'); // 1
decodeCellRef('B3'); // { row: 2, col: 2 }
encodeCellRef({ row: 2, col: 2 }); // 'B3'

Browser Download

import { writeBlob } from 'modern-xlsx';
const blob = writeBlob(wb);
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'output.xlsx';
a.click();
URL.revokeObjectURL(url);

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /