Javascript 如何在excel中将数字转换为文本;datatables.js“;下载

Javascript 如何在excel中将数字转换为文本;datatables.js“;下载,javascript,jquery,excel,datatables,Javascript,Jquery,Excel,Datatables,下载excel文件时,我正在尝试将一个数字转换为datatables.js中的文本。如果我通常在一个数字之前手动在excel文件中输入一个单引号,,那么它就起作用了。。但当我以编程方式这样做时,它不会。。我哪里做错了?以下是我的专栏声明 "columns": [ { "data": "ID", "visible": false, "searchable": false }, { "data": "ProductCode", "title": 'Product Code', "visibl

下载excel文件时,我正在尝试将一个数字转换为
datatables.js
中的文本。如果我通常在一个数字之前手动在excel文件中输入一个单引号
,那么它就起作用了。。但当我以编程方式这样做时,它不会。。我哪里做错了?以下是我的专栏声明

"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'
        }
    }]
});
这是我的按钮声明

"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'
        }
    }]
});

它在下载的excel文件中显示如下
'12345
,但如果我只按F2键(编辑)并按enter键,excel会将其转换为文本:(。任何帮助都会非常有用。

您是否尝试过删除单引号


只要这样做就可以了。

我在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++;
        };
var addRow=函数(行){
currentRow=rowPos+1;
rowNode=_createNode(rels,“row”,{attr:{r:currentRow}});

对于(var i=0,ien=row.length;这是一种在javascript中将数字转换为文本的方法。您提到的问题可能与excel本身自动检测数据类型有关。我认为是这样,或者可能是我正在使用的javascript库,我需要对此进行调试。我想这与excel无关,因为如果我手动执行此操作,它会将其转换为文本。。