2016-03-22 47 views
0

我試圖在下載excel文件時將數字轉換爲datatables.js中的文本。如果我通常在一個數字之前手動在excel文件中手動輸入一個單引號' ..但是當我以編程的方式執行時,它不會..我在哪裏做錯了?以下是我的專欄聲明;如何將數字轉換爲Excel中的文本「datatables.js」下載

"columns": [ 
{ "data": "ID", "visible": false, "searchable": false }, 
{ 
    "data": "ProductCode", "title": 'Product Code', "visible": true, responsivePriority: 1, render: function (data, type, row) { 

     if (type === 'export') { 

      return "'" + data; 
     } 
     else { 

      return data; 
     } 
    } 
}, 
... 
] 

這是我的按鈕聲明;

// Initialize Download Button 
new $.fn.dataTable.Buttons(table, { 

    buttons: [{ 
     extend: 'excel', text: 'Download Excel', className: 'btn-sm btn-success downloadButton', exportOptions: { 
      columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 
      orthogonal: 'export' 
     } 
    }] 
}); 

它顯示了這樣'12345在下載的Excel文件,但如果我只是按F2(編輯),然後按輸入Excel的將其轉換爲文本:(。任何幫助將是非常可觀的。

回答

0

有無你試圖消除單引號?

只是這樣做return "" + data;。它應該工作。

+0

不工作:( – Mahib

+0

這是轉換的方式數字到JavaScript中的文字。也許你提到的問題與Excel本身自動檢測數據類型有關。 –

+0

我認爲這樣或可能是我正在使用的JavaScript庫我需要調試..它不是關於Excel我想如果我這樣做手動它將其轉換爲文本.. – Mahib

0

我通過在datatable.js文件addrow功能註釋掉下面的代碼解決了這個問題。

//檢測號碼 - 不帶前導零或負

// anywhere but the start 
       //if (typeof row[i] === 'number' || (
       //  row[i].match && 
       //  $.trim(row[i]).match(/^-?\d+(\.\d+)?$/) && 
       //  ! $.trim(row[i]).match(/^0\d+/)) 
       //) { 
       // cell = _createNode(rels, 'c', { 
       //  attr: { 
       //   t: 'n', 
       //   r: cellId 
       //  }, 
       //  children: [ 
       //   _createNode(rels, 'v', { text: row[i] }) 
       //  ] 
       // }); 
       //} 
       //else { 
下面

匹配的數字是功能

var addRow = function (row) { 
      currentRow = rowPos+1; 
      rowNode = _createNode(rels, "row", { attr: {r:currentRow} }); 

      for (var i=0, ien=row.length ; i<ien ; i++) { 
       // Concat both the Cell Columns as a letter and the Row of the cell. 
       var cellId = createCellPos(i) + '' + currentRow; 
       var cell; 

       if (row[i] === null || row[i] === undefined) { 
        row[i] = ''; 
       } 

       // Detect numbers - don't match numbers with leading zeros or a negative 
       // anywhere but the start 
       //if (typeof row[i] === 'number' || (
       //  row[i].match && 
       //  $.trim(row[i]).match(/^-?\d+(\.\d+)?$/) && 
       //  ! $.trim(row[i]).match(/^0\d+/)) 
       //) { 
       // cell = _createNode(rels, 'c', { 
       //  attr: { 
       //   t: 'n', 
       //   r: cellId 
       //  }, 
       //  children: [ 
       //   _createNode(rels, 'v', { text: row[i] }) 
       //  ] 
       // }); 
       //} 
       //else { 
        // Replace non standard characters for text output 
        var text = ! row[i].replace ? 
         row[i] : 
         row[i].replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, ''); 

        cell = _createNode(rels, 'c', { 
         attr: { 
          t: 'inlineStr', 
          r: cellId 
         }, 
         children:{ 
          row: _createNode(rels, 'is', { 
           children: { 
            row: _createNode(rels, 't', { 
             text: text 
            }) 
           } 
          }) 
         } 
        }); 
       //} 

       rowNode.appendChild(cell); 
      } 
      relsGet.appendChild(rowNode); 
      rowPos++; 
     }; 
相關問題