回答編集履歴
1
追加
answer
CHANGED
@@ -12,4 +12,70 @@
|
|
12
12
|
workbook.SheetNames.push(n);
|
13
13
|
workbook.Sheets[n] = XLSX.utils.table_to_sheet(currentValue, wopts);
|
14
14
|
});
|
15
|
+
```
|
16
|
+
|
17
|
+
### 追記
|
18
|
+
|
19
|
+
2次元配列をHTMLTableElementに変換して、XLSXに出力できることを確認しておきました。
|
20
|
+
以下、コード。
|
21
|
+
|
22
|
+
```HTML
|
23
|
+
<!DOCTYPE html>
|
24
|
+
<html>
|
25
|
+
<head>
|
26
|
+
<meta charset="utf-8" />
|
27
|
+
<title>sample1</title>
|
28
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.2/xlsx.full.min.js"></script>
|
29
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
|
30
|
+
</head>
|
31
|
+
<body>
|
32
|
+
<input type="button" id="xlsx" value="Excel出力" onclick="func1()">
|
33
|
+
<script language="javascript" type="text/javascript">
|
34
|
+
var array1 = [ ["apple", "banana", "cherry"], [1, 2, 3] ];
|
35
|
+
|
36
|
+
function func1() {
|
37
|
+
var wopts = {
|
38
|
+
bookType: 'xlsx',
|
39
|
+
bookSST: false,
|
40
|
+
type: 'binary'
|
41
|
+
};
|
42
|
+
|
43
|
+
var workbook = {SheetNames: [], Sheets: {}};
|
44
|
+
|
45
|
+
var n = 'Sheet' + 1;
|
46
|
+
workbook.SheetNames.push(n);
|
47
|
+
// workbook.Sheets[n] = array1;
|
48
|
+
workbook.Sheets[n] = XLSX.utils.table_to_sheet(ARRAY_TO_OBJ(array1), wopts); //配列をTableElementに変換してセット
|
49
|
+
|
50
|
+
var wbout = XLSX.write(workbook, wopts);
|
51
|
+
|
52
|
+
saveAs(new Blob([s2ab(wbout)], {type: 'application/octet-stream'}), 'test.xlsx');
|
53
|
+
}
|
54
|
+
|
55
|
+
function s2ab(s) {
|
56
|
+
var buf = new ArrayBuffer(s.length);
|
57
|
+
var view = new Uint8Array(buf);
|
58
|
+
for (var i = 0; i != s.length; ++i) {
|
59
|
+
view[i] = s.charCodeAt(i) & 0xFF;
|
60
|
+
}
|
61
|
+
return buf;
|
62
|
+
}
|
63
|
+
|
64
|
+
//2次元配列をテーブルエレメントに変換して返す
|
65
|
+
function ARRAY_TO_OBJ(array){
|
66
|
+
// 要素の取得
|
67
|
+
var element = document.createElement("table"); //table生成
|
68
|
+
array.forEach(function (arr) {
|
69
|
+
var row = element.insertRow( -1 ) ; //行追加
|
70
|
+
arr.forEach(function (value) {
|
71
|
+
var cell = row.insertCell() ; //セル追加
|
72
|
+
cell.textContent = value ; //セル1に値セット
|
73
|
+
});
|
74
|
+
});
|
75
|
+
return element
|
76
|
+
}
|
77
|
+
</script>
|
78
|
+
</body>
|
79
|
+
</html>
|
80
|
+
|
15
81
|
```
|