【问题标题】:Docx - Removing section of documentDocx - 删除文档部分
【发布时间】:2015-11-14 08:11:49
【问题描述】:

有没有办法删除文档中可以指定开始和结束标签的部分?

我需要一种方法,我可以通过传入我的开始和结束捕获(@@DELETEBEGIN 和 @@DELETEEND)来删除文档的一部分

例如,我的文档中有这个:

您好,欢迎阅读本文档

@@DELETEBEGIN{要在代码中检查的一些值}

如果值为真,一些文本将被删除

@@DELETEEND

最后一行

【问题讨论】:

  • 我不认为有一个简单的理由这样做或根本没有办法。
  • 我可能已经找到了解决方案,请在下面查看我的答案

标签: c# regex novacode-docx


【解决方案1】:

如果您需要删除从@@DELETEBEGIN 到@@DELETEEND 的文本,其中@@DELETEBEGIN 不在Paragraph 的开头并且@@DELETEEND 不在Paragraph 的末尾,则此代码应工作。

DocX document = DocX.Load("C:\\Users\\phil\\Desktop\\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
    //use this if you need whole text of a paragraph
    string paraText = item.Text;
    var result = paraText.Split(' ');
    int count = 0;
    list2 = new List<string>();
    //use this if you need word by word
    foreach (var data in result)
    {
        string word = data.ToString();
        if (word.Contains("@@DELETEBEGIN")) flag = true;
        if (word.Contains("@@DELETEEND"))
        { 
            flag = false;
            list2.Add(word);
        }
        if (flag) list2.Add(word); 
        count++;
    }
    list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
    string temp = "";
    for (int y = 0; y < list1[i].Count(); y++)
    {
        if (y == 0) 
        {
            temp = list1[i][y];
            continue;
        }
        temp += " " + list1[i][y];                   
    }
    if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();

我必须感谢post 循环遍历每个单词。

【讨论】:

  • 回复晚了,但我不得不说谢谢!我开始遇到我之前在一些客户文档中使用的代码的问题。这种方法更健壮,在我的情况下效果更好。非常感谢!
  • 没问题,去年夏天我和 Novacode 一起工作了很多次,处理过很多不同的事情。
【解决方案2】:

我想我已经找到了解决方案,至少它对我有用,如果有什么我可以做得更好的,请告诉我:

deleteCommand 是@@DELETEBEGIN 字符串,deleteEndCommand 是@@DELETEEND

    private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
    {
        try
        {
            int deleteStart = 0;
            int deleteEnd = 0;

            //Get the array of the paragraphs containing the start and end catches
            for (int i = 0; i < doc.Paragraphs.Count; i++)
            {
                if (doc.Paragraphs[i].Text.Contains(deleteCommand))
                    deleteStart = i;
                if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
                    deleteEnd = i;
            }

            if (deleteStart > 0 && deleteEnd > 0)
            {
                //delete from the paraIndex as the arrays will shift when a paragraph is deleted
                int paraIndex = deleteStart;
                for (int i = deleteStart; i <= deleteEnd; i++)
                {
                    doc.RemoveParagraphAt(paraIndex);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

【讨论】:

  • 是的,如果您想删除从 Paragraph[i] 的开头到 Paragraph[i] 的末尾的某些内容,那么这很有效。如果您想将Paragraph[i] 中间的某些内容删除到另一个Paragraph[i] 的不同位置,则它不会起作用。如果您的解决方案解决了您的问题,那很好。
  • 是的,这是真的,因为我认为这是不可能的,因为文档的结构,但这段代码对我有用,因为在我的情况下,删除字符串将始终是单独的段落
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多