Search code examples
c#sqlasp.netloopsnovacode-docx

DocX looping table for unknown amount of rows


I have recently installed DocX for asp.net and have encountered an issue for create a loop for an unknown amount of rows.

I am creating a report for users coming from a SQL server. A section of this is to do with history, in the database for each report there is a different amount of rows for each report that is created. is there a way to create a loop or similar to output this unknown amount of rows?

for (int i = 0; i <= 2; i++)
{
    t.Rows[i].Cells[0].Paragraphs.First().Append(Data from SQL);
}

t.Alignment = Alignment.center;
t.Design = TableDesign.TableGrid;

t.Rows[0].Cells[0].Paragraphs.First().Append("Update Date");
t.Rows[0].Cells[1].Paragraphs.First().Append("Update By");
t.Rows[0].Cells[2].Paragraphs.First().Append("Code");
t.Rows[0].Cells[3].Paragraphs.First().Append("Status/Description");
t.Rows[0].Cells[4].Paragraphs.First().Append("System");

t.Rows[1].Cells[0].Paragraphs.First().Append("update_date");
t.Rows[1].Cells[1].Paragraphs.First().Append("update_by");
t.Rows[1].Cells[2].Paragraphs.First().Append("codes");
t.Rows[1].Cells[3].Paragraphs.First().Append("status");
t.Rows[1].Cells[4].Paragraphs.First().Append("system");

t.rows[0].Cells[0-4] are headers the rest will be coming from the database. Is there an better way rather than using DocX?

t.Rows[1].Cells[0-4] is needed to come from the database (headers of table in SQL) This is frustrating me, I have never been good with loops

Thank you for your time.


Solution

  • Here is an example of the code. I am using a class to place all of my database information.

    List<Education> eduList = aDoc.getAppEdu(aDoc.AppID);
    if (refList.Count < 0)
    {
          appQue.AppendLine("Applicant did not supply Educational Background information." + Environment.NewLine).Bold();
    }
    else
    {
         foreach (Education ed in eduList)
         {
              Novacode.Table tblEdu = doc.AddTable(6, 1);                                      
              tblEdu.AutoFit = AutoFit.Contents;   
              tblEdu.Rows[0].Cells[0].Paragraphs.First().Append("Education").Bold().FontSize(13);  
                tblEdu.Rows[1].Cells[0].Paragraphs.First().Append("* School Name: ");                          tblEdu.Rows[1].Cells[0].Paragraphs.First().Append(ed.SchoolName).Bold(); ;
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append("* City: ");
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append(ed.City).Bold();
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append("* State: ");
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append(ed.EduState).Bold();
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append("* Zip: ");
                tblEdu.Rows[2].Cells[0].Paragraphs.First().Append(ed.Zip).Bold();
                tblEdu.Rows[3].Cells[0].Paragraphs.First().Append("* From: ");
                tblEdu.Rows[3].Cells[0].Paragraphs.First().Append(ed.SStartScho).Bold();
                tblEdu.Rows[3].Cells[0].Paragraphs.First().Append("* To: ");
                tblEdu.Rows[3].Cells[0].Paragraphs.First().Append(ed.SEndScho).Bold();
                tblEdu.Rows[4].Cells[0].Paragraphs.First().Append("* Did you graduate? ");
                string grad = "No";
                if (ed.Graduate == true)
                {
                    grad = "Yes";
                }                                                     
                tblEdu.Rows[4].Cells[0].Paragraphs.First().Append(grad).Bold();
                tblEdu.Rows[5].Cells[0].Paragraphs.First().Append("* Diploma/Degree: ");               tblEdu.Rows[5].Cells[0].Paragraphs.First().Append(ed.Degree).Bold();                                                            
                doc.InsertTable(tblEdu);
                appQue = doc.InsertParagraph();
         }
    }