<!-- add the shim first --> <script type="text/javascript" src="shim.min.js"></script> <!-- after the shim is referenced, add the library --> <script type="text/javascript" src="xlsx.full.min.js"></script>
let xhr = XMLHttpRequest(); req.open('GET', 'sheetjs.xlsx', true); req.onload = function(e){ 此时req.response是一个ArrayBuffer,通过new Uint8Array()创建不带符号整数的Uint8Array构造函数。 let data = new Uint8Array(req.response); let wb = XLSX.read(data, {type: "array"}); document.getElementById('xxx').innerHTML = XLSX.units.sheet_to_html(wb.Sheets[wb.SheetNames[0]],{editable: true}).replace("<table",'<table id="table" border="1"'); } req.send();
POST
1 2 3 4 5 6
let wb = XLSX.utils.table_to_book(document.getElementById('xxx')); let fd = new FormData(); let data = XLSX.write(wb, {bookType: '文件格式', type: 'array'}); fd.append('data', new File([data], '文件名'+'.'+'文件格式(csv, xlsx, xls等)')); req.open('POST', url, true); req.send(fd);
let wb = XLSX.utils.table_to_book(document.getElementById('xxx')); let fd = new FormData(); let data = XLSX.write(wb, {bookType: '文件格式', type: 'array'}); fd.append('data', new File([data], '文件名.格式名')); this.http.post('url', fd, {'responseType': 'blob'或者'arraybuffer'}).subscribe(res =>{ console.log(res); })
nodejs后端:
1 2 3 4 5 6 7 8
app.post('/upload', (req, res) =>{ res.header('Access-Control-Allow-Origin', '*'); let f = req.files[Object.keys(req.files)[0]]; // f.name 生成的excel的名字和格式 let newpath = path.join(__dirname, f.name); // 定义新文件所在的位置 fs.renameSync(f.path, newpath); re.end('xx'); })
> Importing: aoa_to_sheet converts an array of arrays of JS data to a worksheet. json_to_sheet converts an array of JS objects to a worksheet. table_to_sheet converts a DOM TABLE element to a worksheet. sheet_add_aoa adds an array of arrays of JS data to an existing worksheet. sheet_add_json adds an array of JS objects to an existing worksheet.
> Exporting: sheet_to_json converts a worksheet object to an array of JSON objects. sheet_to_csv generates delimiter-separated-values output. sheet_to_txt generates UTF16 formatted text. sheet_to_html generates HTML output. sheet_to_formulae generates a list of the formulae (with value fallbacks).
> Cell and cell address manipulation: format_cell generates the text value for a cell (using number formats). encode_row / decode_row converts between 0-indexed rows and 1-indexed rows. encode_col / decode_col converts between 0-indexed columns and column names. encode_cell / decode_cell converts cell addresses. encode_range / decode_range converts cell ranges.