-
-
Notifications
You must be signed in to change notification settings - Fork 99
html支持 #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
html支持 #116
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,26 @@ | ||
| namespace MiniSoftware | ||
| { | ||
| using DocumentFormat.OpenXml; | ||
| using DocumentFormat.OpenXml.Drawing.Charts; | ||
| using DocumentFormat.OpenXml.Packaging; | ||
| using DocumentFormat.OpenXml.Wordprocessing; | ||
| using Extensions; | ||
| using Utility; | ||
| using HtmlToOpenXml; | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Xml; | ||
| using System.Xml.Linq; | ||
| using Utility; | ||
| using A = DocumentFormat.OpenXml.Drawing; | ||
| using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing; | ||
| using PIC = DocumentFormat.OpenXml.Drawing.Pictures; | ||
| using System.Xml; | ||
| using System.Xml.Linq; | ||
| using DocumentFormat.OpenXml.Drawing.Charts; | ||
| using System.Threading.Tasks; | ||
| using System.Threading; | ||
|
|
||
| public static partial class MiniWord | ||
| { | ||
|
|
@@ -653,6 +654,16 @@ private static void ReplaceText(Paragraph p, WordprocessingDocument docx, Dictio | |
|
|
||
| t.Remove(); | ||
| } | ||
| else if (value is MiniWordHtml html) | ||
| { | ||
| AddHtmls(docx, run, new[] { html }); | ||
| t.Remove(); | ||
| } | ||
| else if (value is IEnumerable<MiniWordHtml> htmlList) | ||
| { | ||
| AddHtmls(docx, run, htmlList); | ||
| t.Remove(); | ||
| } | ||
| else | ||
| { | ||
| var newText = value is DateTime | ||
|
|
@@ -1175,5 +1186,57 @@ private static byte[] GetBytes(string path) | |
| return ms.ToArray(); | ||
| } | ||
| } | ||
|
|
||
| #region html支持 | ||
|
|
||
| /// <summary> | ||
| /// 填充htmls | ||
| /// </summary> | ||
| /// <param name="run"></param> | ||
| /// <param name="miniWordHtmls"></param> | ||
| private static void AddHtmls(WordprocessingDocument docx, Run run, IEnumerable<MiniWordHtml> miniWordHtmls) | ||
| { | ||
| //找到当前顶级段落(body)添加,html中的表格不能直接放在run或者段落里 | ||
| Paragraph topPara = FindTopPara(run); | ||
| if (topPara == null) return; | ||
| foreach (var miniWordHtml in miniWordHtmls) | ||
| { | ||
| try | ||
| { | ||
| //实例化转换对象 | ||
| HtmlConverter converter = new HtmlConverter(docx.MainDocumentPart); | ||
| //解析 | ||
| var paras = converter.Parse(miniWordHtml.HtmlText); | ||
| //倒排插入(因为都是插入到标记位置后面所以需要倒排) | ||
| for (var i = paras.Count - 1; i >= 0; i--) | ||
| { | ||
| var item = paras[i]; | ||
| topPara.Parent.InsertAfter(item, topPara); | ||
| } | ||
| } | ||
| catch (Exception) | ||
| { } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 找到当前顶级段落(body)添加 | ||
| /// </summary> | ||
| /// <param name="run"></param> | ||
| /// <returns></returns> | ||
| private static Paragraph FindTopPara(Run run) | ||
| { | ||
| for (var pnode = run.Parent; pnode != null; pnode = pnode.Parent) | ||
| { | ||
| if (pnode is Paragraph para && pnode.Parent != null && pnode.Parent is Body) | ||
| { | ||
| return para; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
Comment on lines
+1227
to
+1237
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
private static Paragraph FindTopPara(Run run)
{
for (var pnode = run.Parent; pnode != null; pnode = pnode.Parent)
{
if (pnode is Paragraph para && pnode.Parent is Body)
{
return para;
}
}
return null;
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已调整 |
||
|
|
||
| #endregion | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace MiniSoftware | ||
| { | ||
| /// <summary> | ||
| /// html参数对象 | ||
| /// </summary> | ||
| public class MiniWordHtml | ||
| { | ||
| public string HtmlText { get; set; } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对
MiniWordHtml,MiniWordHtml[]和IEnumerable<MiniWordHtml>的处理存在代码重复,并且对IEnumerable<MiniWordHtml>调用ToArray()会带来不必要的性能开销。可以通过修改AddHtmls方法的签名,使其接受IEnumerable<MiniWordHtml>,来简化这部分逻辑。这样可以合并MiniWordHtml[]和IEnumerable<MiniWordHtml>的处理分支,并避免不必要的内存分配和集合转换。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已调整