当从Datatables导出时,如何将文本对齐到Excel中的第一列顶部

7 浏览
0 Comments

当从Datatables导出时,如何将文本对齐到Excel中的第一列顶部

我想在Excel表格中导出数据表时,将列A的文本对齐方式设置为顶部对齐。作为一名新手程序员,非常感谢任何帮助。

这是我目前得到的结果。[图片链接](https://i.stack.imgur.com/i7dAC.png)

我希望导出后的Excel表格中的列A文本是顶部对齐的,这是我想要的结果。如下图所示,可以看到列A的文本是顶部对齐的。[图片链接](https://i.stack.imgur.com/MZCbP.png)

在MS Excel中,有一个顶部对齐的功能,如下图所示。但是我想在数据表代码中实现这个功能。[图片链接](https://i.stack.imgur.com/s4ZHX.png)

以下是我目前用于导出的代码:

{
  extend: 'excelHtml5',
  footer: true,
  text: '保存为Excel',
  pageSize: 'A4',
  title: 'shop',
  filename: 'shop',
  customize: function (xlsx) {
    var sheet = xlsx.xl.worksheets['sheet1.xml'];
    var style = xlsx.xl['styles.xml'];
    var tagName = style.getElementsByTagName('sz');
    $('row c[r^="A"]', sheet).attr('s', '2');
    $('row c[r^="B"]', sheet).attr('s', '55');
    $('row[r=2] c', sheet).attr('s', '32');
    $('row[r=1] c', sheet).attr('s', '51');
    $('xf', style).find("alignment[horizontal='center']").attr("wrapText", "1");
    $('row', sheet).first().attr('ht', '40').attr('customHeight', "1");
    var col = $('col', sheet);
    $(col[0]).attr('width', 8);
    $(col[1]).attr('width', 25);
    $(col[2]).attr('width', 8);
    $(col[3]).attr('width', 9);
    $(col[4]).attr('width', 7);
    $(col[5]).attr('width', 6);
    $(col[6]).attr('width', 7);
    $(col[7]).attr('width', 8);
    $(col[8]).attr('width', 8);
    $('row*', sheet).each(function (index) {
      if (index > 0) {
        $(this).attr('ht', 32);
        $(this).attr('customHeight', 1);
      }
    });
    var ranges = buildRanges(sheet);
    ranges.push("A1:I1");
    var mergeCellsHtml = '';
    ranges.forEach(function (range) {
      mergeCellsHtml = mergeCellsHtml + '';
    })
    mergeCellsHtml = mergeCellsHtml + '';
    $('sheetData', sheet).after(mergeCellsHtml);
    $('mergeCells', sheet).last().remove();
  },
  exportOptions: {
    columns: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    rows: function (idx, data, node) {
      return data[6] + data[7] > 0 ? true : false;
    }
  }
}

构建范围的函数如下:

function buildRanges(sheet) {
  let prevCat = ''; // 上一个分类
  let currCat = ''; // 当前分类
  let currCellRef = ''; // 当前单元格引用
  let rows = $('row', sheet);
  let startRange = '';
  let endRange = '';
  let ranges = [];
  rows.each(function (i) {
    if (i > 0 && i < rows.length) {
      let cols = $('c', $(this));
      cols.each(function (j) {
        if (j == 0) {
          currCat = $(this).text();
          currCellRef = $(this).attr('r');
          if (currCat !== prevCat) {
            if (i == 0) {
              startRange = currCellRef;
              endRange = currCellRef;
              prevCat = currCat;
            } else {
              if (endRange !== startRange) {
                ranges.push(startRange + ':' + endRange);
              }
              startRange = currCellRef;
              endRange = currCellRef;
              prevCat = currCat;
            }
          } else {
            endRange = currCellRef;
          }
        }
      });
      if (i == rows.length - 1 && endRange !== startRange) {
        ranges.push(startRange + ':' + endRange);
      }
    }
  });
  return ranges;
}

0