未知行数的DocX循环表

本文关键字:循环 DocX 未知 | 更新日期: 2023-09-27 18:16:16

我最近为asp.net安装了DocX,并遇到了为未知数量的行创建循环的问题。

我正在为来自SQL服务器的用户创建报告。其中一部分与历史记录有关,在每个报告的数据库中,创建的每个报告都有不同数量的行。是否有一种方法来创建一个循环或类似于输出这个未知的行量?

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]是标头,其余的将来自数据库。有没有比使用DocX更好的方法?

t.Rows[1].Cells[0-4]需要来自数据库(SQL表的头)这是令人沮丧的我,我从来没有很好与循环

感谢您的宝贵时间。

未知行数的DocX循环表

下面是一个示例代码。我正在使用一个类来放置我所有的数据库信息。

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();
     }
}