js-xlsx
js-xlsx table_to_book bug
by Rambabu Pudi
HTML
<script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<script src="https://cdn.bootcss.com/xlsx/0.10.3/xlsx.full.min.js"></script>
<table class="table table-bordered table-hover" id="ttt">
<thead>
<tr>
<th colspan="2">aaa</th>
<th colspan="2">bbb</th>
<th colspan="2">ccc</th>
</tr>
<tr>
<th>aaa.111</th>
<th>aaa.222</th>
<th>bbb.111</th>
<th>bbb.222</th>
<th>ccc.111</th>
<th>ccc.222</th>
</tr>
</thead>
<tbody>
<tr>
<td>11</td>
<td>11.0</td>
<td>11%</td>
<td>11.0%</td>
<td>0.11</td>
<td>-11%</td>
</tr>
<tr>
<td>11.1</td>
<td>11.0</td>
<td>11.1%</td>
<td>11.01%</td>
<td>0.11</td>
<td>-11.0%</td>
</tr>
</tbody>
</table>
<button id="btnDown">下载</button>
<script>(function (view) {
"use strict";
view.URL = view.URL || view.webkitURL;
if (view.Blob && view.URL) {
try {
new Blob;
return;
} catch (e) {}
}
// Internally we use a BlobBuilder implementation to base Blob off of
// in order to support older browsers that only have BlobBuilder
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
var
get_class = function(object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
}
, FakeBlobBuilder = function BlobBuilder() {
this.data = [];
}
, FakeBlob = function Blob(data, type, encoding) {
this.data = data;
this.size = data.length;
this.type = type;
this.encoding = encoding;
}
, FBB_proto = FakeBlobBuilder.prototype
, FB_proto = FakeBlob.prototype
, FileReaderSync = view.FileReaderSync
, FileException = function(type) {
this.code = this[this.name = type];
}
, file_ex_codes = (
"NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
+ "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR...
JavaScript
document.getElementById('btnDown').onclick=function() {
function Workbook() {
this.SheetNames = [];
this.Sheets = {};
}
Workbook.prototype = {
addSheet: function(sheetName, data) {
var that = this;
that.SheetNames.push(sheetName);
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 = {
v: data[R][C]
};
if (cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({
c: C,
r: R
});
// convert
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;
}
}
function datenum(v, date1904) {
if (date1904) v += 1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
if (range.s.c < 10000000) {
ws['!ref'] = XLSX.utils.encode_range(range);
}
return ws;
}
var ws = sheet_from_array_of_arrays(data);
that.Sheets[sheetName] = ws;
},
addSheetByTable: function(sheetName, domTable) {
var that = this;
...