Excel2Word

When I add a table to the end of a document, it always inserts a paragraph before the table. Is there any way to suppress it? Here is the code I am using…

private void AddTable(Worksheet excelWorksheet, Document doc, int sectionfirstrow, int sectionlastrow, int rowStartIndex, int columnStartIndex, int newColumnStartIndex, ArrayList tablePartList)
{
    // Create a new Word table
    Table wordsTable = new Table(doc);
    // Loop through rows in the Excel worksheet
    for (int rowIndex = sectionfirstrow; rowIndex <= sectionlastrow; rowIndex++)
    {
        // Create new row
        Aspose.Words.Tables.Row wordsRow = new Aspose.Words.Tables.Row(doc);
        // Get collection of Excel cells
        Aspose.Cells.Cells cells = excelWorksheet.Cells;

        // Set height of current row
        wordsRow.RowFormat.Height = cells.GetRowHeightPixel(rowIndex) * 0.71; // 0.75;
        wordsRow.RowFormat.HeightRule = HeightRule.Exactly;
        // apply centering to the row as needed
        if (excelWorksheet.PageSetup.CenterHorizontally)
            wordsRow.RowFormat.Alignment = RowAlignment.Center;
        // Append current row to current table.
        wordsTable.AppendChild(wordsRow);
        // Loop through columns and add columns to Word table while table's width < maxWidth
        for (int columnIndex = columnStartIndex; columnIndex < newColumnStartIndex; columnIndex++)
        {
            // Convert Excel cell to Word cell (cell width is set in ImportExcelCell)
            Aspose.Words.Tables.Cell wordsCell = ImportExcelCell(doc, cells, rowIndex, columnIndex);

            // Insert cell into the row
            wordsRow.AppendChild(wordsCell);
        }
    }
    // Add Word table to ArrayList
    tablePartList.Add(wordsTable);
}

Hi
Thanks for your inquiry. This code just create list of tables that will be inserted into the document. You can try use InsertBefore method to insert tables before paragraph.
Best regards.

Sorry about that :(. Here’e the code I am working with. It works except that now it is ignoring my pagebreaks. The pagebreak was supposed to be before the words ‘Basis of Estimate’. See attached doc.

// Insert all tables and page-breaks into the Word document
// the table_break_items should always be one less or the same as the numbers of tables since that is the criteria for breaking the table into table parts
int table_break_item_index = 0;
for (int table_index = tablePartsArray.Count - 1; table_index >= 0; table_index--) // these were created in reverse order (in GetTablePart) so we have to roll them out again in reverse order
{
    // Move cursor to document end
    builder.MoveToDocumentEnd();
    // Insert table
    builder.CurrentSection.Body.InsertBefore((Node)tablePartsArray[table_index], builder.CurrentParagraph);
    // builder.CurrentSection.Body.AppendChild((Node)tablePartsArray[table_index]);
    // Move cursor to document end
    builder.MoveToDocumentEnd();
    // insert the document or page-break
    if (table_break_items.Count > 0 && table_break_item_index < table_break_items.Count)
    {
        table_break tb = (table_break)table_break_items.GetByIndex(table_break_item_index);
        if (tb.tablebreaktype == table_break_type.document)
            AppendDoc(doc, GetSubReportDocument(tb.doc));
        else
            builder.InsertBreak(BreakType.PageBreak);
        table_break_item_index++;
    }
}

Hi
Thanks for your inquiry. Please try using the following code instead yours.

// Insert all tables and page-breaks into the Word document
// the table_break_items should always be one less or the same as the numbers of tables since that is the criteria for breaking the table into table parts
int table_break_item_index = 0;
for (int table_index = tablePartsArray.Count - 1; table_index >= 0; table_index--) // these were created in reverse order (in GetTablePart) so we have to roll them out again in reverse order
{
    // Move cursor to document end
    builder.MoveToDocumentEnd();
    // Insert table
    builder.CurrentSection.Body.InsertBefore((Node)tablePartsArray[table_index], builder.CurrentParagraph);
    // Move cursor to document end
    builder.MoveToDocumentEnd();
    // insert the document or page-break
    if (table_break_items.Count > 0 && table_break_item_index < table_break_items.Count)
    {
        table_break tb = (table_break)table_break_items.GetByIndex(table_break_item_index);
        if (tb.tablebreaktype == table_break_type.document)
            AppendDoc(doc, GetSubReportDocument(tb.doc));
        else
            builder.InsertBreak(BreakType.SectionBreakNewPage);
        table_break_item_index++;
    }
}

I highlighted my changes.
I hope this could help you.
Best regards.

Works beautifully. Thank you so much for all of your help!
- John