对于shieldui导出为ex​​cel的意外的标识符错误

我需要将所有数据导出到excel表格。 我有我的网格下面的列

<table id="exportTable" class="table table-responsive table-condensed table-bordered table-hover table-striped"> <thead> <tr> <th>#</th> <th>Name</th> <th>User ID</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Vino</td> <td>vino123</td> <td>vino@gmail.com</td> </tr> </tbody> </table> 

我用下面的代码导出到Excel表单。

 <link rel="stylesheet" type="text/css" href="http://www.shieldui.com/shared/components/latest/css/light/all.min.css" /> <script type="text/javascript" src="http://www.shieldui.com/shared/components/latest/js/shieldui-all.min.js"></script> <script type="text/javascript" src="http://www.shieldui.com/shared/components/latest/js/jszip.min.js"></script> <script type="text/javascript"> jQuery(function ($) { $("#exportButton").click(function () { // parse the HTML table element having an id=exportTable var dataSource = shield.DataSource.create({ data: "#exportTable", schema: { type: "table", fields: { Name: { type: String }, User ID: { type: String }, Email: { type: String } } } }); // when parsing is done, export the data to Excel dataSource.read().then(function (data) { new shield.exp.OOXMLWorkbook({ author: "PrepBootstrap", worksheets: [ { name: "PrepBootstrap Table", rows: [ { cells: [ { style: { bold: true }, type: String, value: "Name" }, { style: { bold: true }, type: String, value: "User ID" }, { style: { bold: true }, type: String, value: "Email" } ] } ].concat($.map(data, function(item) { return { cells: [ { type: String, value: item.Name }, { type: String, value: item.User ID }, { type: String, value: item.Email } ] }; })) } ] }).saveAs({ fileName: "Processhistoryexcel" }); }); }); }); 

它的工作时,列名没有空间。 例如,我有一个列用户ID其给出错误,当我给用户ID,但它的工作时,它没有像用户ID的空间。

它给我错误的领域细胞提到的地方。

对于您的User ID键,看起来像是空白导致错误

未捕获的SyntaxError:意外的标识符

你可以通过用引号括住fields对象中的用户ID键来解决这个问题

"User ID": { type: String },

那么在你的cells数组中,你可以使用括号来访问它,因为现在在这个键里有空格

{ type: String, value: item["User ID"] },

还值得一提的是为什么你需要使用括号表示法:

但是,只能使用方括号表示法访问任何不是有效的JavaScript标识符的属性名称(例如,具有空格或连字符的属性名称,或以数字开头的属性名称)

来源: MDN使用对象