2010-02-20 67 views
3

我想提取word文档中出现的子弹信息。 我想是这样的: 假设下面的文字,是word文档中:如何从word文档中提取项目符号信息?

步骤启动汽车:

  • 开门
  • 坐在里面
  • 关上门
  • 插入钥匙

然后,我希望我的文本文件,如下图所示:

步骤启动汽车:

<BULET>开门</BULET >

<BULET>坐在里面</BULET >

<BULET>关门</BULET >

<BULET> Insert键</BULET >

<BULET>等</BULET >

我使用C#语言来做到这一点。

我可以从word文档中提取段落,并直接在文本文件中使用某些格式化信息(如文本是粗体还是斜体等)编写它们,但不知道如何提取此项子项信息。

任何人都可以请告诉我如何做到这一点?

在此先感谢

回答

-4

我得到的答案.....

首先我是款基础上转换文档。但是,如果我们逐句处理doc文件,则可以确定该句子是否包含子弹或任何形状,或者该句子是否为表格的一部分。所以一旦我们得到这些信息,我们就可以适当地转换这个句子。如果有人需要源代码,我可以分享它。

+1

请注意与我们分享? – Martijn 2010-02-25 12:55:22

+2

嗨shekar, 请问您可以分享一下代码片段,从文档中提取项目符号信息吗? 我也面临同样的问题 预先感谢 – Tamizhvendan 2011-01-23 01:56:52

+0

你真的应该张贴解决你的问题的代码。这个答案不是非常有用,否则将来可能会被删除。 – 2012-01-22 11:03:48

1

您可以通过阅读每个句子来完成。 doc。句子是一个Range对象的数组。所以你可以从段落中获得相同的Range对象。

 foreach (Paragraph para in oDoc.Paragraphs) 
     { 
      string paraNumber = para.Range.ListFormat.ListLevelNumber.ToString(); 
      string bulletStr = para.Range.ListFormat.ListString; 
      MessageBox.Show(paraNumber + "\t" + bulletStr + "\t" + para.Range.Text); 
     } 

进入paraNumber,你可以得到段落级别,进入buttetStr你可以得到子弹作为字符串。

1

我使用的是由Eric White提供的OpenXMLPower工具this。它的免费和可用在NUGet包。你可以从Visual Studio软件包管理器安装它。 enter image description here

他提供了一个随时可用的代码片段。这个工具为我节省了很多时间。以下是我定制的代码片段用于我的要求的方式。 Infact你可以在你的项目中使用这些方法。

private static WordprocessingDocument _wordDocument; 
private StringBuilder textItemSB = new StringBuilder(); 
private List<string> textItemList = new List<string>(); 


/// Open word document using office SDK and reads all contents from body of document 
/// </summary> 
/// <param name="filepath">path of file to be processed</param> 
/// <returns>List of paragraphs with their text contents</returns> 
private void GetDocumentBodyContents() 
{ 
    string modifiedString = string.Empty; 
    List<string> allList = new List<string>(); 
    List<string> allListText = new List<string>(); 

    try 
    { 
_wordDocument = WordprocessingDocument.Open(wordFileStream, false); 
     //RevisionAccepter.AcceptRevisions(_wordDocument); 
     XElement root = _wordDocument.MainDocumentPart.GetXDocument().Root; 
     XElement body = root.LogicalChildrenContent().First(); 
     OutputBlockLevelContent(_wordDocument, body); 
    } 
    catch (Exception ex) 
    { 
     logger.Error("ERROR in GetDocumentBodyContents:" + ex.Message.ToString()); 
    } 
} 


// This is recursive method. At each iteration it tries to fetch listitem and Text item. Once you have these items in hand 
// You can manipulate and create your own collection. 
private void OutputBlockLevelContent(WordprocessingDocument wordDoc, XElement blockLevelContentContainer) 
{ 
    try 
    { 
     string listItem = string.Empty, itemText = string.Empty, numberText = string.Empty; 
     foreach (XElement blockLevelContentElement in 
      blockLevelContentContainer.LogicalChildrenContent()) 
     { 
      if (blockLevelContentElement.Name == W.p) 
      { 
       listItem = ListItemRetriever.RetrieveListItem(wordDoc, blockLevelContentElement); 
       itemText = blockLevelContentElement 
        .LogicalChildrenContent(W.r) 
        .LogicalChildrenContent(W.t) 
        .Select(t => (string)t) 
        .StringConcatenate(); 
       if (itemText.Trim().Length > 0) 
       { 
        if (null == listItem) 
        { 
         // Add html break tag 
         textItemSB.Append(itemText + "<br/>"); 
        } 
        else 
        { 
         //if listItem == "" bullet character, replace it with equivalent html encoded character         
         textItemSB.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + (listItem == "" ? "&bull;" : listItem) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + itemText + "<br/>"); 
        } 
       } 
       else if (null != listItem) 
       { 
        //If bullet character is found, replace it with equivalent html encoded character 
        textItemSB.Append(listItem == "" ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull;" : listItem); 
       } 
       else 
        textItemSB.Append("<blank>"); 
       continue; 
      } 
      // If element is not a paragraph, it must be a table. 

      foreach (var row in blockLevelContentElement.LogicalChildrenContent()) 
      {       
       foreach (var cell in row.LogicalChildrenContent()) 
       {        
        // Cells are a block-level content container, so can call this method recursively. 
        OutputBlockLevelContent(wordDoc, cell); 
       } 
      } 
     } 
     if (textItemSB.Length > 0) 
     { 
      textItemList.Add(textItemSB.ToString()); 
      textItemSB.Clear(); 
     } 
    } 
    catch (Exception ex) 
    { 
     ..... 
    } 
} 
相关问题