Word 문서의 테이블에서 빈 행을 삭제하는 방법
                                            
                                                
                                                
                                                
                                                
                                                
                                                 3333 단어  csharpasposewords
                    
       //load the document
       Document doc = new Document(path+ "DocumentWithEmpty Rows.docx");
       //get all rows
       Node[] rows = doc.GetChildNodes(NodeType.Row, true).ToArray();
       //iterate throughthe rows and check if all the cells are empty
       foreach (Row row in rows)
       {
           bool removeRow = true;
           foreach (Cell cell in row.Cells)
           {
               if (cell.FirstParagraph != null)
                   removeRow = !cell.FirstParagraph.HasChildNodes;
           }
           //if all the cells in the row are empty remove the row
           if (removeRow)
               row.Remove();
       }
       doc.Save(MyDir + "DocumentWithoutEmptyRows.docx");
이 스니펫은 더 간단한 DOC(X) 문서에서 잘 작동하며 기본 요구 사항을 충족합니다.
그러나 블록(문서 내)에 책갈피 노드가 있는 경우 제공된 스니펫에서 예외가 발생합니다. 이를 피하기 위해 문서를 로드하기 전에 두 줄의 코드를 더 추가하고 첫 번째 줄에서 수정합니다.
       LoadOptions options = new LoadOptions();
       options.AnnotationsAtBlockLevel = false;
       //load the document
       Document doc = new Document(path+ "DocumentWithEmpty Rows.docx", options);
다른 잠재적인 문제는 다음과 같습니다. "일부 셀에 빈 공간이 있거나 줄 바꿈 문자/캐리지 리턴이 포함되어 있거나 일부 행이 셀 내에 중첩되어 있는 경우 등은 어떻습니까?"다음 코드 조각은 셀 끝에 있는 모든 불필요한 기호(공백 및 줄 바꿈 문자)를 제거하고 행 내의 모든 셀이 비어 있으면 행을 삭제합니다.
       LoadOptions options = new LoadOptions();
       options.AnnotationsAtBlockLevel = false;
        //load the document
       Document doc = new Document(path+ "DocumentWithEmpty Rows.docx", options);
       Words.Document doc = new Words.Document(MyDir + "Original_MultiRow_Columns - Copy.docx");
       Words.SectionCollection sections = doc.Sections;
       foreach (Words.Section section in sections)
           if (section != null)
               foreach (Words.Tables.Table t in section.Body.Tables)
                   foreach (Row r in t.Rows)
                   {
                       bool removeRow = true;
                       foreach (Cell c in r.Cells)
                       {
                           Node[] paragraphs = c.GetChildNodes(NodeType.Paragraph, true).ToArray();
                           for (int i = paragraphs.Length - 1; i >= 0; i--)
                           {
                               Words.Paragraph p = (Words.Paragraph)paragraphs[i];
                               if (p.ToString(SaveFormat.Text).Trim() == "")
                               {
                                   p.Remove();
                               }
                               else
                               {
                                   removeRow = false;
                                        break;
                               }
                           }
                       }
                       if (removeRow)
                           r.Remove();
                   }
       doc.Save(MyDir + "output20.3v1.docx")
이 짧은 글이 누군가에게 도움이 되었으면 합니다.
Reference
이 문제에 관하여(Word 문서의 테이블에서 빈 행을 삭제하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/drazanjarak/how-to-delete-empty-rows-from-tables-in-a-word-document-2f5i텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)