DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Exporting a DataTable from WinForms to a docx file takes a lot of time

Open valimaties opened this issue 2 years ago • 4 comments

Hi. I've just updated from 2.3.0 to 2.4.0, because I just want to use the new parameter from Section page numeration. But now the exporting process takes a lot of time, and I don't know why, because I'm using the same code as I used on the previous version. What happened with the new version?

Edit: More than that, my all table is empty. Seems none of the paragraphs inside the table object are filled. But is the same code as in 2.3.0, what has been changed?

OK. I've started debugging this thing and I found the following: After each line of code which is using some appending operation, or anything that involve an object on the file, I get the following lines of information in debugger:

This document contains more than 1 section. Consider using Sections[wantedSection].Headers.
This document contains more than 1 section. Consider using Sections[wantedSection].Headers.
This document contains more than 1 section. Consider using Sections[wantedSection].Headers.
This document contains more than 1 section. Consider using Sections[wantedSection].Footers.
This document contains more than 1 section. Consider using Sections[wantedSection].Footers.
This document contains more than 1 section. Consider using Sections[wantedSection].Footers.

My code is the following, what I'm doing wrong?

DocX doc = DocX.Create(filePath, DocumentTypes.Document);
while (doc.Sections.Count < numeGrup.Count)
{
    // I need to insert sections for each group in "numeGrup" list
    doc.InsertSection();
}
var sec = doc.Sections[0];
for (int k = 0; k < numeGrup.Count; k++)
{
    sec = doc.Sections[k];
    sec.PageLayout.Orientation = Orientation.Landscape;
    sec.PageNumberType.PageNumberStart = 1;
    sec.PageWidth = 800;
    sec.MarginLeft = 20;
    sec.MarginRight = 20;
    sec.MarginTop = 20;
    sec.MarginBottom = 20;
    sec.InsertParagraph(numeGrup[k].Key.Nume).SpacingBefore(20d).Font("Arial").FontSize(10).Bold().SpacingAfter(10d);
    var table = doc.AddTable(detaliiPeFirma.Count() + 1 + groups.Count, 9);
    table.SetTableCellMargin(TableCellMarginType.left, 1);
    table.SetTableCellMargin(TableCellMarginType.right, 1);
    table.SetTableCellMargin(TableCellMarginType.top, 1);
    table.SetTableCellMargin(TableCellMarginType.bottom, 1);
    var p = sec.InsertParagraph();
    p.InsertTableAfterSelf(table);
    table.Design = TableDesign.LightGrid;
    var headerRow = table.Rows.First();
    headerRow.TableHeader = true;
    headerRow.Cells[0].Paragraphs[0].Append("Field1").Bold().FontSize(10);
    headerRow.Cells[1].Paragraphs[0].Append("Field2").Bold().FontSize(10);
    headerRow.Cells[2].Paragraphs[0].Append("Field3").Bold().FontSize(10);
    headerRow.Cells[3].Paragraphs[0].Append("Field4").Bold().FontSize(10);
    headerRow.Cells[4].Paragraphs[0].Append("Field5").Bold().FontSize(10);
    headerRow.Cells[5].Paragraphs[0].Append("Field6").Bold().FontSize(10);
    headerRow.Cells[6].Paragraphs[0].Append("Field7").Bold().FontSize(10);
    headerRow.Cells[7].Paragraphs[0].Append("Field8").Bold().FontSize(10);
    headerRow.Cells[8].Paragraphs[0].Append("Field9").Bold().FontSize(10);
}

I put only a part of my code, the generation of tables and filling of headers of the tables. But no paragraph inside table's cells are filled. And it's working very slow now! Maybe because of those messages in the debugger?

valimaties avatar Jan 20 '23 11:01 valimaties

No feedback about this issue? Seems that first section is filled very fast, but starting with the second section, the fill of the table is very slow. No one encountered this issue? Am I doing something wrong?

valimaties avatar Jan 25 '23 09:01 valimaties

