HTML导出到Excel,mso-data-placement:same-cell丢失换行符

时间:2013-08-19 21:17:35

标签: html excel

我正在生成一个HTML文档,然后我将其用作Excel电子表格。一切都工作正常,除了首先,Excel正在阅读作为新单元格的换行符。我找到了一个解决方法,那就是添加<style>br { mso-data-placement:same-cell; }</style>,确保将所有数据保存在同一个单元格中,但我完全丢失了换行符并将它们保存在同一行中。

还有其他人有这个问题吗?

我无法弄清楚为什么会发生这种情况。这是我的完整代码:

<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>

1 个答案:

答案 0 :(得分:3)

您没有关闭“head”标记。 如果仍然无效,请尝试将样式设置为内联。例如:

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

与它在样式标签中相反。 Excel应该评估您的头部样式,但是让我们检查那里是否存在问题。