sheetjs/demos/server/sheetjs.controller.ts
2022-02-26 01:48:40 -04:00

20 lines
729 B
TypeScript

import { Controller, Logger, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { readFile, utils } from 'xlsx';
@Controller('sheetjs')
export class SheetjsController {
private readonly logger = new Logger(SheetjsController.name);
@Post('upload-xlsx-file')
@UseInterceptors(FileInterceptor('file'))
async uploadXlsxFile(@UploadedFile() file: Express.Multer.File) {
// Open the uploaded XLSX file and perform SheetJS operations
const workbook = readFile(file.path);
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
const output = utils.sheet_to_csv(firstSheet);
this.logger.log(output);
return output;
}
}