导出的XLS文件或者以错误的编码打开,或者以一列中的所有数据打开

我有一个JS编写的函数,生成一个XLS文件,从HTML表格下载。 一切工作正常,除了如果XLS文件直接打开,一行中的所有值出现在一个列中。 但是,如果在字符集声明中省略了BOM,则值将显示在单独的列中,但会使用特殊字符混淆编码。

这是我使用的代码:

function exportTableToCSV($table, filename) { var $rows = $table.find('tr:has(td)'), // Temporary delimiter characters unlikely to be typed by keyboard // This is to avoid accidentally splitting the actual contents tmpColDelim = String.fromCharCode(11), // vertical tab character tmpRowDelim = String.fromCharCode(0), // null character // actual delimiter characters for CSV format colDelim = '"\t"', rowDelim = '"\r\n"', // Grab text from table into CSV formatted string csv = '"' + $rows.map(function (i, row) { var $row = $(row), $cols = $row.find('td'); return $cols.map(function (j, col) { var $col = $(col), text = $col.text(); return text.replace('"', '""'); // escape double quotes }).get().join(tmpColDelim); }).get().join(tmpRowDelim) .split(tmpRowDelim).join(rowDelim) .split(tmpColDelim).join(colDelim) + '"', // Data URI csvData = 'data:application/ms-excel;charset=utf-8,\ufeff' + encodeURIComponent(csv); $(this) .attr({ 'download': filename, 'href': csvData, 'target': '_blank' }); } // This must be a hyperlink $(".excelExport").on('click', function (event) { // CSV $(".timeStampUt").show(); exportTableToCSV.apply(this, [$('#xlsData'), 'oszido-<?php echo $now_date1; ?>-<?php echo $now_time1; ?>.xls']); //Change name of excel as per your need $(".timeStampUt").hide(); if($(this).hasClass("utExcelExport")) { changeValue('ClosureOfAid', 'no', 'utExcelExport-retired-isNotNull'); } // IF CSV, don't do event.preventDefault() or return false // We actually need this to be a typical hyperlink }); 

如果我尝试从数据>从文本打开它并从那里打开XLS,一切工作正常。 这对我来说很好,但是我的客户希望它可以直接打开文件。 我该如何处理?