使用xlsx-style设置表格行高不生效

xlsx-style设置表格的行高不生效,原因是xlsx-style并没有提供设置行高的方法。

解决办法:

xlsx提供了设置表格行高的方法,因此可以采用修改xlsx-style源码的方式解决问题。

步骤:

找到node_modules/xlsx/xlsx.js,搜索write_ws_xml_data,复制该函数替换node_modules/xlsx-style/xlsx.js文件中的write_ws_xml_data函数。

function write_ws_xml_data(ws, opts, idx, wb) {
    var o = [], r = [], range = safe_decode_range(ws['!ref']), cell="", ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
    var dense = ws["!data"] != null;
    var params = ({r:rr}), row, height = -1;
    var date1904 = (((wb||{}).Workbook||{}).WBProps||{}).date1904;
    for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
    for(R = range.s.r; R <= range.e.r; ++R) {
        r = [];
        rr = encode_row(R);
        for(C = range.s.c; C <= range.e.c; ++C) {
            ref = cols[C] + rr;
            var _cell = dense ? (ws["!data"][R]||[])[C]: ws[ref];
            if(_cell === undefined) continue;
            if((cell = write_ws_xml_cell(_cell, ref, ws, opts, idx, wb, date1904)) != null) r.push(cell);
        }
        if(r.length > 0 || (rows && rows[R])) {
            params = ({r:rr});
            if(rows && rows[R]) {
                row = rows[R];
                if(row.hidden) params.hidden = 1;
                height = -1;
                if(row.hpx) height = px2pt(row.hpx);
                else if(row.hpt) height = row.hpt;
                if(height > -1) { params.ht = height; params.customHeight = 1; }
                if(row.level) { params.outlineLevel = row.level; }
            }
            o[o.length] = (writextag('row', r.join(""), params));
        }
    }
    if(rows) for(; R < rows.length; ++R) {
        if(rows && rows[R]) {
            params = ({r:R+1});
            row = rows[R];
            if(row.hidden) params.hidden = 1;
            height = -1;
            if (row.hpx) height = px2pt(row.hpx);
            else if (row.hpt) height = row.hpt;
            if (height > -1) { params.ht = height; params.customHeight = 1; }
            if (row.level) { params.outlineLevel = row.level; }
            o[o.length] = (writextag('row', "", params));
        }
    }
    return o.join("");
}

该函数中还依赖px2pt函数,因此回到xlsx的xlsx.js文件中,搜索px2pt,将以下内容复制到xlsx-style的xlsx.js文件中。

var DEF_PPI = 96, PPI = DEF_PPI;
function px2pt(px) { return px * 96 / PPI; }
function pt2px(pt) { return pt * PPI / 96; }

总结

可以直接将以下全部代码复制,替换xlsx-style的xlsx.js文件中的write_ws_xml_data函数。

var DEF_PPI = 96, PPI = DEF_PPI;
function px2pt(px) { return px * 96 / PPI; }
function pt2px(pt) { return pt * PPI / 96; }
function write_ws_xml_data(ws, opts, idx, wb) {
    var o = [], r = [], range = safe_decode_range(ws['!ref']), cell="", ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
    var dense = ws["!data"] != null;
    var params = ({r:rr}), row, height = -1;
    var date1904 = (((wb||{}).Workbook||{}).WBProps||{}).date1904;
    for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
    for(R = range.s.r; R <= range.e.r; ++R) {
        r = [];
        rr = encode_row(R);
        for(C = range.s.c; C <= range.e.c; ++C) {
            ref = cols[C] + rr;
            var _cell = dense ? (ws["!data"][R]||[])[C]: ws[ref];
            if(_cell === undefined) continue;
            if((cell = write_ws_xml_cell(_cell, ref, ws, opts, idx, wb, date1904)) != null) r.push(cell);
        }
        if(r.length > 0 || (rows && rows[R])) {
            params = ({r:rr});
            if(rows && rows[R]) {
                row = rows[R];
                if(row.hidden) params.hidden = 1;
                height = -1;
                if(row.hpx) height = px2pt(row.hpx);
                else if(row.hpt) height = row.hpt;
                if(height > -1) { params.ht = height; params.customHeight = 1; }
                if(row.level) { params.outlineLevel = row.level; }
            }
            o[o.length] = (writextag('row', r.join(""), params));
        }
    }
    if(rows) for(; R < rows.length; ++R) {
        if(rows && rows[R]) {
            params = ({r:R+1});
            row = rows[R];
            if(row.hidden) params.hidden = 1;
            height = -1;
            if (row.hpx) height = px2pt(row.hpx);
            else if (row.hpt) height = row.hpt;
            if (height > -1) { params.ht = height; params.customHeight = 1; }
            if (row.level) { params.outlineLevel = row.level; }
            o[o.length] = (writextag('row', "", params));
        }
    }
    return o.join("");
}

注意:修改源码仅是修改了本地的代码,当再次npm该包时,修改过的源码就会被覆盖,解决方法可以使用

patch-package。

1.安装patch-package

npm i patch-package -D

2.执行

npx patch-package package-xlsx-style

3.成功后会在项目根目录下生成一个补丁文件,然后删除node_modules,重新安装包,如果修改的源码还存在就没问题了。

如果在执行npx patch-package xlsx-style时报错

我这里是yarn安装的xlsx-style,所以删掉package.lock.json文件删掉,重新执行后就成功了。

 参考原文连接:vue2 使用xlsx + slsx-style导出excel文件,修改表格样式合并单元格修改行高_vue如何实现修改excel表的样式-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值