javascript - 用node将数据导出为excel,如何设置这个excel的样式?
怪我咯
怪我咯 2017-04-11 10:39:15
0
3
783

用的 excel-export(https://github.com/functionscope/Node-Excel-Export) 这个小模块。 里面可以引用一个.xml的文件来定制导出的excel表的样式。 主要想问这个.xml 怎么去编辑?或者有其他什么方法吗?
我在wps里调好excel样式然后导出为xml文件然后在node里配置上去,但是e生成的excel并不会应用这个样式。
大家有这方面经验吗?

怪我咯
怪我咯

走同样的路,发现不同的人生

membalas semua(3)
Ty80

在node_modules.0.5.1@excel-exportexample路径下面有示例的style.xml文件的

大家讲道理

由于没有发现比较好用的生成带样式的 Excel 库,于是自己写了一个,导出带样式的还算比较方便,项目地址:https://github.com/d-band/bet...

这是一个复杂点的 Demo:

const fs = require('fs');
const xlsx = require('better-xlsx');

const file = new xlsx.File();
const sheet = file.addSheet('Sheet1');
const data = [
  ['Auto', 200, 90, 'B2-C2'],
  ['Entertainment', 200, 32, 'B3-C3'],
  ['Food', 350, 205.75, 'B4-C4'],
  ['Home', 300, 250, 'B5-C5'],
  ['Medical', 100, 35, 'B6-C6'],
  ['Personal Items', 300, 80, 'B7-C7'],
  ['Travel', 500, 350, 'B8-C8'],
  ['Utilities', 200, 100, 'B9-C9'],
  ['Other', 50, 60, 'B10-C10']
];

function border(cell, top, left, bottom, right) {
  const light = 'ffded9d4';
  const dark = 'ff7e6a54';
  cell.style.border.top = 'thin';
  cell.style.border.topColor = top ? dark : light;
  cell.style.border.left = 'thin';
  cell.style.border.leftColor = left ? dark : light;
  cell.style.border.bottom = 'thin';
  cell.style.border.bottomColor = bottom ? dark : light;
  cell.style.border.right = 'thin';
  cell.style.border.rightColor = right ? dark : light;
}

function fill(cell, type) {
  type = type || 0;
  const colors = ['ffffffff', 'ffa2917d', 'ffe4e2de', 'fffff8df', 'fff1eeec'];
  // 1: header, 2: first col, 3: second col, 4: gray, 0: white
  cell.style.fill.patternType = 'solid';
  cell.style.fill.fgColor = colors[type];
  cell.style.fill.bgColor = 'ffffffff';
}

const header = sheet.addRow();
header.setHeightCM(0.8);
const headers = ['Category', 'Budget', 'Actual', 'Difference'];
for (let i = 0; i < headers.length; i++) {
  const hc = header.addCell();
  hc.value = headers[i];
  hc.style.align.v = 'center';
  if (i > 0) hc.style.align.h = 'right';
  hc.style.font.color = 'ffffffff';
  border(hc, 0, 0, 1, 0);
  fill(hc, 1);
}

const len = data.length;
for (let i = 0; i < len; i++) {
  const line = data[i];
  const row = sheet.addRow();
  row.setHeightCM(0.8);
  // Col 1
  const cell1 = row.addCell();
  cell1.value = line[0];
  cell1.style.align.v = 'center';
  if (i === 0) {
    border(cell1, 1, 0, 0, 1);
  } else if (i === len - 1) {
    border(cell1, 0, 0, 1, 1);
  } else {
    border(cell1, 0, 0, 0, 1);
  }
  fill(cell1, 2);
  // Col 2
  const cell2 = row.addCell();
  cell2.value = line[1];
  cell2.numFmt = '$#,##0.00';
  cell2.cellType = 'TypeNumeric';
  cell2.style.align.v = 'center';
  if (i === 0) {
    border(cell2, 1, 1, 0, 0);
  } else if (i === len - 1) {
    border(cell2, 0, 1, 1, 0);
  } else {
    border(cell2, 0, 1, 0, 0);
  }
  fill(cell2, 3);
  // Col 3
  const cell3 = row.addCell();
  cell3.value = line[2];
  cell3.numFmt = '$#,##0.00';
  cell3.cellType = 'TypeNumeric';
  cell3.style.align.v = 'center';
  if (i === 0) {
    border(cell3, 1, 0, 0, 0);
  } else if (i === len - 1) {
    border(cell3, 0, 0, 1, 0);
  } else {
    border(cell3, 0, 0, 0, 0);
  }
  fill(cell3, i % 2 === 0 ? 0 : 4);
  // Col 4
  const cell4 = row.addCell();
  cell4.formula = line[3];
  cell4.numFmt = '$#,##0.00';
  cell4.cellType = 'TypeFormula';
  cell4.style.align.v = 'center';
  if (i === 0) {
    border(cell4, 1, 0, 0, 0);
  } else if (i === len - 1) {
    border(cell4, 0, 0, 1, 0);
  } else {
    border(cell4, 0, 0, 0, 0);
  }
  fill(cell4, i % 2 === 0 ? 0 : 4);
}

for (let i = 0; i < 4; i++) {
  sheet.col(i).width = 20;
}

file
  .saveAs()
  .pipe(fs.createWriteStream(__dirname + '/complex.xlsx'))
  .on('finish', () => console.log('Done.'));
黄舟

用 ejsexcel 不需要用代码设置样式, Excel模板引擎的方式导出

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan