Angular xlsx - 多个 json 到 sheet
Angular xlsx - multiple json to sheet
我使用 js-xlsx 和函数 utils.json_to_sheet.
导出到 excel
问题是我有另一个 json 对象要导出到同一个 sheet,就在前一个对象的下方。
XLSX.utils.json_to_sheet(outputData);
试图在文档等中找到有关它的任何信息,但似乎没有人遇到过这个问题。不久前有人问了一个类似的问题,不幸的是没有答案 - How to add multiple table with different data-set to the same sheet in exceljs 。有什么想法吗?
我最终通过邮件与 xlsx 支持人员交谈,他们给了我答案。
有一个函数叫做 sheet_add_json。
简单示例:
XLSX.utils.sheet_add_json(sheet, secondJson, { skipHeader: true, origin: "A" +
(firstJson.length + 3) });
更多示例:
Consider the worksheet:
XXX| A | B | C | D | E | F | G |
---+---+---+---+---+---+---+---+
1 | S | h | e | e | t | J | S |
2 | 1 | 2 | | | 5 | 6 | 7 |
3 | 2 | 3 | | | 6 | 7 | 8 |
4 | 3 | 4 | | | 7 | 8 | 9 |
5 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
This worksheet can be built up in the order A1:G1, A2:B4, E2:G4, A5:G5:
/* Initial row */
var ws = XLSX.utils.json_to_sheet([
{ A: "S", B: "h", C: "e", D: "e", E: "t", F: "J", G: "S" }
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true});
/* Write data starting at A2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 1, B: 2 }, { A: 2, B: 3 }, { A: 3, B: 4 }
], {skipHeader: true, origin: "A2"});
/* Write data starting at E2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 5, B: 6, C: 7 }, { A: 6, B: 7, C: 8 }, { A: 7, B: 8, C: 9 }
], {skipHeader: true, origin: { r: 1, c: 4 }, header: [ "A", "B", "C" ]});
/* Append row */
XLSX.utils.sheet_add_json(ws, [
{ A: 4, B: 5, C: 6, D: 7, E: 8, F: 9, G: 0 }
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true, origin: -1});
首先,创建一本新书
让 wb = XLSX.utils.book_new()
新建一个sheet
让 ws = XLSX.utils.json_to_sheet(dataarray[i]); //dataarray[i] 应该 return objects array
将sheet附加到您的书
XLSX.utils.book_append_sheet(wb, ws, 'SheetName');
代码示例
const arr = [
[{ FirstColumn: "Some data", SecondColumn: "Page 1 with 1 data" }],
[
{ FirstColumn: "Some data-1", SecondColumn: "Page 2 with 2 data-1" },
{ FirstColumn: "Some data-2", SecondColumn: "Page 2 with 2 data-2" }
],
[
{ FirstColumn: "Some data-1", SecondColumn: "Page 3 with 3 data-1" },
{ FirstColumn: "Some data-2", SecondColumn: "Page 3 with 3 data-2" },
{ FirstColumn: "Some data-3", SecondColumn: "Page 3 with 3 data-3" }
]
];
const fileName = "filename.xlsx";
const sheetName = ["sheet1", "sheet2", "sheet3"];
let wb = XLSX.utils.book_new();
for (var i = 0; i < sheetName.length; i++) {
let ws = XLSX.utils.json_to_sheet(arr[i]);
XLSX.utils.book_append_sheet(wb, ws, sheetName[i]);
}
XLSX.writeFile(wb, fileName);
我使用 js-xlsx 和函数 utils.json_to_sheet.
导出到 excel问题是我有另一个 json 对象要导出到同一个 sheet,就在前一个对象的下方。
XLSX.utils.json_to_sheet(outputData);
试图在文档等中找到有关它的任何信息,但似乎没有人遇到过这个问题。不久前有人问了一个类似的问题,不幸的是没有答案 - How to add multiple table with different data-set to the same sheet in exceljs 。有什么想法吗?
我最终通过邮件与 xlsx 支持人员交谈,他们给了我答案。 有一个函数叫做 sheet_add_json。
简单示例:
XLSX.utils.sheet_add_json(sheet, secondJson, { skipHeader: true, origin: "A" + (firstJson.length + 3) });
更多示例:
Consider the worksheet:
XXX| A | B | C | D | E | F | G |
---+---+---+---+---+---+---+---+
1 | S | h | e | e | t | J | S |
2 | 1 | 2 | | | 5 | 6 | 7 |
3 | 2 | 3 | | | 6 | 7 | 8 |
4 | 3 | 4 | | | 7 | 8 | 9 |
5 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
This worksheet can be built up in the order A1:G1, A2:B4, E2:G4, A5:G5:
/* Initial row */
var ws = XLSX.utils.json_to_sheet([
{ A: "S", B: "h", C: "e", D: "e", E: "t", F: "J", G: "S" }
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true});
/* Write data starting at A2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 1, B: 2 }, { A: 2, B: 3 }, { A: 3, B: 4 }
], {skipHeader: true, origin: "A2"});
/* Write data starting at E2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 5, B: 6, C: 7 }, { A: 6, B: 7, C: 8 }, { A: 7, B: 8, C: 9 }
], {skipHeader: true, origin: { r: 1, c: 4 }, header: [ "A", "B", "C" ]});
/* Append row */
XLSX.utils.sheet_add_json(ws, [
{ A: 4, B: 5, C: 6, D: 7, E: 8, F: 9, G: 0 }
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true, origin: -1});
首先,创建一本新书
让 wb = XLSX.utils.book_new()
新建一个sheet
让 ws = XLSX.utils.json_to_sheet(dataarray[i]); //dataarray[i] 应该 return objects array
将sheet附加到您的书
XLSX.utils.book_append_sheet(wb, ws, 'SheetName');
代码示例
const arr = [
[{ FirstColumn: "Some data", SecondColumn: "Page 1 with 1 data" }],
[
{ FirstColumn: "Some data-1", SecondColumn: "Page 2 with 2 data-1" },
{ FirstColumn: "Some data-2", SecondColumn: "Page 2 with 2 data-2" }
],
[
{ FirstColumn: "Some data-1", SecondColumn: "Page 3 with 3 data-1" },
{ FirstColumn: "Some data-2", SecondColumn: "Page 3 with 3 data-2" },
{ FirstColumn: "Some data-3", SecondColumn: "Page 3 with 3 data-3" }
]
];
const fileName = "filename.xlsx";
const sheetName = ["sheet1", "sheet2", "sheet3"];
let wb = XLSX.utils.book_new();
for (var i = 0; i < sheetName.length; i++) {
let ws = XLSX.utils.json_to_sheet(arr[i]);
XLSX.utils.book_append_sheet(wb, ws, sheetName[i]);
}
XLSX.writeFile(wb, fileName);