【问题标题】:Excel export to HTML cells overlappingExcel 导出到重叠的 HTML 单元格
【发布时间】:2020-01-01 03:34:57
【问题描述】:

我有一个 Excel 工作簿,我正在尝试导出为 HTML 格式(从下面的 Powershell 调用的 vba 脚本)。当我在本地机器(Win10、Office 2016)上运行它时,格式非常好。但是,当我尝试从它应该存在的服务器(WS2012、Office 2016)运行相同的脚本时,格式被破坏了(奇怪的是,在某些机器上的 Internet Explorer 中除外)。

我无法弄清楚为什么启动计算机会导致仅此部分的格式出现问题。

Excel 原文:

工作:

不工作:

所以在我的本地计算机和服务器上运行之间存在一些问题,服务器一直在 O'2013 上,但即使在尝试 O'2016 之后它仍然无法正常工作。我还禁用了宏安全性,以防万一搞砸了(它在我的本地被禁用),这也无济于事。

excel文件和vba应该没问题,因为它可以正常工作。

导出 HTML 的宏:

With ActiveWorkbook.PublishObjects.Add(xlSourceWorkbook, _

        "C:\Tasks\Phonebook-HTML\Web\Index.htm", , , xlHtmlStatic, _

        "TELEPHONEBOOK_18991", "")

        .Publish (True)

        .AutoRepublish = False

    End With

工作 HTML:

<tr height="59" style="mso-height-source:userset;height:44.45pt">

  <td height="59" class="xl154" style="height:44.45pt"><a href="sheet007.html#RANGE!A2"><span style="color:#0070C0;font-size:16.0pt;

  font-weight:700">A</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A27"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">B</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A94"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">C</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A157"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">D</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A215"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">E</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A227"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">F</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A247"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">G</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A281"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">H</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A328"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">I</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A334"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">J</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A354"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">K</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A391"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">L</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A422"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">M</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A503"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">N</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A517"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">O</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A534"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">P</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A585"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Q</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A587"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">R</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A634"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">S</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A695"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">T</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A718"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">U</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A719"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">V</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A741"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">W</span></a></td>

  <td class="xl154"><a href=""><span style="color:#0070C0;font-size:16.0pt;

  font-weight:700">X</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A767"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Y</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A771"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Z</span></a></td>

</tr>

不工作的 HTML:

<tr height="59" style="mso-height-source:userset;height:44.45pt">

  <td height="59" class="xl154" style="height:44.45pt"><a href="sheet007.html#RANGE!A2"><span style="color:#0070C0;font-size:16.0pt;

  font-weight:700">A</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A27"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">B</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A94"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">C</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A157"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">D</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A215"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">E</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A227"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">F</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A247"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">G</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A281"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">H</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A328"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">I</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A334"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">J</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A354"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">K</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A391"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">L</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A422"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">M</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A503"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">N</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A517"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">O</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A534"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">P</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A585"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Q</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A587"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">R</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A634"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">S</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A695"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">T</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A718"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">U</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A719"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">V</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A741"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">W</span></a></td>

  <td class="xl154"><a href=""><span style="color:#0070C0;font-size:16.0pt;

  font-weight:700">X</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A767"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Y</span></a></td>

  <td class="xl154"><a href="sheet007.html#RANGE!A771"><span style="color:#0070C0;

  font-size:16.0pt;font-weight:700">Z</span></a></td>

</tr>

【问题讨论】:

    标签: html excel vba powershell


    【解决方案1】:

    注意:工作和非工作 HTML 没有区别。我使用了 Diff 比较,它们 100% 相同。

    excel文件和vba应该没问题,因为它可以正常工作。

    看起来你已经点击了 KB257757:https://support.microsoft.com/en-au/help/257757/considerations-for-server-side-automation-of-office

    不知何故,文本“应该在每周一、周三、周五早上更新”的合并单元格将字母向右推。

    我对@9​​87654324@ 持怀疑态度,尽管 Excel 的服务器端自动化不受支持,您能否看看此代码是否有效:

    Workbook workbook = new Workbook(); 
    workbook.LoadFromFile("Book1.xlsx"); 
    Worksheet sheet = workbook.Worksheets[0]; 
    sheet.SaveToHtml("sample.html"); 
    

    如果这不能解决问题,而不是使用 PublishObjects,您可以尝试自己导出 HTML 文件,使用以下代码:https://gist.github.com/jkatricak/801024,如果您需要保留 HTML 样式,您可以调整: Export the dataGridView to Excel with all the cells format

    这将使您可以选择使用自己的 CSS 格式化 HTML 表格,例如:

    <style type="text/css">
        table.tableizer-table {
            font-size: 12px;
            border: 1px solid #CCC; 
            font-family: Arial, Helvetica, sans-serif;
        } 
        .tableizer-table td {
            padding: 4px;
            margin: 3px;
            border: 1px solid #CCC;
        }
        .tableizer-table th {
            background-color: #104E8B; 
            color: #FFF;
            font-weight: bold;
        }
    </style>
    <table class="tableizer-table">
    <thead><tr class="tableizer-firstrow"><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th><th>G</th><th>H</th><th>I</th><th>J</th><th>K</th><th>L</th><th>M</th><th>N</th><th>O</th><th>P</th><th>Q</th><th>R</th><th>S</th><th>T</th><th>U</th><th>V</th><th>W</th><th>X</th><th>Y</th><th>Z</th></tr>
    </tbody></table>
    

    基本上,如果上述方法无济于事,您将需要解决它。 Microsoft 提出了 XML 格式,您可以在没有 Microsoft.Office.Excel.Interop.DLL 的情况下使用 ClosedXML(或 OpenXML,如果您准备迎接挑战)。您可以尝试其他付费产品,例如 EPPPlus - YMMV。

    【讨论】:

    • 谢谢!请看我下面的帖子,这很有帮助
    【解决方案2】:

    @杰里米汤普森

    你让我的大脑沿着正确的路线思考: Somehow the Merge cells of the text "should be updated every Monday, Wednesday, Friday morning" is pushing the Alphabet to the right. 我只是在这两个部分之间添加了一个额外的行,它似乎已经稳定了这两个部分。我们现在用煤气做饭!

    Excel

    网络

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多