question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SheetJS xlsx-style need cell style in excel

See original GitHub issue

I am trying to export excel using SheetJS/xlsx and want to format cell. I am using following code and excel is generating but can’t format a cell. Can any one point the issue or can share a complete sample code for this?

Loading library files

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="http://oss.sheetjs.com/js-xlsx/xlsx.core.min.js"></script>
<script type="text/javascript" src="http://sheetjs.com/demos/FileSaver.js"></script>

Remaining code are

function Workbook() {
    if(!(this instanceof Workbook)) return new Workbook();
    this.SheetNames = [];
    this.Sheets = {};
}

function sheet_from_array_of_arrays(data, opts) {
    var ws = {};
    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    for(var R = 0; R != data.length; ++R) {
        for(var C = 0; C != data[R].length; ++C) {
            if(range.s.r > R) range.s.r = R;
            if(range.s.c > C) range.s.c = C;
            if(range.e.r < R) range.e.r = R;
            if(range.e.c < C) range.e.c = C;
            var cell = {v: data[R][C],
                  s: { alignment: {textRotation: 90 },
                       font: {sz: 14, bold: true, color: #FF00FF }
                };
        //cell.s = {}
       /*var cell ={ v: '2.4.2014',
        t: 's',
        r: '<t>2.4.2014</t>',
        h: '2.4.2014',
        w: '2.4.2014',
        s: 
        { patternType: 'solid',
          fgColor: { theme: 8, tint: 0.3999755851924192, rgb: '9ED2E0' },
          bgColor: { indexed: 64 } } };

      */
            if(cell.v == null) continue;
            var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

            if(typeof cell.v === 'number') cell.t = 'n';
            else if(typeof cell.v === 'boolean') cell.t = 'b';
            else if(cell.v instanceof Date) {
                cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                cell.v = datenum(cell.v);
            }
            else cell.t = 's';

            ws[cell_ref] = cell;
        }
    }
    if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
}
function s2ab(s) {
    var buf = new ArrayBuffer(s.length);
    var view = new Uint8Array(buf);
    for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
    return buf;
}
function GenerateExcelFile(inData, colWidth){
  var wb = new Workbook();
  var ws = sheet_from_array_of_arrays(inData); 

  var ws_name = "SheetJS";  

  /* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
/* TEST: column widths */
ws['!cols'] = colWidth;

var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx")

}

Calling with these code

var excelData = "JSON DATA";
  var wscols = [
    {wch:30},
    {wch:20},
    {wch:20}
];

<button onclick="GenerateExcelFile(excelData,wscols)">Export</button>

Please help me finding out where I am wrong.

Thanks Suman

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

88reactions
SheetJSDevcommented, May 20, 2018

We offer this in the Pro compendium. Since companies have paid for the features already, it would be unfair to them if we turned around and made it available as open source. We have a longer comment in a gist.

0reactions
polash62commented, May 9, 2018

Then I think Datatable Export will help you… Here is a the library… https://datatables.net/extensions/buttons/examples/initialisation/export.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

SheetJS xlsx-cell styling
Personally I use xslx-js-style which completely substitutes sheetjs, while some other solutions still require you to keep in the package.json ...
Read more >
xlsx-js-style - GitHub Pages
Style Attribute Sub Attributes Values fill patternType "solid" or "none" fgColor COLOR_SPEC bgColor COLOR_SPEC
Read more >
SheetJs: Programmatically generating stylish Excel documents
Programmatically generating stylish Excel documents. Learn my strategy for utilizing the SheetJs npm module to create excel documents.
Read more >
sheetjs-style
SheetJS Spreadsheet data parser and writer. Latest version: 0.15.8, last published: a year ago. Start using sheetjs-style in your project by ...
Read more >
Tutorial | SheetJS Community Edition
The goal of this example is to generate a XLSX workbook of US President ... SheetJS Pro offers additional styling options like cell...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found