简体   繁体   English

使用 jQuery Datatables 2016 将带有换行符的值导出到 Excel 中的单个单元格中

[英]Export value with Linebreaks into single cell in Excel with jQuery Datatables 2016

I'm trying to retain linebreaks made with <br> in an HTML table when exporting it to excel with DataTables .当使用DataTables将它导出到 excel 时,我试图在 HTML 表中保留用<br>制作的换行符。
I followed their guide to replace certain things with Regex here: DataTables find and replace during export .我按照他们的指南在这里用正则表达式替换了某些东西: DataTables find and replace during export

I was able to replace things no problem.我能够更换东西没问题。 But I fail to replace the <br> with newlines that make content in the same cell retain their linebreaks.但是我无法用换行符替换<br>使同一单元格中的内容保留其换行符。

This is my JS:这是我的JS:

$( document ).ready(function() {

var fixNewLine = {
        exportOptions: {
            format: {
                body: function ( data, column, row ) {
                    // Strip $ from salary column to make it numeric
                    return column === 5 ?
// THIS WORKS:          data.replace(/test/ig, "blablabla"):
                        data.replace( /<br\s*\/?>/ig, '"'+"\r\n"+'"' ) :
                        data;
                }
            }
        }
    };


    $('#table2excel').DataTable({
        dom: 'Bfrtip',
        buttons:[
            $.extend( true, {}, fixNewLine, {
                extend: 'copyHtml5'
            } ),
            $.extend( true, {}, fixNewLine, {
                extend: 'excelHtml5'
            } ),
            $.extend( true, {}, fixNewLine, {
                extend: 'pdfHtml5'
            } )
        ]

    });
});

The problem lies in this line:问题在于这一行:

data.replace( /<br\s*\/?>/ig, '"'+"\r\n"+'"' ) :

It gets saved in excel with only a pair of " " instead of the actual line break.它保存在excel中,只有一对" "而不是实际的换行符。 Note that this also doesn't work:请注意,这也不起作用:

data.replace( /<br\s*\/?>/ig, "\r\n"):

Any advice?有什么建议吗?

There is a similar thread here: Export value with Linebreaks into single cell in Excel.这里有一个类似的线程: Export value with Linebreaks into single cell in Excel。 jQuery Datatables But it's outdated as it's a year old and there have been updates to DataTables and "TableTools" has been replaced by "Buttons". jQuery Datatables但它已经过时了,因为它已经有一年的历史了,并且 DataTables 已经更新,“TableTools”已被“Buttons”取代。

The correct answer is:正确答案是:

data.replace( /<br\s*\/?>/ig, "\n" ) :

However, you need to press the "wrap text" button when opening the excel.但是,打开excel时需要按“自动换行”按钮。 If someone knows a way to have it wrapped automatically, please let me know.如果有人知道让它自动包装的方法,请告诉我。

The replace function is built in to javascript.替换功能内置于 javascript 中。 Maybe you'd like to remove the quotations?也许您想删除引号?

data.replace( /<br\\s*\\/?>/ig, "\\r\\n")

Works for me in a javascript interpreter.在 javascript 解释器中为我工作。

Its possible that the caller of your formatting function removes newlines and replaces them with spaces格式化函数的调用者可能会删除换行符并用空格替换它们

This works for me with auto wrap text in Windows Excel 2016这对我适用于 Windows Excel 2016 中的自动换行文本

data.replace( /<br\s*\/?>/ig, "\r")  // \r, not \n

And customize buttons和自定义按钮

$('#myTable').DataTable( {
    buttons: [
        {
            extend: 'excelHtml5',
            customize: function( xlsx ) {
                var sheet = xlsx.xl.worksheets['sheet1.xml'];
                // set cell style: Wrapped text
                $('row c', sheet).attr( 's', '55' );
            }
        }
    ]
});

More info about button customization: https://datatables.net/reference/button/excelHtml5有关按钮自定义的更多信息: https : //datatables.net/reference/button/excelHtml5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM