SUM
SUM

Reputation: 1661

js-xlsx : preserve cell style while writing to .xlsx file

I have manually created a .xlsx file. I have added different colors to some rows and some of the cells have custom date format.

I am using js-xlsx npm module to read data from .xlsx file. While writing back to the same .xlsx file to update some cell values, the selected colors and date formats are lost.

I tried below code but that did not help preserve the .xlsx cell styles.

    var XLSX = require('xlsx');
    XLSX.readFile('abc.xlsx', {cellStyles: true});
        var first_sheet_name = workbook.SheetNames[1];
        var address_of_cell = 'A1';
        var worksheet = workbook.Sheets[first_sheet_name];
        var desired_cell = worksheet[address_of_cell];
        var desired_value = desired_cell.v;
        desired_cell.v = 'efg';
   XLSX.writeFile('abc.xlsx', {cellStyles: true});

Upvotes: 3

Views: 18072

Answers (3)

Josh V
Josh V

Reputation: 121

I ended up finding ExcelJS which will preserve styles when making modifications to an .xlsx file just fine. It's a very good alternative if you're not interested in paying a premium for the styling features of js-xlsx.

You can install it with npm install exceljs and the documentation on their GitHub repo (linked above) is excellent.

Upvotes: 9

Eugen M
Eugen M

Reputation: 51

If speed is not an issue i would resort to Excel Interop for reasons such as these, i know you need it installed in your computer but any office should have a microsoft suite installation.

Its excelent compatibility wise, performance wise not so great, however if you only need to change cell values and data, doing so is quite quick even for thousands and thousands of cells as long as you cast them to an array of objects.

I never had anything complex that i couldnt do with it, that includes auto size columns and rows, page print layouts, inserting/deleting cells in an odd shaped manner, formating text and any other cell properties, converting from .xslx to .xls and vice versa and so on and so forth.

What i hate about excel interop is what i hate about all interops, they are slow to process, thats it.

Upvotes: 0

alebianco
alebianco

Reputation: 2555

The stable version of js-xslx does not handle styles well.

But there's a fork, which should be merged back at some point, were extensive work has been done to support it.

You can use it already, if you trust it to be stable enough.

just npm install xlsx-style --save, replace your require('xslx') with require('xslx-style') and you should be good to go

Upvotes: 0

Related Questions