我正在使用NotesForHtmlToOpenXML(http://notesforhtml2openxml.codeplex.com)将HTML文件转换为OpenXML格式
当我将上表的HTML转换为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 (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>
请提供解决方案,将上述HTML正确转换为OpenXML。
答案 0 :(得分:1)
我可以使用下面显示的代码(AltChunk&amp; 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();