For my React app, I'm looking for a solution to import an existing xlsx file, modify some cell values and download as an xlsx file. What I have tried:
- npm xlsx: This library seems to work but it removed all the stylings in the original file which I don't want.
import * as XLSX from "xlsx";
const response = await fetch("./PAT_report.xlsx");
const workbookArrayBuffer = await response.arrayBuffer();
const workbook = XLSX.read(workbookArrayBuffer, {
type: "array",
// Get the first sheet
const sheetName = workbook.SheetNames[1];
const worksheet = workbook.Sheets[sheetName];
// Modify cell value
const cellF12 = worksheet["F12"];
cellF12.v = 100;
// Export the modified workbook
const modifiedWorkbookArrayBuffer = XLSX.write(workbook, {
type: "array",
bookType: "xlsx",
});
const modifiedBlob = new Blob([modifiedWorkbookArrayBuffer], {
type: "application/octet-stream",
});
const url = window.URL.createObjectURL(modifiedBlob);
const a = document.createElement("a");
a.href = url;
a.download = "modified_export.xlsx";
a.click();
window.URL.revokeObjectURL(url);
- npm exceljs: I'm trying to read PAT_report.xlsx file in the same directory but I keep getting error
utils.js:156 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'F_OK')
import ExcelJS from "exceljs";
const workbook = new Excel.Workbook();
const worksheet = await workbook.xlsx.readFile("./PAT_report.xlsx");
Is there anything else I could try? Other libraries I found require license purchase.