Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sun Jul 18, 2021 6:27 am

Hi Team,

We need to identify the merged cells while reading/extracting the content of from the table inside word file. Our requirement is to extract the contents from the table and create the same in HTML form and to do so, we need to get the exact format of the table inside word file.

Currently, by default, it takes all the rows and columns as separate ones.

Please help.

Thanks in advance!!

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Mon Jul 19, 2021 10:30 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
Our Spire.Doc could identify the merged cells by the following code. If there is any question, please feel free to write back.
Code: Select all
Document doc = new Document();
doc.LoadFromFile("input.docx");
Table table = doc.Sections[0].Tables[0] as Table;
for (int i = 0; i < table.Rows.Count; i++)
{
    for (int j = 0; j < table.Rows[i].Cells.Count; j++)
    {
        TableCell cell = table[i, j];
        int columnSpan = cell.GridSpan;
        if (columnSpan > 1)
        {
            Console.WriteLine("The cell has column span and the number of merged cells is: " + columnSpan);
        }

        CellMerge rowSpan = cell.CellFormat.VerticalMerge;
        switch (rowSpan)
        {
            case CellMerge.Start:
                Console.WriteLine("The cell has row span and it's the first cell of the merged cell");
                break;
            case CellMerge.Continue:
                Console.WriteLine("The cell has row span and it's not the first cell of the merged cell");
                break;
        }
        if (columnSpan == 1 && rowSpan == CellMerge.None)
        {
            Console.WriteLine("The cell isn't merged cell");
        }
    }
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Mon Jul 19, 2021 12:57 pm

Hi Annika,

Thanks for the response.

I need the total number of merged cells in case of rowspan also like colspan. Below code gives the count for column merge in advance as soon as merge starts, but count is not working in case of row merge. Please help.

Code: Select all
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.TableCell;
import com.spire.doc.TableRow;
import com.spire.doc.collections.CellCollection;
import com.spire.doc.collections.DocumentObjectCollection;
import com.spire.doc.collections.RowCollection;
import com.spire.doc.documents.CellMerge;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.interfaces.IDocumentObject;

@SuppressWarnings({"unchecked", "static-access"})
public class TableHandlerWithoutHTML {

   
   public static void main(String[] args) {
      
      Document document = new Document();
      document.loadFromFile("NestedTablesWithMerge.docx");
      
      Section section = document.getSections().get(0);
      DocumentObject bodyData = section.getChildObjects().get(0);
      
      if(bodyData.getDocumentObjectType().equals(DocumentObjectType.Body)) {
         DocumentObjectCollection bodyCollection = bodyData.getChildObjects();
         for(IDocumentObject object : (Iterable<IDocumentObject>) bodyCollection) {
            if(object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
               Paragraph paragraph = (Paragraph) object;
               System.out.println(paragraph.getText());
            }
            
            if(object.getDocumentObjectType().equals(DocumentObjectType.Table)) {
               Table table = (Table) object;
               RowCollection rows = table.getRows();
               for(int i=0; i < rows.getCount(); i++) {
                  TableRow row = rows.get(i);
                  CellCollection cells = row.getCells();
                  for(int j=0; j < cells.getCount(); j++) {
                     TableCell cell = table.get(i, j);
                     System.out.println("Total Number of merged cells: " + cell.getGridSpan());
                     CellMerge rowSpan = cell.getCellFormat().getVerticalMerge();
                     if(rowSpan == rowSpan.Start) {
                        System.out.println("1st Cell of merge");
                     }
                     else if(rowSpan == rowSpan.Continue) {
                        System.out.println("Not first cell of merged region");
                     }
                     else {
                        System.out.println("Not merged");
                     }
                     
                     DocumentObjectCollection cellObjects = cell.getChildObjects();
                     for(int k=0; k < cellObjects.getCount(); k++) {
                        DocumentObject cellObject = cellObjects.get(k);
                        if(cellObject.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
                           Paragraph paragraph = (Paragraph) cellObject;
                           System.out.println(paragraph.getText());
                        }
                     }
                  }
               }
            }
         }
      }      
   }
}


I expect the count as 2 when it reaches to cell table.get(1, 4)

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Tue Jul 20, 2021 10:14 am

Hi,

Thanks for your feedback.
Please refer to the following code to calculate the number of merged rows in your Word sample.
Code: Select all
     public static void main(String[] args) {
        Document document = new Document();
        document.loadFromFile("NestedTablesWithMerge.docx");
        //document.saveToFile("out.html",FileFormat.Html);

        for (Section section : (Iterable<? extends Section>) document.getSections()) {

            for (DocumentObject object : (Iterable<? extends DocumentObject>) section.getBody().getChildObjects()) {
                if (object instanceof Table) {
                    Table table = section.getTables().get(0);
                    getSpan(table);
                }
            }
        }
    }
    private static void getSpan(Table table) {
        RowCollection rows = table.getRows();
        for (int i = 0; i < rows.getCount(); i++) {
            CellCollection cols = rows.get(i).getCells();
            for (int j = 0; j < cols.getCount(); j++) {
                TableCell cell = table.get(i, j);
                //Get colSpan
                int colSpan = cell.getGridSpan();
                if (colSpan > 1) {
                    System.out.println("cell[" + i + "," + j + "]---colSpan: " + colSpan);
                }
                //Get rowSpan
                if (cell.getCellFormat().getVerticalMerge().equals(CellMerge.Start)) {
                    int num = 1;
                    for (int k = i; k <rows.getCount()-1 ; k++) {
                        if(table.get(k, j).getCellFormat().getVerticalMerge().equals(CellMerge.Continue))
                        {
                            num++;
                        }
                    }
                    if(num>1)
                    {
                        System.out.println("cell[" + i + "," + j + "]---rowSpan: " + num);
                    }
                }
            }
        }
    }

In previous post, you mentioned that your final goal is to create the same in HTML. Actually, our Spire.Doc provides a method to convert Word to HTML, sample code is given as below. Maybe it is more convenient to implement your need. Hope it helps you.
Code: Select all
Document document = new Document();
document.loadFromFile("NestedTablesWithMerge.docx");
document.saveToFile("out.html",FileFormat.Html);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Jul 21, 2021 12:15 pm

Thanks Annika for your assistance on getting this done.

Still there is some problem with the count and it doesn't behave properly when we have both rowspan and colspan regions start in same row. Please refer the doc attached.

I expect the count when it reaches to second row as below (example table is of size 5x4):
Total Merged Columns: 1
Total Merged Rows: 1
Total Merged Columns: 1
Total Merged Rows: 2
Total Merged Columns: 2
Total Merged Rows: 1
Total Merged Columns: <IDEALLY THIS PRINT SHOULD BE SKIPPED SINCE IT WAS HANDLED IN ABOVE COLSPAN, PLEASE CONFIRM>
Total Merged Rows: 1
Total Merged Columns: 1
Total Merged Rows: 2

Count for the last column of second row is coming as 1 instead of 2. Please advise.

Code: Select all
Table table = (Table) object;
RowCollection rows = table.getRows();
for(int i=0; i < rows.getCount(); i++) {
   TableRow row = rows.get(i);
   CellCollection cells = row.getCells();
   for(int j=0; j < cells.getCount(); j++) {
      TableCell cell = table.get(i, j);
      int rowSpanCount = 1;
      int columnSpanCount = cell.getGridSpan();
      System.out.println("Total Merged Columns: " + columnSpanCount);
      if (cell.getCellFormat().getVerticalMerge().equals(CellMerge.Start)) {
             for (int k = i; k <rows.getCount()-1 ; k++) {
                               if(table.get(k, j).getCellFormat().getVerticalMerge().equals(CellMerge.Continue)) {
                           rowSpanCount++;
                }
         }
         if(rowSpanCount > 1) {
                                System.out.println("Total Merged Rows: " + rowSpanCount);
         }
                 }
       }
}


Additionally, thanks for the code given for the DOCX TO HTML, but our requirement is to extract the partial content which is HTML formatted and make it in HTML form outside. In our case, same document could contain both HTML and Non-HTML (MS Word objects like paragraphs, tables etc.) contents on it.

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Thu Jul 22, 2021 9:52 am

Hello,

Thank you for your feedback.
I will investigate your requirement and give you a feedback as soon as possible.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Jul 23, 2021 9:58 am

Hello,

Thanks for your patience.
Please refer to the code in the attachment to calculate the number of rows merged in a cell.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Mon Jul 26, 2021 7:43 am

Hi Annika,

Thank you so much. The solution works perfect as per the requirement.

Appreciate your effort and assistance.

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Mon Jul 26, 2021 9:56 am

Hello,

You are welcome.
If you have other questions about using Spire.Doc in the future, please feel free to contact us. Wish you all the best.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc