locked
Novacode DOCX create table in word from datatable RRS feed

  • Question

  • User181930479 posted

    im trying to loop over the datatable and create word table , by far if i have 3 rows in the datatable they are being inserted into the first row of my microsoft word table

    below is my code :

        protected void Button2_Click(object sender, EventArgs e)
        {
    
            PullData();
    
    
          
                 gvd2.DataSource = dataTable;
            gvd2.DataBind();
    
    
            // Create a document.
            using (DocX document = DocX.Create(@"D:\Test.docx"))
            {
                // Add a Table to this document.
                Novacode.Table t = document.AddTable(2, 3);
                // Specify some properties for this Table.
                t.Alignment = Alignment.center;
                t.Design = TableDesign.MediumGrid1Accent2;
                // Add content to this Table.
    
                // Add content to this Table.
                t.Rows[0].Cells[0].Paragraphs.First().Append("A");
    
                //foreach (DataRow row in dataTable.Rows)
                //{
                 
                //    t.Rows[1].Cells[0].Paragraphs.First().Append(row["IssueSubjectType"].ToString());
                //}
    
    
    
                // Insert the Table into the document.
                document.InsertTable(t);
    
                // Save the Document.
                document.Save();
                // Release this document from memory.
                document.Dispose();
            }
    
        }
    
    
    
        private DataTable dataTable = new DataTable();
    
      
    
        //  method to pull data from database to datatable   
        public void PullData()
        {
            using (SqlConnection sqlConn = new SqlConnection("Data Source=.;Initial Catalog=UAE_OG-Interanl;Integrated Security=True"))
            {
                string sqlQuery = @"SELECT IssueSubjectType from tbl_IssueStoPublicate WHERE IssueNumber = '625'  order by IssueSubjectOrder desc";
                using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
                {
                    SqlDataAdapter ds = new SqlDataAdapter(cmd);
                    ds.Fill(dataTable);
                }
            }
        }
    

    I have trying looping as well but im getting , index out of range :(

    // Loop through the rows in the Table and insert data from the data source.
    for (int row = 1; row < t.RowCount; row++)
                {
                    for (int cell = 0; cell < t.Rows[row].Cells.Count; cell++)
                    {
                        Paragraph cell_paragraph =t.Rows[row].Cells[cell].Paragraphs[0];
                        cell_paragraph.InsertText(dataTable.Rows[row - 1].ItemArray[cell].ToString(), false);
                    }
                }

    any help to solve this would be a life saviour .....

    Wednesday, April 4, 2018 2:48 PM

All replies

  • User-166373564 posted

    Hi NAF,

    From your sql query , you will get one DataTable which has one column `IssueSubjectType` , that's why you get the `index out of range` error :

                    for (int row = 1; row < t.RowCount; row++)
                    {
                        for (int cell = 0; cell < t.Rows[row].Cells.Count; cell++)
                        {
                            Paragraph cell_paragraph = t.Rows[row].Cells[cell].Paragraphs.First();
                            cell_paragraph.InsertText(dataTable.Rows[row - 1].ItemArray[cell].ToString(), false);
                        }
                    }

    If you have 3 rows ,  `t.Rows[row].Cells.Count` is 3 , then in the second loop ,  dataTable.Rows[0].ItemArray[1].ToString() will threw the error since you only one column in your datatable .

    Best Regards,

    Angie

    Thursday, April 5, 2018 6:02 AM
  • User181930479 posted

    please advise how to fix that

    Thursday, April 5, 2018 6:05 AM