代码之家  ›  专栏  ›  技术社区  ›  user1372430

插入HTML而不是模板中的键。塔尖上的docx。Doc文件

  •  1
  • user1372430  · 技术社区  · 6 年前

    我想从创建Word文档 Template.docx 带尖顶。文件编号:。为此,我编写了如下代码:

    //sample file path
    string samplePath = Application.StartupPath + Path.DirectorySeparatorChar + "Template.docx";
    //result docs paths
    string docxPath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.docx";
    

    按钮提交事件:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        //initialize word object
        document = new Document();
        document.LoadFromFile(samplePath);
        //get strings to replace
        Dictionary<string, string> dictReplace = GetReplaceDictionary();
        //Replace text
        foreach (KeyValuePair<string, string> kvp in dictReplace)
        {
            document.Replace(kvp.Key, kvp.Value, true, true);
        }
        //Save doc file.
        document.SaveToFile(docxPath, FileFormat.Docx);
        document.Close();
    
    }
    

    最后是 GetReplaceDictionary() 方法:

    Dictionary<string, string> GetReplaceDictionary()
    {
        Dictionary<string, string> replaceDict = new Dictionary<string, string>();
        replaceDict.Add("#name#", txtName.Text.Trim());
        replaceDict.Add("#age#",txtAge.Text);
        replaceDict.Add("#address#", txtAddress.Text.Trim());
        replaceDict.Add("#phonenumber#",txtPhonenumber.Text);
        replaceDict.Add("#emailaddress#",txtEmailaddress.Text);
        replaceDict.Add("#experience#", txtExperience.Text.Trim());
        replaceDict.Add("#position#", txtPosition.Text.Trim());
        replaceDict.Add("#salary#", txtSalary.Text);
        replaceDict.Add("#applydate#",dateTimePicker.Text);
        string isEmployed= this.radio_isEmployed_Yes.Checked ? "Yes" : "No";
        replaceDict.Add("#isemployed#", isEmployed);
        replaceDict.Add("#education#", txtEducation.Text.Trim());
    
        return replaceDict;
    }
    

    我想这样写“工作经验”文本框:

    Lorem ipsum <b>dolor sit amet</b>, consectetur <i>adipiscing</i> elit.
    

    屏幕如下: enter image description here

    我想展示一下 dolor sit amet 加粗和 adipiscing 在word文档中倾斜,如下所示: enter image description here

    但不幸的是,HTML标记在Word文档中显示如下: enter image description here

    这是我的 样板docx公司 文件: enter image description here

    如何正确显示HTML文本而不是 #experience# 变量

    1 回复  |  直到 6 年前
        1
  •  1
  •   B.Porter    6 年前

    不幸的是,据我所知,用Spire做这件事并不容易。文档。“替换”只是将一个字符串替换为您已经发现的另一个字符串。《Spire节目指南》确实建议 solution to this 但它并不特别优雅。

    您需要为要替换的文本在模板文档中添加书签。因此,如果选择文本#experience#,并将其添加为名为experience的书签:

    enter image description here

    然后,您可以使用下面的函数将文本替换为提供的html:

    public void ReplaceBookmarkHTML(Document doc, string bookmarkName, string htmlString)
        {
            Section tempSection = doc.AddSection();
            tempSection.AddParagraph().AppendHTML(htmlString);
    
            ParagraphBase replacementFirstItem = tempSection.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase replacementLastItem = tempSection.Paragraphs[tempSection.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
            TextBodySelection selection = new TextBodySelection(replacementFirstItem, replacementLastItem);
            TextBodyPart part = new TextBodyPart(selection);
    
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
            bookmarkNavigator.MoveToBookmark(bookmarkName);
            bookmarkNavigator.ReplaceBookmarkContent(part);
            doc.Sections.Remove(tempSection);
        }
    

    然后,您可以像下面这样调用它,以使您的HTML替换书签:

    ReplaceBookmarkHTML(document, "experience", "Lorem ipsum <b>dolor sit amet</b>, consectetur <i>adipiscing</i> elit.");