NotesForHtmlToOpenXML 表行跨度 &列跨度问题

发布于 2025-01-01 03:34:37 字数 3274 浏览 0 评论 0原文

将 HTML 文件转换为 OpenXML 格式

我正在使用 NotesForHtmlToOpenXML (http://notesforhtml2openxml.codeplex.com) 原始表格图像

当我将上表的 HTML 转换为 docx 格式时,输出如下。

转换后的 docx 表格图像

下面是原始表格的 HTML 代码。

 <table id="tbl-pg-2-13-5" class="table-main" border="1" cellspacing="1" cellpadding="1">
    <tbody>
        <tr id="pg-2-14">
            <td id="pg-2-15" class="table-td" valign="top" rowspan="2" colspan="2">
            </td>
            <td id="pg-2-16" class="table-td" valign="top" colspan="2">
                <p id="pg-2-17" class="table-textstyle2">
                    <i>Consideration of use</i>
                </p>
            </td>
        </tr>
        <tr id="pg-2-19">
            <td id="pg-2-20" class="table-td" valign="top">
                <p id="pg-2-21" class="table-textstyle2">
                    <i>Low</i>
                </p>
            </td>
            <td id="pg-2-23" class="table-td" valign="top">
                <p id="pg-2-24" class="table-textstyle2">
                    <i>High</i>
                </p>
            </td>
        </tr>
        <tr id="pg-2-26">
            <td id="pg-2-27" class="table-td" rowspan="2" valign="top">
                <p id="pg-2-28" class="table-textstyle2">
                    <i>Quest for fundamental understanding</i>
                </p>
            </td>
            <td id="pg-2-30" class="table-td" valign="top">
                <p id="pg-2-31" class="table-textstyle2">
                    <i>High</i>
                </p>
            </td>
            <td id="pg-2-33" class="table-td" valign="top">
                <p id="pg-2-61" class="table-textstyle0">
                    1. Pure basic research &#x0028;Bohr&#x0029;
                </p>
            </td>
            <td id="pg-2-38" class="table-td" valign="top">
                <p id="pg-2-62" class="table-textstyle0">
                    2. Use-inspired basic research &#x0028;Pasteur&#x0029;
                </p>
            </td>
        </tr>
        <tr id="pg-2-45">
            <td id="pg-2-46" class="table-td" valign="top">
                <p id="pg-2-47" class="table-textstyle2">
                    <i>Low</i>&#x00A0;
                </p>
            </td>
            <td id="pg-2-49" class="table-td" valign="top">
                <p id="pg-2-63" class="table-textstyle0">
                    3.
                </p>
            </td>
            <td id="pg-2-52" class="table-td" valign="top">
                <p id="pg-2-64" class="table-textstyle0">
                    4. Pure applied research &#x0028;Edison&#x0029;
                </p>
            </td>
        </tr>
    </tbody>
</table>

请提供将上述 HTML 正确转换为 OpenXML 的解决方案。

I am converting a HTML file to OpenXML format using NotesForHtmlToOpenXML (http://notesforhtml2openxml.codeplex.com)

Original Table image

When I convert the above table's HTML to docx format, the output is given below.

Converted docx Table image

Given below is the HTML code of the original table.

 <table id="tbl-pg-2-13-5" class="table-main" border="1" cellspacing="1" cellpadding="1">
    <tbody>
        <tr id="pg-2-14">
            <td id="pg-2-15" class="table-td" valign="top" rowspan="2" colspan="2">
            </td>
            <td id="pg-2-16" class="table-td" valign="top" colspan="2">
                <p id="pg-2-17" class="table-textstyle2">
                    <i>Consideration of use</i>
                </p>
            </td>
        </tr>
        <tr id="pg-2-19">
            <td id="pg-2-20" class="table-td" valign="top">
                <p id="pg-2-21" class="table-textstyle2">
                    <i>Low</i>
                </p>
            </td>
            <td id="pg-2-23" class="table-td" valign="top">
                <p id="pg-2-24" class="table-textstyle2">
                    <i>High</i>
                </p>
            </td>
        </tr>
        <tr id="pg-2-26">
            <td id="pg-2-27" class="table-td" rowspan="2" valign="top">
                <p id="pg-2-28" class="table-textstyle2">
                    <i>Quest for fundamental understanding</i>
                </p>
            </td>
            <td id="pg-2-30" class="table-td" valign="top">
                <p id="pg-2-31" class="table-textstyle2">
                    <i>High</i>
                </p>
            </td>
            <td id="pg-2-33" class="table-td" valign="top">
                <p id="pg-2-61" class="table-textstyle0">
                    1. Pure basic research (Bohr)
                </p>
            </td>
            <td id="pg-2-38" class="table-td" valign="top">
                <p id="pg-2-62" class="table-textstyle0">
                    2. Use-inspired basic research (Pasteur)
                </p>
            </td>
        </tr>
        <tr id="pg-2-45">
            <td id="pg-2-46" class="table-td" valign="top">
                <p id="pg-2-47" class="table-textstyle2">
                    <i>Low</i> 
                </p>
            </td>
            <td id="pg-2-49" class="table-td" valign="top">
                <p id="pg-2-63" class="table-textstyle0">
                    3.
                </p>
            </td>
            <td id="pg-2-52" class="table-td" valign="top">
                <p id="pg-2-64" class="table-textstyle0">
                    4. Pure applied research (Edison)
                </p>
            </td>
        </tr>
    </tbody>
</table>

Please provide solution to convert the above HTML to OpenXML correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-08 03:34:37

我能够使用下面显示的代码(AltChunk & AlternateFormatImportPart)进行导入。这里的finalDocument 是WordprocessingDocument 类型。

                   string mainhtml = "<table id=\"tbl-pg-2-13-5\" class=\"table-main\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">" +
"<tbody>" +
    "<tr id=\"pg-2-14\">" +
        "<td id=\"pg-2-15\" class=\"table-td\" valign=\"top\" rowspan=\"2\" colspan=\"2\">" +
        "</td>" +
        "<td id=\"pg-2-16\" class=\"table-td\" valign=\"top\" colspan=\"2\">" +
            "<p id=\"pg-2-17\" class=\"table-textstyle2\">" +
                "<i>Consideration of use</i>" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-19\">" +
        "<td id=\"pg-2-20\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-21\" class=\"table-textstyle2\">" +
                "<i>Low</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-23\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-24\" class=\"table-textstyle2\">" +
                "<i>High</i>" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-26\">" +
        "<td id=\"pg-2-27\" class=\"table-td\" rowspan=\"2\" valign=\"top\">" +
            "<p id=\"pg-2-28\" class=\"table-textstyle2\">" +
                "<i>Quest for fundamental understanding</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-30\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-31\" class=\"table-textstyle2\">" +
                "<i>High</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-33\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-61\" class=\"table-textstyle0\">" +
                "1. Pure basic research (Bohr)" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-38\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-62\" class=\"table-textstyle0\">" +
                "2. Use-inspired basic research (Pasteur)" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-45\">" +
        "<td id=\"pg-2-46\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-47\" class=\"table-textstyle2\">" +
                "<i>Low</i> " +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-49\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-63\" class=\"table-textstyle0\">" +
                "3." +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-52\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-64\" class=\"table-textstyle0\">" +
                "4. Pure applied research (Edison)" +
            "</p>" +
        "</td>" +
    "</tr>" +
"</tbody>" +

“”;

                AlternativeFormatImportPart chunk = finalDocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);

                using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter stringWriter = new StreamWriter(chunkStream, Encoding.UTF8))
                    {
                        stringWriter.Write(mainhtml);
                    }
                }

                AltChunk altChunk1 = new AltChunk();
                altChunk1.Id = finalDocument.MainDocumentPart.GetIdOfPart(chunk);

                finalDocument.MainDocumentPart.Document.AppendChild(altChunk1);


                finalDocument.MainDocumentPart.Document.Save();

