Benda
Benda

Reputation: 110

Export HTML table to XLS and retain formatting

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>

Upvotes: 1

Views: 9497

Answers (1)

P. Gearman
P. Gearman

Reputation: 1166

To the best of my knowledge, only inline CSS on the table elements will export properly.

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.

Upvotes: 2

Related Questions