Hi,

Try using the table insertion in the docx after filling the table's Paragraph : var p = sec.InsertParagraph(); p.InsertTableAfterSelf( table ); after the Append of "FieldX".

It should works.

I don't see this table filling being slow. Thank you.

XceedBoucherS avatar Feb 09 '23 21:02 XceedBoucherS

I've tried your solution, but the table is append with all those rows as empty rows. But if I add the table in the doc before starting the population-loop, the paragraphs are displaying correctly after opening the docx file. Only that the populating job is very slow for the second section. And even slower for the next section, and so on... Please make a test, with some random data. Fill a table with 1000 records, and group them in three sections, like three sub-tables. And try to populate after that a docx table for each datatable, in separate sections. For me it works very slow from the second section to the last one. And if I don't find a solution, I must find another way to save the data to a docx file, unfortunatelly, because I cannot wait 5 minutes for 1000 rows in a datatable. Is too much for me. 🙏

PS: I think something is happening with the latest version, because if I remember well, the version before didn't had the same lag.

valimaties avatar Feb 11 '23 22:02 valimaties

Hi, I agree with you, 5 min of waiting is not acceptable. There could be something different in the latest version.

Could you build up the test you are talking about in the preceding post? This way, I would test the same sample as you. When I test something like the initial post of this thread, I don't see anything slow. Here's my sample : `var doc = DocX.Create( "test.docx" ); var counter = -1; while( ++counter < 4 ) { doc.InsertSection(); } var sec = doc.Sections[ 0 ]; for( int k = 0; k < 4; k++ ) { sec = doc.Sections[ k ]; sec.PageLayout.Orientation = Orientation.Landscape; sec.PageNumberType.PageNumberStart = 1; sec.PageWidth = 800; sec.MarginLeft = 20; sec.MarginRight = 20; sec.MarginTop = 20; sec.MarginBottom = 20; sec.InsertParagraph( "test" + k ).SpacingBefore( 20d ).Font( "Arial" ).FontSize( 10 ).Bold().SpacingAfter( 10d ); var table = doc.AddTable( 2, 9 ); table.SetTableCellMargin( TableCellMarginType.left, 1 ); table.SetTableCellMargin( TableCellMarginType.right, 1 ); table.SetTableCellMargin( TableCellMarginType.top, 1 ); table.SetTableCellMargin( TableCellMarginType.bottom, 1 );

    //table.Design = TableDesign.LightGrid;
    var headerRow = table.Rows.First();
   // headerRow.TableHeader = true;
    headerRow.Cells[ 0 ].Paragraphs[ 0 ].Append( "Field1" ).Bold().FontSize( 10 );
    headerRow.Cells[ 1 ].Paragraphs[ 0 ].Append( "Field2" ).Bold().FontSize( 10 );
    headerRow.Cells[ 2 ].Paragraphs[ 0 ].Append( "Field3" ).Bold().FontSize( 10 );
    headerRow.Cells[ 3 ].Paragraphs[ 0 ].Append( "Field4" ).Bold().FontSize( 10 );
    headerRow.Cells[ 4 ].Paragraphs[ 0 ].Append( "Field5" ).Bold().FontSize( 10 );
    headerRow.Cells[ 5 ].Paragraphs[ 0 ].Append( "Field6" ).Bold().FontSize( 10 );
    headerRow.Cells[ 6 ].Paragraphs[ 0 ].Append( "Field7" ).Bold().FontSize( 10 );
    headerRow.Cells[ 7 ].Paragraphs[ 0 ].Append( "Field8" ).Bold().FontSize( 10 );
    headerRow.Cells[ 8 ].Paragraphs[ 0 ].Append( "Field9" ).Bold().FontSize( 10 );

    var p = sec.InsertParagraph();
    p.InsertTableAfterSelf( table );
  }

  doc.Save();`

Thank you for your help

XceedBoucherS avatar Feb 14 '23 20:02 XceedBoucherS