sheetjs style

sheetjs导出excel,设置样式

https://github.com/gitbrent/xlsx-js-style

导入

<script src="./xlsx.bundle.js"></script>

demo

function download() {
    const wb = XLSX.utils.book_new()
    var ws = XLSX.utils.json_to_sheet([
    ], {header:["7月新华公司报结申请单"]});
    XLSX.utils.sheet_add_json(ws, [
      {A: '报结单号', D: '报结日期', E: '2022.07.22', I: 'MES_ID:', J: 'BJ-2022070021',
      L: '结算单位:', N: '江西新华新材料科技股份有限公司'}
    ], {skipHeader: true, origin: "A2", header: getA_Z()});
    ws['!merges'] = [XLSX.utils.decode_range('A1:U1')];
    ws['!merges'].push(XLSX.utils.decode_range('A2:B2'));
    ws['A1'].s = ws['A2']['s'] = {
      font: {
        name: '宋体',
        sz: 24,
        bold: true,
        color: { rgb: "FFFFAA00" }
      },
    }

    // 3、把工作表放到工作簿中
    XLSX.utils.book_append_sheet(wb, ws, 'sheet')
    console.log(ws)
    // 4、生成数据保存
    XLSX.writeFile(wb,"测试表格.xlsx",{
      cellStyles: true,
      bookType: 'xlsx'
    });
  }

  function getA_Z() {
    let a = 'A'.charCodeAt(0);
    let z = 'Z'.charCodeAt(0)
    let arr = [];
    for (; a <= z; a++) {
      arr.push(String.fromCharCode(a))
    }
    return arr;
  }

单元格宽度

ws['!cols'] = [
      { wpx: 40 }, { wpx: 40 }, { wpx: 40 }, { wpx: 60 }, { wpx: 100 } }
  ]
posted @ 2022-08-25 10:59  fight139  阅读(471)  评论(0编辑  收藏  举报