xsheetjs/demos/server/sheetjs.controller.ts

20 lines
729 B
TypeScript
Raw Normal View History

2022-02-26 05:48:40 +00:00
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;
}
}