2012-06-28 22 views
5

Ich habe ein Problem mit der Analyse von * .docx Dokument mit OpenXML (C#).Extract-Tabelle von DOCX

So, hier sind meine Schritte:
1. Laden * .docx Dokument
2. Recieve Liste der Absätze
3. In jedem Absatz Look für Text-, Bild- und Tabellenelemente
4. Für jeden Text und Bildelement hTML-Tags
5. Speichern Ausgabe erstellen, wie * .html

Datei ich habe herausgefunden, wie Bilddatei im Dokument zu finden und sie extrahieren. Nun ist noch ein Schritt zu gehen - finden Sie, wo sich die Tabellenposition im Text befindet (Absatz).

Wenn jemand weiß, wie man eine Tabelle im * .docx Dokument mit OpenXML findet, bitte helfen. Danke.

Zusätzlich: Ok, vielleicht bin ich nicht klar erklären, was ich meine. Wenn wir Inhalt des Absatzes erhalten, können Sie Chield-Objekte als Textblöcke, Bilder und so weiter finden. Also, wenn der Absatz enthält, dass Bild enthält, bedeutet dies, dass an dieser Stelle im Word-Dokument platziert Bild.

Probe meiner Funktion:

public static string ParseDocxDocument(string pathToFile) 
    { 
     StringBuilder result = new StringBuilder(); 
     WordprocessingDocument wordProcessingDoc = WordprocessingDocument.Open(pathToFile, true); 
     List<ImagePart> imgPart = wordProcessingDoc.MainDocumentPart.ImageParts.ToList(); 
     IEnumerable<Paragraph> paragraphElement = wordProcessingDoc.MainDocumentPart.Document.Descendants<Paragraph>(); 
     int imgCounter = 0; 


     foreach (Paragraph par in paragraphElement) 
     { 

       //Add new paragraph tag 
       result.Append("<div style=\"width:100%; text-align:"); 

       //Append anchor style 
       if (par.ParagraphProperties != null && par.ParagraphProperties.Justification != null) 
        switch (par.ParagraphProperties.Justification.Val.Value) 
        { 
         case JustificationValues.Left: 
          result.Append("left;"); 
          break; 
         case JustificationValues.Center: 
          result.Append("center;"); 
          break; 
         case JustificationValues.Both: 
          result.Append("justify;"); 
          break; 
         case JustificationValues.Right: 
         default: 
          result.Append("right;"); 
          break; 
        } 
       else 
        result.Append("left;"); 

       //Append text decoration style 
       if (par.ParagraphProperties != null && par.ParagraphProperties.ParagraphMarkRunProperties != null && par.ParagraphProperties.ParagraphMarkRunProperties.HasChildren) 
        foreach (OpenXmlElement chield in par.ParagraphProperties.ParagraphMarkRunProperties.ChildElements) 
        { 
         switch (chield.GetType().Name) 
         { 
          case "Bold": 
           result.Append("font-weight:bold;"); 
           break; 
          case "Underline": 
           result.Append("text-decoration:underline;"); 
           break; 
          case "Italic": 
           result.Append("font-style:italic;"); 
           break; 
          case "FontSize": 
           result.Append("font-size:" + ((FontSize)chield).Val.Value + "px;"); 
           break; 
          default: break; 
         } 
        } 

       result.Append("\">"); 

       //Add image tag 
       IEnumerable<Run> runs = par.Descendants<Run>(); 
       foreach (Run run in runs) 
       { 
        if (run.HasChildren) 
        { 
         foreach (OpenXmlElement chield in run.ChildElements.Where(o => o.GetType().Name == "Picture")) 
         { 
          result.Append(string.Format("<img style=\"{1}\" src=\"data:image/jpeg;base64,{0}\" />", GetBase64Image(imgPart[imgCounter].GetStream()), 
              ((DocumentFormat.OpenXml.Vml.Shape)chield.ChildElements.Where(o => o.GetType().Name == "Shape").FirstOrDefault()).Style 
           )); 
          imgCounter++; 
         } 
        } 
       } 

       //Append inner text 
       IEnumerable<Text> textElement = par.Descendants<Text>(); 
       if (par.Descendants<Text>().Count() == 0) 
        result.Append("<br />"); 

       foreach (Text t in textElement) 
       { 
        result.Append(t.Text); 
       } 


       result.Append("</div>"); 
       result.Append(Environment.NewLine); 

     } 

     wordProcessingDoc.Close(); 

     return result.ToString(); 
    } 

Jetzt whant I Tisch im Text angeben (wie es in Word erscheinen).

Final:

Ok, jeder, habe ich herausgefunden habe. In meiner Beispielfunktion ein großer Fehler. Ich zähle Absatzelemente des Dokuments Body. Tabellen befinden sich auf derselben Ebene wie Absatz, daher ignorieren Tabellen Tabellen. Also müssen wir Elemente des Dokumentkörpers aufzählen.

Hier ist mein Test-Funktion, um eine korrekte HTML von docx zu erzeugen (es ist nur Test-Code, so dass es nicht sauber)

public static string ParseDocxDocument(string pathToFile) 
    { 
     StringBuilder result = new StringBuilder(); 
     WordprocessingDocument wordProcessingDoc = WordprocessingDocument.Open(pathToFile, true); 
     List<ImagePart> imgPart = wordProcessingDoc.MainDocumentPart.ImageParts.ToList(); 
     List<string> tableCellContent = new List<string>(); 
     IEnumerable<Paragraph> paragraphElement = wordProcessingDoc.MainDocumentPart.Document.Descendants<Paragraph>(); 
     int imgCounter = 0; 

     foreach (OpenXmlElement section in wordProcessingDoc.MainDocumentPart.Document.Body.Elements<OpenXmlElement>()) 
     { 
      if(section.GetType().Name == "Paragraph") 
      { 
       Paragraph par = (Paragraph)section; 
       //Add new paragraph tag 
       result.Append("<div style=\"width:100%; text-align:"); 

       //Append anchor style 
       if (par.ParagraphProperties != null && par.ParagraphProperties.Justification != null) 
        switch (par.ParagraphProperties.Justification.Val.Value) 
        { 
         case JustificationValues.Left: 
          result.Append("left;"); 
          break; 
         case JustificationValues.Center: 
          result.Append("center;"); 
          break; 
         case JustificationValues.Both: 
          result.Append("justify;"); 
          break; 
         case JustificationValues.Right: 
         default: 
          result.Append("right;"); 
          break; 
        } 
       else 
        result.Append("left;"); 

       //Append text decoration style 
       if (par.ParagraphProperties != null && par.ParagraphProperties.ParagraphMarkRunProperties != null && par.ParagraphProperties.ParagraphMarkRunProperties.HasChildren) 
        foreach (OpenXmlElement chield in par.ParagraphProperties.ParagraphMarkRunProperties.ChildElements) 
        { 
         switch (chield.GetType().Name) 
         { 
          case "Bold": 
           result.Append("font-weight:bold;"); 
           break; 
          case "Underline": 
           result.Append("text-decoration:underline;"); 
           break; 
          case "Italic": 
           result.Append("font-style:italic;"); 
           break; 
          case "FontSize": 
           result.Append("font-size:" + ((FontSize)chield).Val.Value + "px;"); 
           break; 
          default: break; 
         } 
        } 

       result.Append("\">"); 

       //Add image tag 
       IEnumerable<Run> runs = par.Descendants<Run>(); 
       foreach (Run run in runs) 
       { 
        if (run.HasChildren) 
        { 
         foreach (OpenXmlElement chield in run.ChildElements.Where(o => o.GetType().Name == "Picture")) 
         { 
          result.Append(string.Format("<img style=\"{1}\" src=\"data:image/jpeg;base64,{0}\" />", GetBase64Image(imgPart[imgCounter].GetStream()), 
              ((DocumentFormat.OpenXml.Vml.Shape)chield.ChildElements.Where(o => o.GetType().Name == "Shape").FirstOrDefault()).Style 
           )); 
          imgCounter++; 
         } 
         foreach (OpenXmlElement table in run.ChildElements.Where(o => o.GetType().Name == "Table")) 
         { 
          result.Append("<strong>HERE'S TABLE</strong>"); 
         } 
        } 
       } 

       //Append inner text 
       IEnumerable<Text> textElement = par.Descendants<Text>(); 
       if (par.Descendants<Text>().Count() == 0) 
        result.Append("<br />"); 

       foreach (Text t in textElement.Where(o=>!tableCellContent.Contains(o.Text.Trim()))) 
       { 
        result.Append(t.Text); 
       } 


       result.Append("</div>"); 
       result.Append(Environment.NewLine); 

      } 
      else if (section.GetType().Name=="Table") 
      { 
       result.Append("<table>"); 
       Table tab = (Table)section; 
       foreach (TableRow row in tab.Descendants<TableRow>()) 
       { 
        result.Append("<tr>"); 
        foreach (TableCell cell in row.Descendants<TableCell>()) 
        { 
         result.Append("<td>"); 
         result.Append(cell.InnerText); 
         tableCellContent.Add(cell.InnerText.Trim()); 
         result.Append("</td>"); 
        } 
        result.Append("</tr>"); 
       } 
       result.Append("</table>"); 
      }     
     } 


     wordProcessingDoc.Close(); 

     return result.ToString(); 
    } 

    private static string GetBase64Image(Stream inputData) 
    { 
     byte[] data = new byte[inputData.Length]; 
     inputData.Read(data, 0, data.Length); 
     return Convert.ToBase64String(data); 
    } 

Antwort

1

Versuchen nach der ersten Tabelle im Dokument zu finden.

Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First(); 
+0

Ich habe wissen, wie Tabellen zu lesen und zu analysieren. Meine Frage ist, wie man innenposition Text findet – EkzoMan

+0

Ich habe meinen Arbeitscode hinzugefügt. Dein Beitrag gibt mir die richtige Richtung zur Arbeit, also markiere ich deine Antwort als richtig – EkzoMan