Home Web Front-end JS Tutorial How to use js-xlsx cell style

How to use js-xlsx cell style

Mar 12, 2018 pm 03:05 PM
javascript Cell

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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to prevent Excel from removing leading zeros How to prevent Excel from removing leading zeros Feb 29, 2024 am 10:00 AM

Is it frustrating to automatically remove leading zeros from Excel workbooks? When you enter a number into a cell, Excel often removes the leading zeros in front of the number. By default, it treats cell entries that lack explicit formatting as numeric values. Leading zeros are generally considered irrelevant in number formats and are therefore omitted. Additionally, leading zeros can cause problems in certain numerical operations. Therefore, zeros are automatically removed. This article will teach you how to retain leading zeros in Excel to ensure that the entered numeric data such as account numbers, zip codes, phone numbers, etc. are in the correct format. In Excel, how to allow numbers to have zeros in front of them? You can preserve leading zeros of numbers in an Excel workbook, there are several methods to choose from. You can set the cell by

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

WPS divides one cell into two WPS divides one cell into two Mar 20, 2024 pm 06:00 PM

WPS software is an indispensable helper for text and document processing. In practical applications, it is often necessary to operate in cells to process text and documents. There is a lot of knowledge about cells. Today, let me introduce the specific steps on how to divide a cell into two in WPS. 1. First, we open the WPS table that needs to be edited and select the cell document that needs to be divided into two cells. 2. Click to open &quot;Column&quot; in the data. 3. Then select &quot;Fixed Width&quot; in the pop-up window and click &quot;Next&quot;. 4. In the pop-up window, click where you want to separate columns and press Enter to confirm. Draw inferences from one example. If you need to change it to 3, you can also change the number to 3. 5. Then click Finish. 6. There is another way

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to sum cells in multiple worksheets in Excel How to sum cells in multiple worksheets in Excel Feb 19, 2024 pm 01:57 PM

This article will demonstrate how to sum cells in multiple worksheets in Excel. Microsoft Excel is a powerful spreadsheet program used for data management. When working with data, you may need to sum across multiple cells. This guide will show you how to achieve this easily. How to sum cells in multiple worksheets in Excel When summing cells in multiple worksheets in Excel, you may encounter the following two situations: Adding a single cell value in a range of cells Adding Values ​​We will cover both methods here. Adding a single cell value across multiple worksheets in Excel We collected a sample number containing the sales of 6 different companies for four consecutive months (from January to April)

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles