Home  >  Article  >  Web Front-end  >  How to use js-xlsx cell style

How to use js-xlsx cell style

php中世界最好的语言
php中世界最好的语言Original
2018-03-12 15:05:3110858browse

This time I will show you how to use the cell style of js-xlsx, and what are the precautions for using the cell style of js-xlsx. The following is a practical case, let's take a look.

Download xlsx-style

npm install xlsx-style


xlsx-style core module is in your installation path\node_modules\xlsx-style\dist

2. Sample code

Copy the file xlsx.full.min.js

Write the sample code:

<!DOCTYPE html><html><head>
    <meta charset="UTF-8">
    <title>示例</title></head><body>
    <script src="./xlsx.full.min.js"></script>
    <script>
        function saveAs(obj, fileName) {            var tmpa = document.createElement("a");
            tmpa.download = fileName || "下载";
            tmpa.href = URL.createObjectURL(obj);
            tmpa.click();
            setTimeout(function () {
                URL.revokeObjectURL(obj);
            }, 100);
        }        var jsono = [{            "id": 1, "合并的列头1": "数据11", "合并的列头2": "数据12", "合并的列头3": "数据13", "合并的列头4": "数据14",
        }, {            "id": 2, "合并的列头1": "数据21", "合并的列头2": "数据22", "合并的列头3": "数据23", "合并的列头4": "数据24",
        }];        const wopts = { bookType: &#39;xlsx&#39;, bookSST: true, type: &#39;binary&#39;, cellStyles: true };        function downloadExl(json, type) {            var tmpdata = json[0];
            json.unshift({});            var keyMap = []; //获取keys
            for (var k in tmpdata) {
                keyMap.push(k);
                json[0][k] = k;
            }            var tmpdata = [];//用来保存转换好的json 
            json.map((v, i) => keyMap.map((k, j) => Object.assign({}, {                v: v[k],                position: (j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1)
            }))).reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {                v: v.v
            });            var outputPos = Object.keys(tmpdata); //设置区域,比如表格从A1到D10
            tmpdata["B1"].s = { font: { sz: 14, bold: true, color: { rgb: "FFFFAA00" } }, fill: { bgColor: { indexed: 64 }, fgColor: { rgb: "FFFF00" } } };//<====设置xlsx单元格样式
            tmpdata["!merges"] = [{                s: { c: 1, r: 0 },                e: { c: 4, r: 0 }
            }];//<====合并单元格 
            var tmpWB = {                SheetNames: [&#39;mySheet&#39;], //保存的表标题
                Sheets: {                    &#39;mySheet&#39;: Object.assign({},
                        tmpdata, //内容
                        {                            &#39;!ref&#39;: outputPos[0] + &#39;:&#39; + outputPos[outputPos.length - 1] //设置填充区域
                        })
                }
            };
            tmpDown = new Blob([s2ab(XLSX.write(tmpWB,
                { bookType: (type == undefined ? &#39;xlsx&#39; : type), bookSST: false, type: &#39;binary&#39; }//这里的数据是用来定义导出的格式类型
            ))], {                    type: ""
                });
            saveAs(tmpDown, "这里是下载的文件名" + &#39;.&#39; + (wopts.bookType == "biff2" ? "xls" : wopts.bookType));
        }        function getCharCol(n) {            let temCol = &#39;&#39;,
                s = &#39;&#39;,
                m = 0
            while (n > 0) {
                m = n % 26 + 1
                s = String.fromCharCode(m + 64) + s
                n = (n - m) / 26
            }            return s
        }        function s2ab(s) {            if (typeof ArrayBuffer !== &#39;undefined&#39;) {                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;
            } else {                var buf = new Array(s.length);                for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;                return buf;
            }
        }    </script>
    <button onclick="downloadExl(jsono)">导出</button></body></html>

I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!

Related reading:

How to use canvas to make a useful graffiti drawing board

How to use s-xlsx to implement Excel File import and export (Part 2)

The above is the detailed content of How to use js-xlsx cell style. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn