Front-end JS export Excel table

1, install the dependency module NPM install XLSX -d

2. Create HTML files

  <script lang="javascript" src="./node_modules/xlsx/xlsx.mini.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
Copy the code

3, use,

  <script type="text/javascript">
        window.onload = function () {
            $.ajax({
                url: "https://xxx/biz.h5/home/progRing? channelNo=84322&progNo=240749&pageIndex=1&pageSize=10".type: "GET".dataType: "jsonp".// Specify the type of data returned by the server
                success: function (data) {
                    const filename = "file.xlsx"; // File name
                    const data1 = ['aWord'.'introduction'."listenTimes"]
                    var arrop = []
                    arrop.push(data1)
                    for (var i = 0; i < data.listRing.length; i++) {
                        let arr = []
                        arr.push(data.listRing[i].aWord)
                        arr.push(data.listRing[i].introduction)
                        arr.push(data.listRing[i].ringName)
                        arrop.push(arr)
                    }
                    // Data, be sure to note the need for a two-dimensional array
                    var ws_name = "Sheet1"; // Name of the first sheet in Excel
                    var wb = XLSX.utils.book_new(),
                        ws = XLSX.utils.aoa_to_sheet(arrop);
                    XLSX.utils.book_append_sheet(wb, ws, ws_name); // Add data to the workbook
                    XLSX.writeFile(wb, filename); / / export Excel}}); } </script>` ``

Copy the code