javascript - 使用 excel.js 模块 + Node 在列标题前添加行

标签 javascript node.js excel exceljs

我正在尝试使用 node.js 中的 excel.js 模块创建 xslx 文件。我能够创建列并添加其值。但是,我需要在列标题之前插入一些行,在那里我可以有一些描述。我该怎么做?

I need something like this

任何帮助将不胜感激

我试过的代码是

     var worksheet = workbook.getWorksheet(1);
    worksheet.mergeCells('A1:B1');
    var row = worksheet.getRow(1);
    row.getCell(1).value =' Hello, World!'; 
   // ... merged cells are linked 
    worksheet.getCell('A1').value = 'PI';
    worksheet.columns = [
        {header: 'Id', key: 'id', width: 10},
        {header: 'Name', key: 'name', width: 32},
        {header: 'complexity', key: 'complexity', width: 10},
        {header: 'hours', key: 'hours', width: 10},
        {header: 'cost', key: 'cost', width: 10}
    ];
     worksheet.addRow({name:'m', complexity: 'd', hours:5, cost: 7});

最佳答案

https://github.com/guyonroche/exceljs/issues/433 找到答案

> @rihabbs:我需要像这个例子那样做 [![在此处输入图片描述][1]][1] [1]: /image/9oRE4.png

> @mfahmirukman:

/*TITLE*/
sheet.mergeCells('C1', 'J2');
sheet.getCell('C1').value = 'Client List'

/*Column headers*/
sheet.getRow(9).values = ['idClient', 'Name', 'Tel', 'Adresse'];

/*Define your column keys because this is what you use to insert your data according to your columns, they're column A, B, C, D respectively being idClient, Name, Tel, and Adresse.
So, it's pretty straight forward */
sheet.columns = [
  { key: 'idClient'},
  { key: 'name'},
  { key: 'tel'},
  { key: 'adresse'}
]

/*Let's say you stored your data in an array called arrData. Let's say that your arrData looks like this */
arrData = [{
  idClient: 1,
  name: 'Rihabbs',
  tel: '0123456789',
  adresse: 'Home sweet home'
},
{
  idClient: 2,
  name: 'mfahmirukman',
  tel: '0123456789',
  adresse: 'Indonesia'
}
]
/* Now we use the keys we defined earlier to insert your data by iterating through arrData and calling worksheet.addRow()
*/
arrData.forEach(function(item, index) {
  sheet.addRow({
     idClient: item.idClient,
     name: item.name,
     tel: item.tel,
     adresse: item.adresse
  })
})

关于javascript - 使用 excel.js 模块 + Node 在列标题前添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37830513/

相关文章:

excel - 从图表图例中删除系列并在 excel vba 中设置轴范围

javascript - 如何使用 jQuery 从多个具有相同类名的元素中选择一个特定元素 (div)?

javascript - 如何使用带有 vue js 的计算和 Prop 将事件样式应用于菜单

javascript - EJS在2个页面之间共享数据

c++ - 使用 OLE 以编程方式保存 Excel 文件

vba - 如何使用不同列上的另一个自动过滤器循环过滤范围?

javascript - 使用 Jquery,如何使用从 url 返回的竞赛更新 HTML 表的一行?

javascript - 如何使用 Angularjs ionic 缓存图像?

javascript - 通过 AWS IOT sdk javascript 配置 AWS IOT 设备

node.js - 在 DigitalOcean 上托管多个 meteor 应用程序