NodejsテキストファイルとExcelファイルの作成
2411 ワード
依存モジュールfsとexcelの導入が必要
fsファイルモジュールの作成
json 2 excel json回転ecxel
これは単純なDemoで、nodejsサーバが起動するとoutputが生成されます.txtとj 2 x.xlsx
インプリメンテーションコード
コマンドの実行
npm install fs
npm install json2excel
fsファイルモジュールの作成
json 2 excel json回転ecxel
これは単純なDemoで、nodejsサーバが起動するとoutputが生成されます.txtとj 2 x.xlsx
インプリメンテーションコード
const http = require('http');
var fs = require('fs');
var path = require('path');
var jexcel=require('json2excel');
var sheet1={
header: {
'author': 'Author Name',
'title': 'Article Title'
},
items: [
{
author:'john',
title:'how to use this'
},
{
author:'Bob',
title:'so Easy'
}
],
sheetName: 'sheet1' //
};
var data = {
sheets: [sheet1],
filepath: path.join(path.resolve(__dirname, '../..'),"xls/j2x.xlsx")
}
const hostname = '0.0.0.0';
const port = 3003;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World
');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
fs.writeFile(path.join(path.resolve(__dirname, '../..'),"xls/output.txt"), "Hello World!", function(err) {
if(err) {
return console.log(err);
}
console.log("File saved successfully!");
});
jexcel.j2e(data,function(err){
console.log('finish')
});
});
コマンドの実行
node /Users/admin/Downloads/test/main/engin/example.js
__dirname
path.resolve(__dirname, '../..')
path.join(__dirname,"xls/output.txt") ,