Error occurred on saving new excel file using exceljs writeBuffer - node.js

I am using exceljs library in my angular/electron project and I am trying to save a new updated excel file but an error occurred.
my code:
const workbook = new Excel.Workbook();
const data = await fs.promises.readFile('item.xlsx');
const wb = await workbook.xlsx.load(data.buffer);
const worksheet = wb.getWorksheet('Order');
const buffer = await wb.xlsx.writeBuffer();
const blob = new Blob([buffer]);
FileSaver.saveAs(blob, `${Date.now()}.xlsx`);
any suggestions.

Related

how to read excel file using excelJS & multer

I'm trying to read a CSV Excel file using exceljs and multer for the file upload.
#Post('excel')
#UseInterceptors(FileInterceptor('excel'))
async uploadFile(#UploadedFile() file: Express.Multer.File) {
const workBook = new Excel.Workbook();
await workBook.csv
.read(createReadStream(file.buffer))
.catch((err) => console.log('err'));
const sheet = workBook.getWorksheet('Sheet1');
const cellValue = sheet.getRow(0).getCell(1).value;
return cellValue;
}
But I am getting the Error: ENOENT: no such file or directory
Instead of read, you have to use load
workBook.xlsx.load(file.buffer)

Fill Data In Existing PDF Form Using Typscript

I'm trying to fill a form inside an existing PDF using typescript, so far I had no success using pdf-lib
const formPdfBytes = await axios.get(formUrl, {
responseType: "arraybuffer",
});
const pdfDoc = await PDFDocument.load(formPdfBytes.data);
const form = pdfDoc.getForm();
const nameField = form.getTextField("CharacterName 2");
nameField.setText("Mario");
const pdfBytes = await pdfDoc.save();
No errors are showing up, but when I open the PDF it is still empty

Data download functionality for API response Angular

I have an API response where I'm getting data as comma separated values and not as JSON objects. I need to export this data into CSV file. Can someone please suggest a method to do this?
You could parse your data to a JSON-object:
const apiData: string = 'value1,value2,value3';
const data: string[] = apiData.split(',');
And then you could use a combination of xlsx and fileSaver
Pseudocode:
const worksheet: xlsx.WorkSheet = xlsx.utils.json_to_sheet(data);
const workbook: xlsx.WorkBook = {
Sheets: { [worksheetName]: worksheet },
SheetNames: [worksheetName]
};
const buffer = xlsx.write(workbook, { 'csv', type: 'array' });
const blob = new Blob([buffer], { type: 'text/comma-separated-values' });
fileSaver.saveAs(blob, 'filename.csv');

fast csv alwaysWriteHeaders true wont work node js

I am new to node.js. I have to email the retrieved data in CSV format. I wrote code and it is working fine but with an empty array the headers are not included in the CSV, I get an empty excel sheet.
I used
var fastcsv = require("fast-csv");
var format = require('#fast-csv/format');
var csv = fastcsv.write(finalData, {headers: true});
I tried the code below, but it gives me the "format is not a function" error
var csv = fastcsv.write(finalData, {headers: true});
var csv = csv.format({alwaysWriteHeaders:true})
please help
Try use it like this, restructure it.
var fastcsv = require("fast-csv");
var { format } = require('#fast-csv/format');
var csv = fastcsv.write(finalData, {headers: true});
var formated = format({ alwaysWriteHeaders:true })
console.log(formated)

writeFile not woking in exceljs

i am using exceljs and try to write value in a cell but it does not working. However workbook.xlsx.readFile(filename) is working but workbook.xlsx.writeFile(filename) won't.
Here is my code:-
var Excel = require('exceljs');
var fs = require('fs')
module.exports.summary = function(req, res, next) {
try {
var filename = process.cwd() + '/template/report/summary.xlsx';
var workbook = new Excel.Workbook();
workbook.xlsx.writeFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet(1);
console.log('worksheet',worksheet);
var row = worksheet.getRow(5);
row.getCell('C').value = new Date();
row.commit();
worksheet.commit();
workbook.commit().then(function() {
console.log('xls file is written.');
});
res.json({ msg: done })
});
} catch (e) {
next(e);
}
}
Try this code, Hope this will help you
const excel = require('exceljs');
//Creating New Workbook
var workbook = new excel.Workbook();
//Creating Sheet for that particular WorkBook
var sheetName = 'Sheet1';
var sheet = workbook.addWorksheet(sheetName);
//Header must be in below format
sheet.columns = [{key:"name", header:"name"}, {key: "age", header: "age"}];
//Data must be look like below, key of data must be match to header.
var data = [{name:"Kalai", age: 24}, {name:"Vignesh", age:24}];
//adding each in sheet
for(i in data){
sheet.addRow(data[i]);
}
//Finally creating XLSX file
var fileName = "Sample.xlsx";
workbook.xlsx.writeFile(fileName).then(() => {
callback(null);
});
writeFile is no longer supported, so I suggest you to exchange it for "writeBuffer".
This code worked to me:
const buffer = workbook.xlsx.writeBuffer();
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
let EXCEL_EXTENSION = '.xlsx';
const blob= new Blob([buffer], {type: fileType});
saveAs(blob, 'filename' + EXCEL_EXTENSION);
In my case i have name like "data2022/2023" the main problem is "/" when i remove the slash "data2020" its work. Dunno why cant use unicoden"/"

Resources