JS导出excel

接口返回json数据 js生成excel此外需要这两个依赖



下载地址
https://github.com/SheetJS/js-xlsx


exports2:function (data) {
                    var table=[];
                    table.push({
                        A:"卡号",
                        B:"名称",
                        C:"商品有效期",
                        D:"面额",
                        E:"上传时间",
                        F:"兑换经销商ID",
                        G:"兑换时间",
                        H:"所属应用",
                        I:"状态",
                        J:"操作人",
                    });

                    data.forEach(function (item) {
                        var row={
                            A:item.Number,
                            B:item.Name,
                            C:item.BeginTime+"-"+item.EndTime,
                            D:item.Denomination,
                            E:item.CreateTime,
                            F:item.DealerId,
                            G:item.useTime,
                            H:item.AppName,
                            I:item.State,
                            J:item.Operator


                        };
                        table.push(row);
                    });

                    var wb = XLSX.utils.book_new();
                    var ws = XLSX.utils.json_to_sheet(table, {header:["A","B","C","D","E","F","G","H","I","J"], skipHeader:true});
                    ws['!cols']= [
                        {width: 15},
                        {width: 15},
                        {width: 22},
                        {width: 15},
                        {width: 22},
                        {width: 15},
                        {width: 15},
                        {width: 15},
                        {width: 15},
                        {width: 15}
                    ];
                    var timestamp = (new Date()).getTime();
                    XLSX.utils.book_append_sheet(wb, ws, "结果");
                    XLSX.writeFile(wb,"结果"+timestamp+".xlsx");

                },

你可能感兴趣的:(Excel)