将HTML表导出到XLS并保留格式
问题描述:
我想将HTML表导出到XLS,同时保留所有格式。
I would like to export a HTML table to XLS and at the same time retain all formatting.
以下代码似乎正在运行,除了小心是出口损失如何保持原状?
The following code seems to be working, except that the hilight is lost on export. How do I keep it in place?
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div id='data'>
<table border='1'>
<tr>
<td>
<strong>Greeting</strong>
</td>
<td>
<strong>Message</strong>
</td>
</tr>
<tr>
<td>
Hello
</td>
<td>
World. <mark>I am hilighted!</mark>
</td>
</tr>
</table>
</div>
<script type='text/javascript'>
$(document).ready(function()
{
$("#btnExport").click(function(e)
{
var path = 'data:application/vnd.ms-excel,' + encodeURIComponent($('#data').html());
window.open(path);
e.preventDefault();
});
});
</script>
<input type='button' id='btnExport' value='Export as XLS'>
</body>
答
据我所知,只有表格元素上的内联CSS才能正常导出。
To the best of my knowledge, only inline CSS on the table elements will export properly.
所以,如果你有 style =
,导出文件将有一个黄色单元格,但我不相信跨度,标记或内联div通过它们的CSS。< td>
中的background-color:yellow
So, if you had style="background-color: yellow"
on a <td>
, the export file would have a yellow cell, but I don't believe spans, marks or inline divs carry their CSS through at all.