Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to add a new page every time I run the code and then paste data to that new page using Xceed's DocX dll.

What I have tried:

string fileName=@"F:\DataGridView.docx";

if (File.Exists(fileName))
{
    using( var document = DocX.Load( fileName ) )
    {
        document.InsertSectionPageBreak(false);

        document.AddHeaders();


        Paragraph para=document.InsertParagraph();
        para.Append("_______________________________");
        Paragraph para2=document.InsertParagraph();


        Header header_default = document.Headers.First;
        Paragraph p1 = header_default.InsertParagraph();
        p1.Append("TEST HEADER").Bold().Font("Arial").Color(Color.Blue);
        p1.Alignment=Alignment.center;
        p1.FontSize(18);
        p1.Append("                                       Date:  "+DateTime.Today.ToString("dd-MM-yyyy"));
        p1.Alignment=Alignment.right;


        var table = document.AddTable( dataGridView1.SelectedRows.Count+2, dataGridView1.Columns.Count );
        table.Design = TableDesign.TableGrid;
        table.Alignment=Alignment.center;

        int rowNumber = 0;

        int columnNumber = 0;
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(column.HeaderText);
            columnNumber++;
        }

        rowNumber++;

        foreach (DataGridViewRow row in dataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index))
        {
            columnNumber = 0;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(row.Cells[columnNumber].Value.ToString());
                columnNumber++;
            }

            rowNumber++;
        }

        decimal total = dataGridView1.SelectedRows.OfType<DataGridViewRow>()
            .Sum(tt => Convert.ToDecimal(tt.Cells[1].Value));

        table.Rows[dataGridView1.SelectedRows.Count+1].Cells[1].Paragraphs.First().Append("Total :").Bold(true);
        table.Rows[dataGridView1.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(total.ToString()).Bold(true);

        document.InsertTable(table);

        document.Save();
    }
}

I'm getting error
System.InvalidOperationException: Sequence contains no elements
on the line
Paragraph para=document.InsertParagraph();


How do I fix this?
Posted
Comments
Richard Deeming 12-Apr-21 5:47am    
Sounds like a bug in the library. Try reporting it to Xceed:
Issues · xceedsoftware/DocX · GitHub[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900