Node.js-将数据导出到Excel时单元格中的TextWrap

时间:2018-07-06 09:02:10

标签: node.js npm exceljs

我正在使用exceljs npm软件包格式化excelsheet,但是当数据太大时,我无法在单元格内进行textwrap。导出excel时,“我的描述”单元格包含大量数据,好像文本与下一个单元格重叠。

          try {
           var workbook = new excel.Workbook();
           var worksheet = workbook.addWorksheet('ABC');

           worksheet.columns = [
            { header: 'ABC', key: 'ABC', width: 15 },
            { header: 'Description', key: 'Description', width: 20 },
            { header: 'Comments', key: 'Comments', width: 20 }
        ];
        // setStyleToHeader(worksheet);
        worksheet.getRow(1).font = { name: 'Calibri', family: 4, size: 12, bold: true };
        worksheet.getRow(1).border = {
            top: { style: 'thin' },
            left: { style: 'thin' },
            bottom: { style: 'thin' },
            right: { style: 'thin' }
        };
        new sql.ConnectionPool(dbconfig).connect().then(pool => {
            return pool.request()
               .execute('sp_getAll_data')
        }).then(result => {
            debugger;
            let rows = result.recordset;
            for (var i = 0; i < rows.length; i++) {
                worksheet.addRow({
                    ABC: rows[i].ABC,
                    Description: rows[i].Description ,
                    Comments: rows[i].Comments
                });
            }
            sql.close();
            workbook.xlsx.write(res);
            debugger;
            res.setHeader('Content-Type', 'application/vnd.openxmlformats');
            res.setHeader("Content-Disposition", "attachment; filename=" + "ABC.xlsx");
            res.end(result, 'binary');
            res.status(200).json();
        }).catch(err => {
            // res.status(500).send({ message: "${err}", err })
            // sql.close();
        });
    } catch (e) {

    }

1 个答案:

答案 0 :(得分:0)

如果只想换行,可以使用cell的'alignment'属性。

worksheet.getCell('D1').alignment = { wrapText: true };

在插件页面上https://www.npmjs.com/package/exceljs#alignment