0

我最近为 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 中的表头)这让我很沮丧,我从来都不擅长循环

感谢您的时间。

4

1 回答 1

0

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

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();
     }
} 
于 2015-12-28T17:36:53.460 回答