我是文件系统的新手。我想将数据写入表结构(行和列)中的文本文件中。我找到了一个模块,即 easy-table ,但它没有wordwrap,表格列标题显示曲折的方式。任何人都可以帮我完成这个结束。谢谢。
答案 0 :(得分:0)
您可以尝试此代码,这将解决您的问题,当您运行此脚本时,您的表将存储在tabledata.txt文件中
const table = require('table').table;
const fs = require('fs');
let data = [
['0A', '0B', '0C'],
['1A', '1B', '1C'],
['2A', '2B', '2C']
];
let output = table(data);
console.log(output);
fs.writeFile("tabledata.txt", output,"utf8", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
答案 1 :(得分:0)
如果您的要求是在浏览器中打开文件,那么您可以生成具有某些css属性的html文件,这将为您提供完美的格式。您可以尝试以下代码。
const table = require('table').table;
const fs = require('fs');
let data = [
['0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B', '0C'],
['0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B', '0C'],
['0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B','0A', '0B', '0C', '0A', '0B', '0C', '0A', '0C', '0A', '0B', '0C', '0A', '0B', '0C']
];
let tableOutput = table(data);
console.log(tableOutput);
let fileData = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body style="white-space:pre; font-family:monospace">
${tableOutput}
</body>
</html>`;
fs.writeFile("tabledata.html", fileData, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});