npm package to create spreadsheet in node environment and in browser
npm i to-spreadsheet
import { generateExcel , EnvironmentType, skipCell, writeEquation } from 'to-spreadsheet/lib/index';
const sampleData = [
{
title: 'Maifee1', content: [
['meaw', 'grrr'],
['woof', 'smack'],
[1],
[1, 2],
[1, 2, 3, writeEquation('SUM(A5,C5)')],
]
},
{ title: 'Maifee2', content: [[1], [1, skipCell(3), 2]] },
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
]
generateExcel(sampleData); // <-- by default generate XLSX for node
generateExcel(sampleData, EnvironmentType.BROWSER); // <-- for browserYou can create date cells that are properly formatted in Excel:
import {
generateExcel,
createDateCell,
createBorderedDateCell,
createBackgroundDateCell
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'DateDemo',
content: [
[
'Event',
'Date',
'Styled Date'
],
[
'Project Start',
createDateCell(new Date('2024-01-01')),
createBorderedDateCell(new Date('2024-01-15'), createAllBorders())
],
[
'Milestone',
createDateCell(new Date()),
createBackgroundDateCell(new Date('2024-12-25'), '#FFCCCC')
]
]
}
];createDateCell(date, style?)- Creates a date cell with optional stylingcreateBorderedDateCell(date, border)- Creates a date cell with borderscreateBackgroundDateCell(date, backgroundColor)- Creates a date cell with background color
You can add background and foreground colors to cells:
import {
generateExcel,
createBackgroundCell,
createForegroundCell,
createColoredCell,
createStyledCell
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'ColorDemo',
content: [
[
'Feature',
'Background Color',
'Foreground Color',
'Both Colors'
],
[
'Yellow Background',
createBackgroundCell('Highlighted', '#FFFF00'),
createForegroundCell('Red Text', '#FF0000'),
createColoredCell('Green BG, Red Text', '#00FF00', '#FF0000')
],
[
'Complex Styling',
createStyledCell('Full Style', {
backgroundColor: '#FFFFCC',
foregroundColor: '#0000FF',
border: createAllBorders(BorderStyle.thick, '#000000')
}),
'Regular cell',
42
]
]
}
];createBackgroundCell(value, backgroundColor)- Creates cell with background colorcreateForegroundCell(value, foregroundColor)- Creates cell with text colorcreateColoredCell(value, backgroundColor, foregroundColor)- Creates cell with both colorscreateStyledCell(value, style)- Creates cell with full styling options
- Multiple sheet support
- Equations
- Cell borders
- Cell styling (background colors, foreground colors, dates)
- Date cells with proper Excel formatting
- Cell alignment (horizontal and vertical)
- Sheet styling
You can add borders to cells using the border functionality:
import {
generateExcel,
createBorderedCell,
createAllBorders,
createTopBorder,
createBottomBorder,
createLeftBorder,
createRightBorder,
BorderStyle
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'BorderDemo',
content: [
[
// Create cells with all borders
createBorderedCell('Header 1', createAllBorders(BorderStyle.thick, '#000000')),
createBorderedCell('Header 2', createAllBorders(BorderStyle.thick, '#000000'))
],
[
// Create cells with specific borders
createBorderedCell('Data 1', createTopBorder()),
createBorderedCell(100, createRightBorder()),
createBorderedCell('Final', createBottomBorder())
]
]
}
];
generateExcel(data);Available border styles:
BorderStyle.none- No borderBorderStyle.thin- Thin border (default)BorderStyle.medium- Medium borderBorderStyle.thick- Thick borderBorderStyle.double- Double borderBorderStyle.dotted- Dotted borderBorderStyle.dashed- Dashed border
Border Creation:
createAllBorders(style?, color?)- Creates borders on all sidescreateTopBorder(style?, color?)- Creates only top bordercreateBottomBorder(style?, color?)- Creates only bottom bordercreateLeftBorder(style?, color?)- Creates only left bordercreateRightBorder(style?, color?)- Creates only right bordercreateBorder(borderConfig)- Creates custom border configuration
Cell Creation:
createBorderedCell(value, border)- Creates a cell with bordercreateStyledCell(value, style)- Creates a cell with custom styling
import { createStyledCell, BorderStyle } from 'to-spreadsheet/lib/index';
// Custom border configuration
const customCell = createStyledCell('Custom', {
border: {
left: BorderStyle.double,
top: BorderStyle.thin,
right: BorderStyle.dashed,
bottom: BorderStyle.thick,
color: '#FF0000' // Red borders
}
});
// Mix styled and regular cells
const data = [
{
title: 'Mixed',
content: [
[customCell, 'Regular Cell', 42]
]
}
];All styling features can be combined together:
import {
createStyledCell,
createAllBorders,
BorderStyle
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'CombinedDemo',
content: [
[
// Cell with background color, text color, and borders
createStyledCell('Fully Styled', {
backgroundColor: '#FFFFCC', // Light yellow background
foregroundColor: '#0000FF', // Blue text
border: createAllBorders(BorderStyle.thick, '#FF0000') // Red thick border
}),
// Date with background and border
createDateCell(new Date(), {
backgroundColor: '#CCFFCC', // Light green background
border: createAllBorders(BorderStyle.double, '#008000') // Green double border
}),
// Regular cells for comparison
'Plain text',
42
]
]
}
];Colors should be specified in hex format:
#FF0000- Red#00FF00- Green#0000FF- Blue#FFFF00- Yellow#FF00FF- Magenta#00FFFF- Cyan#000000- Black#FFFFFF- White#CCCCCC- Light gray
You can align cell content both horizontally and vertically:
import {
generateExcel,
createHorizontallyAlignedCell,
createVerticallyAlignedCell,
createAlignedCell,
createCenteredCell,
HorizontalAlignment,
VerticalAlignment
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'AlignmentDemo',
content: [
[
'Feature',
'Horizontal',
'Vertical',
'Both'
],
[
'Left Align',
createHorizontallyAlignedCell('Left Text', HorizontalAlignment.left),
createVerticallyAlignedCell('Top Text', VerticalAlignment.top),
createAlignedCell('Top-Left', HorizontalAlignment.left, VerticalAlignment.top)
],
[
'Center Align',
createHorizontallyAlignedCell('Center Text', HorizontalAlignment.center),
createVerticallyAlignedCell('Center Text', VerticalAlignment.center),
createCenteredCell('Full Center')
],
[
'Right Align',
createHorizontallyAlignedCell('Right Text', HorizontalAlignment.right),
createVerticallyAlignedCell('Bottom Text', VerticalAlignment.bottom),
createAlignedCell('Bottom-Right', HorizontalAlignment.right, VerticalAlignment.bottom)
]
]
}
];HorizontalAlignment.general- General alignment (Excel default)HorizontalAlignment.left- Left alignmentHorizontalAlignment.center- Center alignmentHorizontalAlignment.right- Right alignmentHorizontalAlignment.fill- Fill alignmentHorizontalAlignment.justify- Justify alignmentHorizontalAlignment.centerContinuous- Center across selectionHorizontalAlignment.distributed- Distributed alignment
VerticalAlignment.top- Top alignmentVerticalAlignment.center- Center alignmentVerticalAlignment.bottom- Bottom alignmentVerticalAlignment.justify- Justify alignmentVerticalAlignment.distributed- Distributed alignment
createHorizontallyAlignedCell(value, alignment)- Creates cell with horizontal alignmentcreateVerticallyAlignedCell(value, alignment)- Creates cell with vertical alignmentcreateAlignedCell(value, horizontal, vertical)- Creates cell with both alignmentscreateCenteredCell(value)- Creates center-aligned cell (convenience function)
Alignment works seamlessly with all other styling features:
import {
createStyledCell,
HorizontalAlignment,
VerticalAlignment,
createAllBorders,
BorderStyle
} from 'to-spreadsheet/lib/index';
const data = [
{
title: 'ComplexStyling',
content: [
[
// Full styling with alignment, colors, and borders
createStyledCell('Complete Style', {
horizontalAlignment: HorizontalAlignment.center,
verticalAlignment: VerticalAlignment.center,
backgroundColor: '#CCFFCC',
foregroundColor: '#FF0000',
border: createAllBorders(BorderStyle.thick, '#000000')
}),
// Aligned date cell
createDateCell(new Date(), {
horizontalAlignment: HorizontalAlignment.right,
verticalAlignment: VerticalAlignment.center,
backgroundColor: '#FFFFCC'
}),
// Simple centered text
createCenteredCell('Centered')
]
]
}
];