经调研,原因如下:
1、sheet1中有一张表,名字叫“ Table1 ”,sheet2有一张表,名字也叫“ Table1 ”。
2、SpreadJS 允许不同的表可以具有相同的表名,但是Excel不允许。
Excel会将第二个同名table的displayName重命名为“ Table1_1 ”
有个解决方案您可以参考下:
在执行workbook.fromJSON前,执行下方代码:
- // usage
- repairTableNames(spreadJSON);
- spread.fromJSON(spreadJSON);
- function repairTableNames(json) {
- // this manager for rename the tables with table id which has the same table name
- var nameManager = {};
- var sheets = json.sheets;
- for (var sheetName in sheets) {
- var sheet = sheets[sheetName];
- var tables = sheet.tables;
- if (tables) {
- for (var i = 0; i < tables.length; i ++) {
- var table = tables[i];
- var tableName = table.name;
- if (typeof nameManager[tableName] === "number") {
- // In this branch, this table has same name with previous table
- // Just increase the table id, then rename with this id
- nameManager[tableName] ++;
- table.name = tableName + "_" + nameManager[tableName];
- } else {
- // In this branch, this table name never appeared in name manager
- nameManager[tableName] = 0;
- }
- }
- }
- }
- }
复制代码
|