You need to enable JavaScript to run this app. tableExport.jquery.plugin配合bootstrap-table导出Excel时如何设置单元格格式
优惠活动
大模型
产品
解决方案
定价
更多

tableExport.jquery.plugin配合bootstrap-table导出Excel时如何设置单元格格式

问题梳理

使用bootstrap-table通过JS接口动态渲染表格,搭配tableExport.jquery.plugin实现Excel导出。需要给特定列的所有<td>元素自动挂载data-tableexport-msonumberformat="0"属性,保证导出时该列按整数格式解析。目前已完成依赖引入和基础导出按钮逻辑,不确定如何在动态渲染的表格上批量添加指定HTML属性。

可行实现方案

直接利用bootstrap-table列配置内置的attributes方法,批量给指定列的单元格添加自定义属性,无需手动操作DOM。

修改步骤

  1. 给需要加属性的itemNo列新增attributes配置项,返回目标属性对象:
    {
        field: 'itemNo',
        title: 'Item No',
        sortable: true,
        // 给当前列所有td自动加指定data属性
        attributes: function(value, row, index) {
            return {
                'data-tableexport-msonumberformat': '0'
            }
        }
    }
    
  2. 修正现有代码的语法错误:
    • 错误的css引入标签:
      <!-- 原错误代码 -->
      <link href="/webjars/bootstrap-table/1.16.0/bootstrap-table.css" rel="stylesheet"></script>
      
      改为:
      <link href="/webjars/bootstrap-table/1.16.0/bootstrap-table.css" rel="stylesheet">
      
    • 补全bootstrapTable配置的缺失闭合标签:
      $('#table').bootstrapTable({
          url: '/getTableData',
          columns: [
              {
                  field: 'orderNo',
                  title: 'Order No',
                  sortable: true
              },
              {
                  field: 'itemNo',
                  title: 'Item No',
                  sortable: true,
                  attributes: function(value, row, index) {
                      return {
                          'data-tableexport-msonumberformat': '0'
                      }
                  }
              }
          ] // 补全columns数组的闭合
      }) // 补全配置对象的闭合
      

注意事项

  • 你之前尝试的tableExport: {msoNumberFormat: '0'}写法,在当前使用的1.16.0版本的bootstrap-table导出扩展中无原生支持,不会生效,上述attributes方法是该版本下最稳定的实现方式。
  • 该方式生成的data属性会被tableExport.jquery.plugin自动识别,不需要额外调整导出按钮的逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.13 05:45:09