nodejs+jsxlsx浣跨敤鏂囦欢娴佹柟寮忎笅杞絜xcel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs+jsxlsx浣跨敤鏂囦欢娴佹柟寮忎笅杞絜xcel相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mic' title='mic'>mic   鏃犳硶   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>

鎶€鏈浘鐗? src=

濡傛灉涔熸湁绫讳技鐨勯檺鍒?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);

以上是关于nodejs+jsxlsx浣跨敤鏂囦欢娴佹柟寮忎笅杞絜xcel的主要内容,如果未能解决你的问题,请参考以下文章

nginx鎸囧畾鏂囦欢璺緞鏈変袱绉嶆柟寮弐oot鍜宎lias

odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢

K8s浣跨敤YAML鏂囦欢鍒涘缓璧勬簮

Linux(CentOS)浣跨敤瀹氭椂浠诲姟鍒犻櫎Tomcat鏃ュ織鏂囦欢

浣跨敤HiBurn鐑у綍楦胯挋.bin鏂囦欢鍒癏i3861寮€鍙戞澘

Android SearchView 鍩烘湰浣跨敤