[英]Export value with Linebreaks into single cell in Excel. jQuery Datatables
I am successfully exporting HTML tables from a web application to excel using jQuery DataTables. 我已成功使用jQuery DataTables将Web应用程序中的HTML表导出到excel。 However one particular column has values containg line breaks and tabs.
但是,一个特定的列的值包含换行符和制表符。 I have managed to display the data correctly on the HTML table by replacing new lines (\\n) and tabs (\\t) with
<br>
and (x5) respectively. 通过分别用
<br>
和&nbsp;(x5)替换新行(\\ n)和制表符(\\ t),我设法在HTML表格上正确显示了数据。
The issue is when exporting to excel i need to have the line breaks back in but keep all the value in one cell. 问题是当导出到excel时,我需要重新插入行,但将所有值保留在一个单元格中。
here is my jquery code: 这是我的jQuery代码:
$('#papercliptable').dataTable({
"sDom": 'T<"clear">lfrtip',
"tableTools": {
"aButtons": [{
"sExtends": "xls",
"sButtonText": "Excel",
"fnCellRender": function (sValue, iColumn, nTr, iDataIndex) {
console.log("sValue = " + sValue);
console.log("iColumn = " + iColumn);
return sValue.replace(/<br\s*\/?>/ig, "\r\n");
},
"sNewLine": "\r\n"
}, {
"sExtends": "print",
"sMessage": "Metrics"
}]
}
});
It does not seem to work for me. 它似乎对我不起作用。 All value goes to single cell but not with new line characters.
所有值都转到单个单元格,但不包括换行符。
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks
谢谢
Tried using: 尝试使用:
return sValue.replace(/<br\s*\/?>/ig, "\x0B");
produces the following 产生以下
Let's have a complete example. 让我们举一个完整的例子。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/tabletools/2.2.4/css/dataTables.tableTools.css">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/tabletools/2.2.4/js/dataTables.tableTools.min.js"></script>
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#papercliptable').DataTable( {
dom: 'T<"clear">lfrtip',
tableTools: {
"sSwfPath": "/copy_csv_xls_pdf.swf",
"aButtons": [{
"sExtends": "xls",
"sFileName": "test.csv",
"fnCellRender": function (sValue, iColumn, nTr, iDataIndex) {
console.log("sValue = " + sValue);
console.log("iColumn = " + iColumn);
re = /<br\s*\/?>/i;
if (re.test(sValue)) {
return '"' + sValue.replace(/<br\s*\/?>/ig, "\n") + '"';
} else {
return sValue;
}
}
}]
}
});
});
</script>
</head>
<body>
<table id="papercliptable">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<td>12345</td>
<td>Value1</td>
<td>Value2</td>
<td>This<br/>is<br/>a<br/>test.</td>
</tr>
</tbody>
</table>
</body>
</html>
Note, you have to have the copy_csv_xls_pdf.swf
within your own domain. 请注意,您必须在自己的域内拥有
copy_csv_xls_pdf.swf
。 You can download it from: https://cdn.datatables.net/tabletools/2.2.4/ 您可以从以下网址下载: https : //cdn.datatables.net/tabletools/2.2.4/
This works for me and produces: 这对我有用,并产生:
CSV: CSV:
A B C D
12345 Value1 Value2 "This
is
a
test."
Note, the spaces between the columns are horizontal tabs "\\t"
0x09
. 注意,列之间的空格是水平制表符
"\\t"
0x09
。
Excel: Excel:
Note, this is the result in Excel if the *.csv
is opened via File - Open
. 注意,如果通过
File - Open
*.csv
,则这是Excel中的结果。 The Text Import Wizard can't handle line breaks within cells in correct manner. 文本导入向导不能正确处理单元格内的换行符。
If the content contains new line characters you need to delimit it within double quotes. 如果内容包含换行符,则需要在双引号中定界。 So use
所以用
return '"' + sValue.replace(/<br\s*\/?>/ig, "\r\n") + '"';
Of course you need to do this only if the content contains \\r\\n (otherwise numbers get formatted as text) 当然,仅当内容包含\\ r \\ n时才需要执行此操作(否则数字将格式化为文本)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.