Home » excel » HTML export into Excel, mso-data-placement: same-cell loses the line break

HTML export into Excel, mso-data-placement: same-cell loses the line break

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m generating an HTML document which I’m then serving as an Excel spreadsheet. Everything is working fine except in the first place, Excel is reading
line breaks as a new cell. I found a workaround for that, and that’s to add <style>br { mso-data-placement:same-cell; }</style> which sure enough keeps all data in the same cell, but I’m losing the line breaks completely and it keeps it all in the same line.

Has anyone else had this problem?

I can’t figure out for the life of me why this is happening. Here is my full code:

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html;charset=windows-1252">
<meta name=ProgId content=Excel.Sheet/>
<meta name=Generator content="Microsoft Excel 11">
<!--[if gte mso 9]><xml>
        <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
                <x:Name>Sheet1</x:Name>
                <x:WorksheetOptions><x:Panes>
                </x:Panes></x:WorksheetOptions>
                </x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook>
</xml>
<![endif]-->
<style>
br {mso-data-placement:same-cell;}
</style>
<body>
<table>
  <tr>
    <td colspan="2" style="font-size:14pt;font-weight:bold;"><?= $user['company_name'] ?> File System Listing</th>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">1. To Enter or Sort</td>
  </tr>
  <tr>
    <td width="45">&nbsp;</td>
    <td width="300">To Enter</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>To Pay</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>To Do</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>To File- Completed</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><?= $user['business_bank'] ?><br>Deposit Slips</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>Receipt Sorting <?= $user['business_bank'] ?><br><?= $user['account1_name'] ?> Card x<?= $user['account1_debit'] ?><br><?= $user['account1_name'] ?> x<?= $user['account1_num'] ?></td>
  </tr>
</table>
</body>
</html>
How to&Answers:

You don’t have a closing “head” tag.
If that still doesn’t work, try placing the style inline. For instance:

<br style="mso-data-placement:same-cell;" />

As opposed to it being in a style tag. Excel should be evaluating your head styles but let’s check if there’s an issue there.