We're sorry Aspose doesn't work properply without JavaScript enabled.

How I can split Tabel to multi table based columns number

Hi guys,
I have a table created inside a word file by HTML and this table has a large number of columns,
how can I split the table into more than one table by a specified number of columns?
Scenario :
My table has only 20 columns I want to split it into three tables each table have 9 columns
Main Tabel - with cols 1,2,3,4…20…etc
Result
Table 1 - with cols 1,2,3,4, …9.
Table 2 - with cols 1,2,3,4, …9.
Table 3- with cols 1,2,
please note that I use Aspose .Net word (c#)
Thanks, and regards,

@JassarMahmoud In MS Word documents as well as in Aspose.Words DOM the rows in the tables are independent, i.e. each row can have different number of cells. So there is not column concept. However, you can emulate the work with columns using Aspose.Words API, For example see how to work with table columns using Aspose.Words.
in your case, you can clone the original table and remove unnecessary columns from the original table and from it’s copy.

1 Like

Do you have an example of a similar situation? I’ve tried many ways, but unfortunately we haven’t succeeded yet
Thank you alot for your response

@JassarMahmoud You can use code like the following:

int columnsPerPart = 3;

Document doc = new Document(@"C:\Temp\in.docx");

// Get the table that needs to be split.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// Suppose all rows in the table contains the same number of cells,
// in this case number of cells in the first column is equal the number of columns in the table.
int columns = table.FirstRow.Cells.Count;

// Caluclate number of the original table clones.
int tablesCount = (int)System.Math.Ceiling((double)columns / columnsPerPart);

// Clone the original talbe required number of times.
ArrayList tables = new ArrayList();
Node currentTable = table;
for (int i = 0; i < tablesCount - 1; i++)
{
// Insert a delimiter paragraph between the table parts.
Paragraph delimiter = new Paragraph(doc);
currentTable.ParentNode.InsertAfter(delimiter, currentTable);
// And the clonned table.
currentTable = table.Clone(true);
delimiter.ParentNode.InsertAfter(currentTable, delimiter);

}

for (int i = 0; i < tablesCount; i++)
{
int columnStartIndex = i * columnsPerPart;
Table t = (Table)tables[i];

// Remove columns before the start index.
for (int colIdx = 0; colIdx < columnStartIndex; colIdx++)
{
Column column = Column.FromIndex(t, 0);
column.Remove();
}

// And the columns at the end.
while (t.FirstRow.Cells.Count > columnsPerPart)
{
Column column = Column.FromIndex(t, t.FirstRow.Cells.Count-1);
column.Remove();
}
}

doc.Save(@"C:\Temp\out.docx");


The Column class is available here.

1 Like

@alexey.noskov
thank you for your response, really appreciate that
your code works with some table
but I have a different and difficult Scenario
I will leave a file explaining the problem, I hope you will be able to solve it.
Thank you so much again
best regardssampel.docx (37.4 KB)

**Scenario 2: ** please look at the sampel.docx (37.4 KB)
attachment

@JassarMahmoud The both sample documents are the same. And demonstrates the difficulty I have mentioned earlier - rows in MS Word tables are independent and each row can contain different number of cells.
In your particular case you can The last 11 cells in each row can be left for each part of the split table. The first and the last 8 rows must be processed separately from the main table content because they contain different number of cells. The rest table’s content can be processed the similar way I suggested in my previous answer.
Unfortunately, it is difficult to provide you an universal code that with cover all possible cases.

1 Like

@alexey.noskov
Thank you for your response and efforts
I’ll try solve it,
If I succeed I’ll share the solve with you
Regards