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, either provide a value
and a type
for each cell:
const data = [
// Row #1
[
// Column #1
{
value: 18,
type: Number
},
// Column #2
{
value: new Date(),
type: Date,
format: 'mm/dd/yyyy'
},
// Column #3
{
value: 'John Smith',
type: String
},
// Column #4
{
value: true,
type: Boolean
}
],
// Row #2
[
// Column #1
{
value: 16,
type: Number
},
// Column #2
{
value: new Date(),
type: Date,
format: 'mm/dd/yyyy'
},
// Column #3
{
value: 'Alice Brown',
type: String
},
// Column #4
{
value: false,
type: Boolean
}
]
]
Or provide data objects
and a schema
:
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,
// (optional) Column width (in characters).
width: 20
},
// 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 for a column or a cell then it's assumed to be a String
.
There're also some additional exported type
s available:
Integer
for integerNumber
s.URL
for URLs.Email
for email addresses.
Each column or cell, aside from having type
and value
, can also have:
width: number
— Approximate column width (in characters). Example:20
.
format: string
— A custom cell data format. Can only be used onDate
,Number
orInteger
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 columns require aformat
).
fontWeight: string
— Can be used to print text in bold. Can only be used onString
cells. Example:"bold"
.
Browser
import writeXlsxFile from 'write-excel-file'
await writeXlsxFile(objects, table, {
fileName: 'Students.xlsx'
})
Uses file-saver
to save the *.xlsx
file from a web browser.
If fileName
parameter is not passed, the returned Promise
resolves to a "blob".
Node.js
const { writeXlsxFile } = require('write-excel-file/node')
await writeXlsxFile(objects, table, {
filePath: '/path/to/file.xlsx'
})
If filePath
parameter is not passed, 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, tableDefinition, 'data.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.