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

如何在jQuery中导出DataTables时将所有表头转为大写?

How to Convert DataTables Headers to Uppercase on Export

Got it, let's get your DataTables headers showing up in uppercase whenever you export data (PDF, CSV, Excel, Copy). The trick is using the header callback function in DataTables' exportOptions—this lets you tweak the header text right before it's added to the exported content.

Updated Code for Your Configuration

Here's your modified DataTables setup with the necessary changes applied to all export buttons:

$('#tableFormData').DataTable({
  "stateSave": true,
  dom: 'Bfrtip',
  buttons: [
    {
      extend: 'pdfHtml5',
      title: 'Form Data.',
      orientation: 'landscape',
      pageSize: 'A4',
      pageMargins: [0, 0, 0, 0],
      margin: [0, 0, 0, 0],
      text: '<u>E</u>xport Page (PDF)',
      key: {
        key: 'e',
        altKey: false
      },
      content: [{ style: 'fullWidth' }],
      styles: {
        fullWidth: {
          fontSize: 18,
          bold: true,
          alignment: 'right',
          margin: [0, 0, 0, 0]
        }
      },
      download: 'download',
      exportOptions: {
        modifier: {
          alignment: 'center'
        },
        // Convert PDF export headers to uppercase
        header: function (data) {
          return data.toUpperCase();
        },
        columnGap: 1
      }
    },
    {
      extend: 'copy',
      exportOptions: {
        header: function (data) {
          return data.toUpperCase();
        }
      }
    },
    {
      extend: 'csv',
      exportOptions: {
        header: function (data) {
          return data.toUpperCase();
        }
      }
    },
    {
      extend: 'excel',
      exportOptions: {
        header: function (data) {
          return data.toUpperCase();
        }
      }
    }
  ]
});

Quick Breakdown

  • The header callback inside exportOptions takes the original header text (data), converts it to uppercase with toUpperCase(), and returns the modified text.
  • We added this callback to each export button to ensure all formats (PDF, Copy, CSV, Excel) have uppercase headers. If you want to avoid repeating this code for every button, you can set global export options instead:
$('#tableFormData').DataTable({
  "stateSave": true,
  dom: 'Bfrtip',
  // Global settings for all export buttons
  buttons: {
    exportOptions: {
      header: function (data) {
        return data.toUpperCase();
      }
    },
    buttons: [
      // Insert your original button configurations here (pdfHtml5, copy, csv, excel)
    ]
  }
});

This global setup will apply the uppercase header rule to every export button automatically, so you don't have to duplicate the logic.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 09:22:34