首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工作簿未在js-xlsx中定义。

工作簿未在js-xlsx中定义。
EN

Stack Overflow用户
提问于 2017-02-08 07:29:52
回答 1查看 4.9K关注 0票数 0

我使用Js-xlsx API将数据导出到.xlsx文件中,我尝试使用AngularJs编写一个示例代码,无法实现it.It抛出Workbook is not defined.I引用wiki,但没有找到it.Please帮助我使用Js将数据列表写入.xlsx文件。

代码片段,

代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.2/xlsx.core.min.js"></script>

...

<button ng-click="ExportData()">Export<button>

AngularJs控制器

代码语言:javascript
复制
$scope.ExportData= function(){

var workbook=new Workbook();// throws not defined
$scope.dataArrayForExportCsv=[ {userName:data.userName, age:data.age} ];
workbook = sheet_from_array_of_arrays($scope.dataArrayForExportCsv);
XLSX.writeFile(workbook, 'sample.xlsx');
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-13 07:32:43

我能够解决这个问题,引用了下面的链接.If,您需要对.xlsx文件进行样式化,您必须将其转换为名为xlsx风格.When的民间项目,我写了这篇文章,分支没有合并到xlsx master.So,您必须使用npm下载必要的JS文件。

jszip.jsxlsx.min.js文件导入到HTML中,并执行coding.You可以使用Cell object添加单元格样式的操作。

示例单元格样式、代码片段

代码语言:javascript
复制
 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 = {};

                 //Set Header col styles
                 if(R <= 0){
                   cell = {
                    v: data[R][C],
                    s : {
                            fill : {
                                    fgColor : {
                                        theme : 8,
                                        tint : 0.3999755851924192,
                                        rgb : '7DCEA0'
                                    }
                                            },
                                            font : {
                                                color : {
                                                    rgb : "FFFFFF"
                                                },
                                                bold : true,
                                                sz : "14"
                                            }
                                        }
                  };
    } else if (data[R][C] == "SUCCESSFUL") {
          //You can check any conditions on text and add styles you wanted.
    }else{

         //Set other cells styles
         cell = {
                                            v : data[R][C],
                                            s : {
                                                fill : {
                                                    fgColor : {
                                                        theme : 8,
                                                        tint : 0.3999755851924192,
                                                        rgb : '08CB26'
                                                    }
                                                },
                                                font : {
                                                    color : {
                                                        rgb : "FFFFFF"
                                                    },
                                                    bold : true
                                                },
                                                border : {
                                                    bottom : {
                                                        style : "thin",
                                                        color : {
                                                            theme : 5,
                                                            tint : "-0.3",
                                                            rgb: "E8E5E4"
                                                        }
                                                    }
                                                }
                                            }
                                        };

       }

                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;
            }
        }

集集宽度

代码语言:javascript
复制
                        var wscols = [ {
                            wch : 40
                        }, {
                            wch : 40
                        }, {
                            wch : 20
                        }, {
                            wch : 20
                        }, {
                            wch : 40
                        }, {
                            wch : 40
                        }, {
                            wch : 20
                        }, {
                            wch : 20
                        }, {
                            wch : 20
                        }, {
                            wch : 20
                        }, {
                            wch : 20
                        }, {
                            wch : 40
                        }, {
                            wch : 40
                        } ];
       ws = sheet_from_array_of_arrays(myRestData);
       ws['!cols'] = wscols;
       ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42107140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档