作者:手机用户2502937923 | 来源:互联网 | 2023-10-11 19:06
篇首语:本文由编程笔记#小编为大家整理,主要介绍了nodejs+jsxlsx浣跨敤鏂囦欢娴佹柟寮忎笅杞絜xcel相关的知识,希望对你有一定的参考价值。鏍囩锛?ahre
篇首语:本文由编程笔记#小编为大家整理,主要介绍了nodejs+jsxlsx浣跨敤鏂囦欢娴佹柟寮忎笅杞絜xcel相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/mic' title='mic'>mic 鏃犳硶 鏂囦欢娴?/a> lob 绔欑偣 const content 璐︽埛 ati
鏈枃浠嬬粛鐢?Node.js 涓殑渚濊禆搴撴潵澶勭悊 Excel 鏂囦欢锛屼富瑕佸埄鐢╦s-xlsx搴撴潵澶勭悊 Excel 鏂囦欢銆?/p>
闂锛?/p>
銆€銆€1.鐢变簬鍏徃姝e紡鏈嶅姟鍣ㄧ殑鏉冮檺鏀剁殑寰堢揣锛岀珯鐐圭殑linux璐︽埛娌℃湁鍐欐潈闄愶紝鏃犳硶鍒╃敤鍐欐枃浠剁殑鏂瑰紡瀵煎嚭excel锛屼簬鏄煡璇㈣祫鏂欓噰鐢ㄦ枃浠舵祦鐨勬柟寮忓鍑恒€?/p>
銆€銆€2.鍥犱负涓嶇啛鎮塶odejs 瀵艰嚧鏃犻檺韪╁潙銆?/p>
js-xlsx github鍦板潃 https://github.com/SheetJS/sheetjs
鍥犱负闇€瑕佸鍑篹xcel鏂囦欢 鍒欐牴鎹甮ithub涓婄殑鏂囨。鏄剧ず鍙互閲囩敤杩欎簺鏂规硶 鏈€绠€鍗曠殑濡傛灉娌℃湁绫讳技鏉冮檺鐨勫叾浠栭檺鍒?閲囩敤writeFile浠ュ啓鏂囦欢鐨勬柟寮忓鍑?/p>
濡傛灉涔熸湁绫讳技鐨勯檺鍒?writeFile杩欑鏂瑰紡灏变笉鏄渶浣抽€夋嫨锛屾煡璇㈣祫鏂欒繃鍚庯紝鍑嗗浠ユ枃浠舵祦鐨勬柟寮忚繘琛屽鐞?/p>
jsxlsx 涔熸彁渚涗簡寰堢畝鍗曠殑澶勭悊 鏍规嵁鏂囨。write鏂瑰紡灏卞彲浠ユ弧瓒虫垜鐨勯渶瑕?鍦╳rite_opts涓缃垜浠渶瑕佺殑绫诲瀷锛岃濡傦細
let buf = Excel.write(wb, {
type: 鈥榖uffer鈥?
bookType: 鈥榵lsx鈥?br />})
鍦ㄦ涔嬪墠鎴戜滑杩橀渶瑕佸皢鎴戜滑瑕佸鍑虹殑鏁版嵁瑁呰浇濡傚伐浣滆〃涓細
let ws = Excel.utils.aoa_to_sheet(data); //data灏辨槸鎴戜滑鐨勬暟鎹?br />let wb = Excel.utils.book_new()锛?br />Excel.utils.book_append_sheet(wb, ws, 鈥榅X鏁版嵁鈥?锛?br />鍦ㄧ粡杩囪繖浜涙楠や箣鍚?鎴戜滑瀵煎嚭鐨勫伐浣滃熀鏈笂宸茬粡瀹屾垚浜嗭紝浣嗘槸鍒繕浜嗗洜涓烘槸閲囩敤鏂囦欢娴佺殑鏂瑰紡鐩稿簲鐨勮缃篃瑕佸姞涓?/pre>
ctx.set(鈥楥ontent-disposition鈥? 鈥榓ttachment; filename=鈥?+ fileName);
ctx.type = "xlsx"
ctx.body = buf
鍓嶇鍦ㄦ帴鏀跺埌鍚庣杩斿洖鐨勬枃浠舵祦鏃讹紝鍒╃敤Blob杩涜鐩稿簲鐨勫鐞嗭細
const data = res.data;
const filename = res.headers[鈥榗ontent-disposition鈥榏.split(鈥?鈥?[1]
// console.log(res.headers)
const url = window.URL.createObjectURL(
new Blob([data], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
})
);
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);