express generate xls

var fs = require("fs");

app.get("/generateExcle",function(req,res){
	var content = [{
	    "name": "Nilesh",
	    "school": "RDTC",
	    "marks": "77"
	   },{
	    "name": "Sagar",
	    "school": "RC",
	    "marks": "99.99"
	   },{
	    "name": "Prashant",
	    "school": "Solapur",
	    "marks": "100"
	 }];
	 var data = '';
	 for(var i = 0;i < content.length;i++){
	 	data+=content[i].name+'	'+content[i].school+'	'+content[i].marks+'
';
	 }
	 fs.appendFile('F:/express/pd/uploads/score.xls',data,(err)=>{  //absolute path!!!
	 	if(err){
	 		console.log(err);
	 	}else{
	 		res.json({code:0,meg:'successfully created xls.'});
	 	}
	 });
})

create docx

var officegen = require('officegen');
var async = require('async');
app.get("/generateWord",function(req,res){
	var docx = officegen ( 'docx' );  //create docx
	var pObj = docx.createP(); //create paragrph
	pObj.options.align="center";  //set center
	pObj.addText("Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit ipsum molestiae assumenda sint cum voluptas impedit dolorem praesentium enim fuga.
");
	pObj.addLineBreak ();//line break
	pObj.addText('with color',{color:'53ff53'});
	pObj.addLineBreak ();
	pObj.addText('External link',{link:'https:google.com'});//add linked text
	pObj.startBookmark("myBookmask"); //not work
	pObj.endBookmark('myBookmask');
	//create table
	var table = [
	  [{
	    val: "No.",
	    opts: {
	      cellColWidth: 4261,
	      b:true,
	      sz: '48',
	      shd: {
	        fill: "7F7F7F",
	        themeFill: "text1",
	        "themeFillTint": "80"
	      },
	      fontFamily: "Avenir Book"
	    }
	  },{
	    val: "Title1",
	    opts: {
	      b:true,
	      color: "A00000",
	      align: "right",
	      shd: {
	        fill: "92CDDC",
	        themeFill: "text1",
	        "themeFillTint": "80"
	      }
	    }
	  },{
	    val: "Title2",
	    opts: {
	      align: "center",
	      vAlign: "center",
	      cellColWidth: 42,
	      b:true,
	      sz: '48',
	      shd: {
	        fill: "92CDDC",
	        themeFill: "text1",
	        "themeFillTint": "80"
	      }
	    }
	  }],
	  [1,'All grown-ups were once children',''],
	  [2,'there is no harm in putting off a piece of work until another day.',''],
	  [3,'But when it is a matter of baobabs, that always means a catastrophe.',''],
	  [4,'watch out for the baobabs!','END'],
	]
	var tableStyle = {
	  tableColWidth: 4261,
	  tableSize: 24,
	  tableColor: "ada",
	  tableAlign: "left",
	  tableFontFamily: "Comic Sans MS",
	  borders: true
	}
	docx.createTable (table, tableStyle);
	var out = fs.createWriteStream(path.join(__dirname,'/uploads/demo.docx'));
	out.on('error',function(err){
		console.log(err);
	});
	async.parallel([
			function(done){
				out.on('close',function(){
					console.log('created');
					done(null);
				});
				docx.generate(out);
			}
		],function(err){
			if(err){
				console.log(err,2);
			}
		})
})




Reference:
https://stackoverflow.com/questions/17450412/how-to-create-an-excel-file-with-nodejs
https://github.com/Ziv-Barber/officegen/blob/HEAD/examples/make_docx.js
https://github.com/Ziv-Barber/officegen/tree/289919183757d7416f21212d1ced3a8c2e96a2d2
https://www.npmjs.com/package/async
https://www.npmjs.com/package/officegen

原文地址:https://www.cnblogs.com/cyany/p/10025063.html