@oniryk/dreamer-xls
is a wrapper of node-xlsx
that provides a more convenient way to deliver AdonisJS v6 responses in xlsx format.
This package is part of @oniryk/dreamer
. It is not intended to be a general purpose package, but you can use it if you want.
npm install @oniryk/dreamer-xls
If you are using @oniryk/dreamer
, you can use it like this:
import Post from '#models/post'
import { index } from '@oniryk/dreamer/extensions/crud'
import xls from '@oniryk/dreamer-xls'
export default class PostsController {
public index = index(Post, {
formats: [ xls() ],
})
// ..
}
You also can use it directly:
import { HttpContext } from '@adonisjs/core/http'
import xls from '@oniryk/dreamer-xls'
export default class PostsController {
public async index(context: HttpContext) {
const data = await Post.all()
await xls()(context, data)
}
}
You can pass an object to the xls
function to customize the output:
xls({
// Define the filename when downloading
filename: 'posts.xlsx', // default is 'export.xlsx'
// Define the sheet name
sheetName: 'Posts',
// Define the columns to export
columns: ['title', 'content', 'author', 'created_at'],
// Translate the column names
i18n: {
title: 'Title',
content: 'Content',
},
// You can also pass the same options as node-xlsx
// See https://www.npmjs.com/package/node-xlsx#usage
sheetOptions: {
'!cols': [
{ wch: 20 },
{ wch: 40 },
{ wch: 20 },
{ wch: 20 },
],
},
})