将HTML表导出到XLS并保留格式 [英] Export HTML table to XLS and retain formatting

查看:129
本文介绍了将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 = < td> 中的background-color:yellow,导出文件将有一个黄色单元格,但我不相信跨度,标记或内联div通过它们的CSS。

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.

这篇关于将HTML表导出到XLS并保留格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