-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
ABCrimson edited this page Mar 2, 2026
·
8 revisions
Common patterns and recipes for modern-xlsx.
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');
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'); }
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; });
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)';
ws.cell('A1').value = 'Spans three columns'; ws.addMergeCell('A1:C1');
ws.setColumnWidth(1, 30); // Column A ws.setRowHeight(1, 40); // Row 1 ws.setRowHidden(5, true); // Hide row 5
// 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' };
// 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', });
ws.cell('A1').value = 'Visit Example'; ws.addHyperlink('A1', 'https://example.com', { display: 'Visit Example', tooltip: 'Opens example.com', });
ws.cell('A1').value = 'Hover for comment'; ws.addComment('A1', 'Author Name', 'This is a comment on A1');
wb.addNamedRange('SalesTotal', 'Data!$A1ドル:$A2ドル'); const range = wb.getNamedRange('SalesTotal');
wb.docProperties = { title: 'Sales Report Q4', creator: 'Finance Team', description: 'Quarterly sales data', created: '2026-01-01T00:00:00Z', };
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();
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);
ws.autoFilter = 'A1:B3';
ws.pageSetup = { orientation: 'landscape', paperSize: 1, fitToWidth: 1, fitToHeight: 0, };
ws.sheetProtection = { sheet: true, selectLockedCells: false, selectUnlockedCells: false, };
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
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'
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);
modern-xlsx v1.0.0
Getting Started
Guides
- Charts & Visualizations
- Formula Engine
- Table Layout Engine
- Tables & Print Layout
- Encryption
- Feature Comparison
Reference
Migration
Project