You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JavaScript导出DataTable至CSV时去除HTML标签方案咨询

解决Leantime中DataTable导出CSV包含HTML标签的问题

直接修改DataTable的csvHtml5按钮配置,给导出选项添加内容格式化回调,就能提取单元格里的纯文本,剥离HTML标签。

修改后的完整配置代码

buttons: [
    {
        extend: 'csvHtml5',
        text: 'Export CSV',
        exportOptions: {
            format: {
                body: function (data, row, column, node) {
                    // 用原生DOM方法提取纯文本
                    const tempDiv = document.createElement('div');
                    tempDiv.innerHTML = data;
                    // 兼容不同浏览器的文本获取方式
                    return tempDiv.textContent || tempDiv.innerText || '';
                }
            }
        }
    }
]

关键说明

  • format.body是DataTable导出时的遍历回调,会逐个处理每个单元格的原始内容(带HTML),你可以在这里做自定义处理
  • 代码通过创建临时<div>元素,将带HTML的内容插入后提取文本,自动剥离所有HTML标签,只保留纯文本
  • 如果仅需处理部分列,可添加列索引判断(索引从0开始),比如只处理第3列:
    body: function (data, row, column, node) {
        if (column === 2) {
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = data;
            return tempDiv.textContent || tempDiv.innerText || '';
        }
        return data;
    }
    

这个方案用原生JS实现,无需额外依赖库,适配多数浏览器,能准确提取嵌套HTML里的纯文本内容。

内容的提问来源于stack exchange,提问作者Lakshmanan k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 22:35:50