I was able to import using code(AltChunk & AlternateFormatImportPart) displayed below. Here finalDocument is of type WordprocessingDocument.

                   string mainhtml = "<table id=\"tbl-pg-2-13-5\" class=\"table-main\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">" +
"<tbody>" +
    "<tr id=\"pg-2-14\">" +
        "<td id=\"pg-2-15\" class=\"table-td\" valign=\"top\" rowspan=\"2\" colspan=\"2\">" +
        "</td>" +
        "<td id=\"pg-2-16\" class=\"table-td\" valign=\"top\" colspan=\"2\">" +
            "<p id=\"pg-2-17\" class=\"table-textstyle2\">" +
                "<i>Consideration of use</i>" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-19\">" +
        "<td id=\"pg-2-20\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-21\" class=\"table-textstyle2\">" +
                "<i>Low</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-23\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-24\" class=\"table-textstyle2\">" +
                "<i>High</i>" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-26\">" +
        "<td id=\"pg-2-27\" class=\"table-td\" rowspan=\"2\" valign=\"top\">" +
            "<p id=\"pg-2-28\" class=\"table-textstyle2\">" +
                "<i>Quest for fundamental understanding</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-30\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-31\" class=\"table-textstyle2\">" +
                "<i>High</i>" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-33\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-61\" class=\"table-textstyle0\">" +
                "1. Pure basic research (Bohr)" +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-38\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-62\" class=\"table-textstyle0\">" +
                "2. Use-inspired basic research (Pasteur)" +
            "</p>" +
        "</td>" +
    "</tr>" +
    "<tr id=\"pg-2-45\">" +
        "<td id=\"pg-2-46\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-47\" class=\"table-textstyle2\">" +
                "<i>Low</i> " +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-49\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-63\" class=\"table-textstyle0\">" +
                "3." +
            "</p>" +
        "</td>" +
        "<td id=\"pg-2-52\" class=\"table-td\" valign=\"top\">" +
            "<p id=\"pg-2-64\" class=\"table-textstyle0\">" +
                "4. Pure applied research (Edison)" +
            "</p>" +
        "</td>" +
    "</tr>" +
"</tbody>" +

"";

                AlternativeFormatImportPart chunk = finalDocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);

                using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter stringWriter = new StreamWriter(chunkStream, Encoding.UTF8))
                    {
                        stringWriter.Write(mainhtml);
                    }
                }

                AltChunk altChunk1 = new AltChunk();
                altChunk1.Id = finalDocument.MainDocumentPart.GetIdOfPart(chunk);

                finalDocument.MainDocumentPart.Document.AppendChild(altChunk1);


                finalDocument.MainDocumentPart.Document.Save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文