Search code examples
htmlcssexcelexport-to-excelhtml-table

white-space: pre; ignored/inconsistent inside <td> in export to (save as) .xls


consider the following, in a browser:

<tr>
   <td><pre>My  ex       tra   whitespace is    preserved</pre></td>
</tr>
<tr>
   <td style="white-space: pre;">My  ex       tra   whitespace is    preserved</td>
</tr>

But in excel, when I open the above .html file, the extra whitespaces are removed unless I use <pre>

This is a problem because one table I'm exporting has 50 columns and ~2000 rows. This not only balloons the size of the file (which would be fine), but also causes excel to crash.

Does anyone know of a workaround for this?

inconsistency


Solution

  • I used the pre tags to wrap the tds like this:

    <tr>
        <pre>
        <td>My  ex       tra   whitespace is    preserved</td>
        <td>My  ex       tra   whitespace is    preserved</td>
        <td>My  ex       tra   whitespace is    preserved</td>
        </pre>
    </tr>
    

    It worked just fine. I used on a 2000 line HTML table with 6 columns.