如果文本框为空,则ASP.NET Open XML删除段落符号

时间:2018-06-25 09:08:09

标签: c# asp.net openxml

我使用OpenXML查找并替换单词中的文本。我有一个下面的FindAndReplace方法:

 private void FindAndReplace(string filename)
{


    try
    {
        if (nametextbox.Value == "")
        {
            ReplaceText("xxxxxNamexxxxx", "", filename);

        }
        else
        {
            ReplaceText("xxxxxNamexxxxx", nametextbox.Value, filename);             
        }

        if (address1textbox.Value == "")
        {
            ReplaceText("xxxxxAddress line 1xxxxx", "", filename);

        }
        else
        {
            ReplaceText("xxxxxAddress line 1xxxxx", address1textbox.Value, filename);
        }}

如果文本框值为空,则当前会替换文本并保留段落符号,因此为空行。我要删除段落符号:

<w:p w:rsidRPr=\"00257621\" w:rsidR=\"00614B04\" w:rsidP=\"00AA1CE5\" w:rsidRDefault=\"004E688E\">
                    <w:pPr>
                        <w:jc w:val=\"both\" />
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                            <w:lang w:val=\"en-GB\" />
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                            <w:lang w:val=\"en-GB\" />
                        </w:rPr>
                        <w:t>xxxxxNamexxxxx</w:t>
                    </w:r>
                </w:p>
                <w:p w:rsidR=\"00D54251\" w:rsidP=\"00257621\" w:rsidRDefault=\"00614B04\">
                    <w:pPr>
                        <w:jc w:val=\"center\" />
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                        </w:rPr>
                    </w:pPr>
                    <w:r w:rsidRPr=\"00614B04\">
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                            <w:lang w:val=\"en-GB\" />
                        </w:rPr>
                        <w:t>with address</w:t>
                    </w:r>
                </w:p>
                <w:p w:rsidRPr=\"00257621\" w:rsidR=\"001C1EFD\" w:rsidP=\"001C1EFD\" w:rsidRDefault=\"001C1EFD\">
                    <w:pPr>
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                            <w:lang w:val=\"en-GB\" />
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:rPr>
                            <w:sz w:val=\"20\" />
                            <w:lang w:val=\"en-GB\" />
                        </w:rPr>
                        <w:t>xxxxxAddress line 1xxxxx</w:t>
                    </w:r>
                </w:p>

使用Open XML的FindAndReplace方法如下:

    private void ReplaceText(string replacefrom, string replaceto, string filename) 
    {
        string caseno = casetextboxinput.Value;
        string nameno = nametextbox.Value;
        string directorycasefile = directoryrootmerge + caseno + @"\" + sequenceCounter + " " + nameno + " " + filename;
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(directorycasefile, true))
        {
        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }

        Regex regexText = new Regex(replacefrom);
        docText = regexText.Replace(docText, replaceto);

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
            sw.Write(docText);
        }
        wordDoc.MainDocumentPart.Document.Save();            
    }
    }

如果文本框为空,如何删除用word替换的空行?

0 个答案:

没有答案