2012-06-28 38 views
5

Tôi có một vấn đề với phân tích cú pháp tài liệu * .docx bằng OpenXML (C#).Trích xuất bảng từ DOCX

Vì vậy, đây là các bước của tôi:
1. Nạp tài liệu * .docx
2. Nhận các danh sách các đoạn
3. Trong mỗi cái nhìn đoạn cho văn bản, hình ảnh và bảng yếu tố
4. Đối với mỗi văn bản và yếu tố hình ảnh tạo thẻ html
5. Lưu đầu ra dưới dạng * .html tệp

Tôi đã tìm ra cách định vị tệp hình ảnh trong tài liệu và trích xuất nó. Bây giờ có một bước để đi - tìm vị trí bảng trong văn bản (đoạn văn).

Nếu có ai biết cách định vị bảng trong tài liệu * .docx bằng OpenXML, vui lòng trợ giúp. Cảm ơn.

Bổ sung: Ok, có thể tôi không rõ ràng giải thích ý tôi là gì. Nếu chúng tôi nhận được nội dung của đoạn văn, bạn có thể tìm thấy các đối tượng chield như textblocks, hình ảnh và như vậy. Vì vậy, nếu đoạn chứa chạy có chứa hình ảnh nó có nghĩa là ở nơi này trong tài liệu Word đặt hình ảnh.

Mẫu chức năng của tôi:

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(); 
    } 

Bây giờ tôi whant để xác định vị trí bảng trong văn bản (như nó xuất hiện trong Word).

Cuối cùng:

Ok, mọi người, tôi đã phát hiện ra. Trong chức năng mẫu của tôi một sai lầm lớn. Tôi liệt kê các phần tử đoạn văn của phần thân Tài liệu. Các bảng có cùng mức như đoạn văn, vì vậy các bảng bỏ qua chức năng. Vì vậy, chúng ta cần liệt kê các yếu tố của cơ thể tài liệu.

Dưới đây là chức năng thử nghiệm của tôi để tạo ra HTML đúng từ docx (nó chỉ là mã kiểm tra, vì vậy nó không sạch)

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); 
    } 

Trả lời

1

Hãy thử sau đây để tìm bảng đầu tiên trong tài liệu.

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

Tôi đã biết cách đọc và phân tích bảng. Câu hỏi của tôi là làm thế nào để tìm thấy văn bản inssition insite – EkzoMan

+0

Tôi đã thêm mã làm việc của mình. Bài đăng của bạn cung cấp cho tôi đúng hướng để làm việc, vì vậy tôi đánh dấu câu trả lời của bạn là chính xác – EkzoMan

Các vấn đề liên quan