NodeJs怎么實(shí)現(xiàn)下載Excel文件功能?

發(fā)布時(shí)間:2024-03-13
nodejs怎么實(shí)現(xiàn)下載excel文件功能?下面本篇文章就來給大家介紹一下。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對大家有所幫助。
【視頻教程推薦:node js教程 】
nodejs作為一門新的語言,報(bào)表功能也不是十分完善。
(1).js-xlsx : 目前 github 上 star 數(shù)量最多的處理 excel 的庫,支持解析多種格式表格xlsx / xlsm / xlsb / xls / csv,解析采用純js實(shí)現(xiàn),寫入需要依賴nodejs或者filesaver .js實(shí)現(xiàn)生成寫入excel,可以生成子表excel,功能強(qiáng)大,但上手難度稍大。不提供基礎(chǔ)設(shè)置excel表格api例單元格寬度,文檔有些亂,不適合快速上手;
https://github.com/sheetjs/js-xlsx
(2).node-xlsx : 基于node.js解析excel文件數(shù)據(jù)及生成excel文件,僅支持xlsx格式文件;
https://github.com/mgcrea/node-xlsx
(3).excel-parser : 基于node.js解析excel文件數(shù)據(jù),支持xls及xlsx格式文件,需要依賴python,太重不太實(shí)用;
https://github.com/leftshifters/excel-parser
(4).excel-export : 基于node.js將數(shù)據(jù)生成導(dǎo)出excel文件,生成文件格式為xlsx,可以設(shè)置單元格寬度,api容易上手,無法生成worksheet字表,比較單一,基本功能可以基本滿足;
https://github.com/functionscope/node-excel-export
(5).node-xlrd : 基于node.js從excel文件中提取數(shù)據(jù),僅支持xls格式文件,不支持xlsx,有點(diǎn)過時(shí),常用的都是xlsx 格式。
nodejs剛出來那幾年開發(fā)人員寫了很多node依賴庫,但是大部分現(xiàn)在處于不維護(hù)狀態(tài)。
現(xiàn)在還在持續(xù)更新的只有node-xlsx excel-export推薦使用,js-xlsx作為一個(gè)大而全的基礎(chǔ)庫(雖然現(xiàn)在也不在更行了,此庫最大的問題是api十分不友好,學(xué)習(xí)曲線高)有能力的項(xiàng)目組可以進(jìn)一步封裝,。
本篇為一個(gè)簡單的下載的demo ,就簡單使用excel-export,
var express = require('express');var router = express.router();var server = express();server.use('/api', router); var nodeexcel = require('excel-export'); const disablelayout ={layout: false}; router.get('/test', function(req, res, next) { res.json({ code:200 }) }) // disable interface layout.hbs user config layout: falserouter.get('/exportexcel/:id', function(req, res, next) { var conf ={}; conf.stylesxmlfile = "styles.xml"; conf.name = "mysheet"; conf.cols = [{ caption:'string', type:'string', beforecellwrite:function(row, celldata){ return celldata.touppercase(); }, width:300 },{ caption:'date', type:'date', beforecellwrite:function(){ var origindate = new date(date.utc(1899,11,30)); return function(row, celldata, eopt){ console.log((celldata - origindate)); if (eopt.rownum%2){ eopt.styleindex = 1; } else{ eopt.styleindex = 2; } if (celldata === null){ eopt.celltype = 'string'; return 'n/a'; } else return (celldata - origindate) / (24 * 60 * 60 * 1000); } }() },{ caption:'bool', type:'bool' },{ caption:'number', type:'number' }]; conf.rows = [ ['pi', '2013-12-5', true, 3.14], ["e", new date(2012, 4, 1), false, 2.7182], ["m&m<>'", new date(date.utc(2013, 6, 9)), false, 1.61803], ["null date", null, true, 1.414] ]; var result = nodeexcel.execute(conf); res.setheader('content-type', 'application/vnd.openxmlformats'); res.setheader("content-disposition", "attachment; filename=" encodeuricomponent("導(dǎo)出列表") ".xlsx"); res.end(result, 'binary');}); router.get('/exportmultisheetexcel/:id', function(req, res, next) { var confs = []; var conf = {}; conf.cols = [{ caption: 'string', type: 'string' }, { caption: 'date', type: 'date' }, { caption: 'bool', type: 'bool' }, { caption: 'number 2', type: 'number' }]; conf.rows = [['hahai', (new date(date.utc(2013, 4, 1))).oadate(), true, 3.14], ["e", (new date(2012, 4, 1)).oadate(), false, 2.7182], ["m&m<>'", (new date(date.utc(2013, 6, 9))).oadate(), false, 1.2], ["null", null, null
上一個(gè):苗木腹接法
下一個(gè):怎么刪除健康碼信息(鄂匯辦怎么刪除健康碼)

防爆靜電控制裝置
花毛茛的病蟲害識(shí)別和防治
白芍栽培技術(shù)
國槐苗木爛皮病
絲光鋇值
RTT032700FTP現(xiàn)貨采購,參數(shù)及規(guī)格書
草地螟防治策略
醫(yī)院污水消毒方法-臭氧高氧化消毒技術(shù)
這個(gè)時(shí)代我們?yōu)槭裁匆炔?/a>
連接手機(jī)的u盤怎樣用手機(jī)打開(手機(jī)連接u盤要打開什么開關(guān))
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos