首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用excel4node在excel表格中写数组

如何用excel4node在excel表格中写数组
EN

Stack Overflow用户
提问于 2018-07-30 13:48:41
回答 3查看 5K关注 0票数 6

我正在使用Excel4node包创建一个excel文件。通过使用以下代码

代码语言:javascript
复制
// Require library
var excel = require('excel4node');

// Create a new instance of a Workbook class
var workbook = new excel.Workbook();

// Add Worksheets to the workbook
var worksheet = workbook.addWorksheet('Sheet 1');
var worksheet2 = workbook.addWorksheet('Sheet 2');

// Create a reusable style
var style = workbook.createStyle({
  font: {
    color: '#FF0800',
    size: 12
  },
  numberFormat: '$#,##0.00; ($#,##0.00); -'
});

// Set value of cell A1 to 100 as a number type styled with paramaters of style
worksheet.cell(1,1).number(100).style(style);

// Set value of cell B1 to 300 as a number type styled with paramaters of style
worksheet.cell(1,2).number(200).style(style);

// Set value of cell C1 to a formula styled with paramaters of style
worksheet.cell(1,3).formula('A1 + B1').style(style);

// Set value of cell A2 to 'string' styled with paramaters of style
worksheet.cell(2,1).string('string').style(style);

// Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
worksheet.cell(3,1).bool(true).style(style).style({font: {size: 14}});

workbook.write('Excel.xlsx');

通过使用这段代码创建一张excel表格,现在我想要的是。我想把数组写到excel表格中。

代码语言:javascript
复制
worksheet.getCell('A1').value = 's.no';

通过使用代码。它正在将数据写入工作表,但它是逐个单元格地写入数据。在excel表格中编写数组需要花费大量的时间。

代码语言:javascript
复制
    data=[{s.no:1,Name:'xxx',Age:'22'},
{s.no:2,Name:'yyy',Age:'12'},
    {s.no:3,Name:'zzz',Age:'32'}]

我想把数组写到excel表格中。

代码语言:javascript
复制
 workbook.write('Excel.xlsx',data);

我给了这样的,但这也不起作用。有人能解决这个问题吗。

EN

回答 3

Stack Overflow用户

发布于 2018-12-18 23:12:20

为什么不使用

代码语言:javascript
复制
sheet.cell(row , col ).string(`your value`).style(`your style`);
票数 0
EN

Stack Overflow用户

发布于 2020-05-02 01:46:09

我要做的是创建一个excel4node层。对…有影响的事物:

代码语言:javascript
复制
// the type arg is the cell write method you want to use such as string, number, or formula
const writeCell(wb, ws, row, col, value, type, style){
  ws.cell(row, col)[type](value).style(wb.createStyle(style))
}

const xl = require('excel4node')
const wb = xl.createWorkbook()

// TODO: track your row and column indices with arrays. let's assume that we are 
// in the outer array and the inner array or row array is called "data"
// this would write out a row of data
data.forEach((d, idx) => {
  writeCell(wb, ws, rowIdx, idx + 1, d.value, d.type, d.style)
}
票数 0
EN

Stack Overflow用户

发布于 2021-10-26 06:47:14

代码语言:javascript
复制
const array_elements = [{
    fullname: "name 1",
    game1point: 10,
    game2point: 12
  },
  {
    fullname: "name 2",
    game1point: 15,
    game2point: 17
  },
]



let startRow = 3;
for (let i = 0; i < array_elements.length; i++) {
  // FULLNAME - FIRST COLUMN
  worksheet.cell(startRow + i, 1).string(array_elements[i].fullname);

  // SECOND COLUMN
  worksheet.cell(startRow + i, 2).number(array_elements[i].game1point);


  // THIRD COLUMN
  worksheet.cell(startRow + i, 3).number(array_elements[i].game2point);


  gameCol++;

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51587669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档