Remove optional hyphens from CSV export for Excel

Remove optional hyphens from CSV export for Excel

ryan_holtonryan_holton Posts: 1Questions: 1Answers: 0

Hi, I'm using DataTables 1.12.1 in my project, along with the ability to export via a CSV. I've modified the code to be able to include child row details as part of my CSV and exporting works well, my issue I'm facing is when importing the CSV into Excel, I'm seeing optional hyphens at the start of currencies.

These optional hyphens aren't part of the CSV contents, and when importing into a cloud solution like Google sheets, everything looks fine.

I'm thinking of switching to the Export as Excel option in DataTables because of this, but wondered am I missing something that could resolve this issue by removing the optional hyphen?

Scripts used

// jQuery
echo $this->Html->script('');

// DataTables
echo $this->Html->script('');
echo $this->Html->script('');
echo $this->Html->script('');

JS for exporting CSV:

var table = $('#commissions-table').DataTable({
  dom: 'Blrtip',
  buttons: [
      extend: 'csv',
      text: 'Export to CSV',
      filename: 'payout',
      customize: function (csv) {
        try {
          let formattedCsvAsString = ''
          const plainCsv = []

          // table doesn't seem to exist
          if (!table) {
            throw "Table can't be found."

          // columns that we can remove
          const columnsToAdd = getCommissionCsvExportRowFormats(ROLE_ID)

          // get headers and table rows
          let tableHeaders = []

          // remove headers for certain users
          for (const [headerIndex, header] of table.buttons.exportData().header.entries()) {
            if (ROLE_ID != 2) {
              if (header == 'Revenue' || header == 'Profit') {


          // table data doesn't exist
          if (!table.rows() || !table.rows().data().toArray()) {
            throw "No data found in table."

          const tableRows = table.rows().data().toArray()


          // add user rows
          for (const [rowIndex, row] of tableRows.entries()) {
            let rowToAdd = {
              username: row.username

            for (const [rowColumnIndex, column] of columnsToAdd.entries()) {
              if (row[column.key] == null || column.is_disabled) {

              rowToAdd[column.key] = getFormattedValue(row[column.key], column.type)


            if (!row.affiliates) {

            // add affiliate rows to CSV
            for (const [childRowIndex, childRow] of row.affiliates.entries()) {
              let childRowToAdd = {
                affiliate: childRow.affiliate

              for (const [childColumnIndex, childColumn] of columnsToAdd.entries()) {
                if (childRow[childColumn.key] == null || childColumn.is_disabled) {

                childRowToAdd[childColumn.key] = getFormattedValue(childRow[childColumn.key], childColumn.type)


          // format the formatted value
          for (const [index, csvRow] of plainCsv.entries()) {
            const formatted = => `"${row}"`)
            formattedCsvAsString += `${formatted.join(',')}\r\n`

          if (!formattedCsvAsString) {
            throw "No data set."

          // return csv
          return formattedCsvAsString
        } catch (err) {
          if (err.message) {
            console.warn(`Unable to convert CSV (${err.message})`)
          } else if (typeof err === 'string') {
            console.warn(`Unable to convert CSV (${err})`)

Here's what the optional hyphen looks like, how can I resolve this?

Screenshot 2022-09-30 at 16 56 06


  • kthorngrenkthorngren Posts: 19,462Questions: 26Answers: 4,610

    Since there is no hyphen in the CSV but Excel is displaying a hyphen then I would first take a look at the cell to see what format Excel is applying to the cell.

    Does this happen only with the child rows?

    Please provide a link to your page or a test case so we can take a look at the source data and what your CSV customize function is doing.


Sign In or Register to comment.