Package Exports
- write-excel-file
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (write-excel-file) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
write-excel-file
Write simple *.xlsx
files in a browser or Node.js
Also check read-excel-file
Install
npm install write-excel-file --save
If you're not using a bundler then use a standalone version from a CDN.
Use
To write an *.xlsx
file, provide the data
— an array of rows, each row being an array of cells, each cell having a type
and a value
:
const data = [
// Row #1
[
// Column #1
{
type: Number,
value: 18
},
// Column #2
{
type: Date,
value: new Date(),
format: 'mm/dd/yyyy'
},
// Column #3
{
type: String,
value: 'John Smith'
},
// Column #4
{
type: Boolean,
value: true
}
],
// Row #2
[
// Column #1
{
type: Number,
value: 16
},
// Column #2
{
type: Date,
value: new Date(),
format: 'mm/dd/yyyy'
},
// Column #3
{
type: String,
value: 'Alice Brown'
},
// Column #4
{
type: Boolean,
value: false
}
]
]
Alternatively, provide a list of objects
and a schema
to transform those objects
to data
:
const objects = [
// Row #1
{
name: 'John Smith',
age: 18,
dateOfBirth: new Date(),
graduated: true
},
// Row #2
{
name: 'Alice Brown',
age: 16,
dateOfBirth: new Date(),
graduated: false
}
]
const schema = [
// Column #1
{
column: 'Name',
type: String,
value: student => student.name
},
// Column #2
{
column: 'Age',
type: Number,
value: student => student.age
},
// Column #3
{
column: 'Date of Birth',
type: Date,
format: 'mm/dd/yyyy',
value: student => student.dateOfBirth
},
// Column #4
{
column: 'Graduated',
type: Boolean,
value: student => student.graduated
}
]
If no type
is specified, it defaults to a String
.
Aside from having a type
and a value
, each cell (or schema column) can also have:
format: string
— A custom cell data format. Can only be used onDate
orNumber
cells. Examples:"0.000"
for printing a floating-point number with 3 decimal places."#,##0.00"
for printing currency."mm/dd/yy"
for formatting a date. AllDate
cells (or schema columns) require aformat
.
fontWeight: string
— Can be used to print text in bold. Available values:"bold"
.align: string
— Can be used to align cell content horizontally. Available values:"left"
,"center"
,"right"
.
Column Width
Column width can also be specified (in "characters").
Schema
To specify column width when using a schema
, set a width
on a schema column:
const schema = [
// Column #1
{
column: 'Name',
type: String,
value: student => student.name,
width: 20 // Column width (in characters).
},
...
]
Cell Data
When not using a schema, one can provide a columns
parameter to specify a width of a column:
// Set Column #3 width to "20 characters".
const columns = [
{},
{},
{ width: 20 }, // in characters
{}
]
Browser
import writeXlsxFile from 'write-excel-file'
// When passing `data` for each cell.
await writeXlsxFile(data, {
columns, // optional
fileName: 'file.xlsx'
})
// When passing `objects` and `schema`.
await writeXlsxFile(objects, {
schema,
fileName: 'file.xlsx'
})
Uses file-saver
to save an *.xlsx
file from a web browser.
If fileName
parameter is not passed then the returned Promise
resolves to a "blob" with the contents of the *.xlsx
file.
Node.js
const { writeXlsxFile } = require('write-excel-file/node')
// When passing `data` for each cell.
await writeXlsxFile(data, {
columns, // optional
filePath: '/path/to/file.xlsx'
})
// When passing `objects` and `schema`.
await writeXlsxFile(objects, {
schema,
filePath: '/path/to/file.xlsx'
})
If filePath
parameter is not passed then the returned Promise
resolves to a Stream
-like object having a .pipe()
method:
const output = fs.createWriteStream(...)
const stream = await writeXlsxFile(objects)
stream.pipe(output)
TypeScript
Not implemented. I'm not familiar with TypeScript.
CDN
One can use any npm CDN service, e.g. unpkg.com or jsdelivr.net
<script src="https://unpkg.com/write-excel-file@1.x/bundle/write-excel-file.min.js"></script>
<script>
writeXlsxFile(objects, schema, {
fileName: 'file.xlsx'
})
</script>
References
Writing *.xlsx
files was originally copy-pasted from zipcelx
package, and then rewritten.
GitHub
On March 9th, 2020, GitHub, Inc. silently banned my account (erasing all my repos, issues and comments, even in my employer's private repos) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The GitHub repo is now only used as a backup (you can star the repo there too), and the primary repo is now the GitLab one. Issues can be reported in any repo.