如何从 table 单元格中获取值

How to get the value from a table cell

我创建了一个 table 并且对 "costs" 列最感兴趣。我可能有不止一行数据,其中我会有多个成本,我想将它们相加并将结果放在 "Total." 旁边的 table 单元格中我看到了一个名为 GetValue() 的方法;但不确定这是否是我正在寻找的或如何使用它。我的想法是,migradoc 有一种方法,您可以在其中获取 table 单元格的值,我会将其存储在变量中。在我创建 "Total" 行的地方,我将使用该变量来显示总数。那我该怎么做呢?

我的代码:

/define header of table
        Row row = table.AddRow();
        row.HeadingFormat = true;
        Cell cell = row.Cells[0];
        cell.AddParagraph("Customer Name");
        cell.Format.Font.Bold = true;

        cell = row.Cells[1];
        cell.AddParagraph("Date Created");
        cell.Format.Font.Bold = true;

        cell = row.Cells[2];
        cell.AddParagraph("Description");
        cell.Format.Font.Bold = true;

        cell = row.Cells[3];
        cell.AddParagraph("Due Date");
        cell.Format.Font.Bold = true;

        cell = row.Cells[4];
        cell.AddParagraph("Billable Hours");
        cell.Format.Font.Bold = true;

        cell = row.Cells[5];
        cell.AddParagraph("Costs");
        cell.Format.Font.Bold = true;



        //define a row of data in the table
        foreach (TicketView1 ticket in SampleTickets)
        {
            row = table.AddRow();

            cell = row.Cells[0];
            cell.AddParagraph(ticket.customer_name);

            cell = row.Cells[1];
            cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));

            cell = row.Cells[2];
            cell.AddParagraph(ticket.description);

            cell = row.Cells[3];
            cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));

            cell = row.Cells[4];
            cell.AddParagraph(ticket.billable_hrs.ToString());
        }

        cell = row.Cells[5];
        cell.AddParagraph(".00");



        //add invisible row as a space line to the table
        row = table.AddRow();
        row.Borders.Visible = false;

        //add the subtotal row
        row = table.AddRow();
        row.Cells[0].Borders.Visible = false;
        row.Cells[0].AddParagraph("Sub Total:");
        row.Cells[0].Format.Font.Bold = true;
        row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
        row.Cells[0].MergeRight = 4;
        cell = row.Cells[5];



        //add tax row
        row = table.AddRow();
        row.Cells[0].Borders.Visible = false;
        row.Cells[0].AddParagraph("TAX:");
        row.Cells[0].Format.Font.Bold = true;
        row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
        row.Cells[0].MergeRight = 4;

        //add total
        row = table.AddRow();
        row.Cells[0].Borders.Visible = false;
        row.Cells[0].AddParagraph("TOTAL:");
        row.Cells[0].Format.Font.Bold = true;
        row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
        row.Cells[0].MergeRight = 4;




    }

我通过简单地创建一个计数变量找到了答案,然后在我的 forEach 循环中添加那个总和加上我的虚拟数据,这将是 60。然后就在我创建我的 "totals" 行的地方,对在它下面,我将包含计数数据的变量添加到单元格中。

我的 forEach 循环和计数变量

double sum = 0;
        foreach (TicketView1 ticket in SampleTickets)
        {
            row = table.AddRow();

            cell = row.Cells[0];
            cell.AddParagraph(ticket.customer_name);

            cell = row.Cells[1];
            cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));

            cell = row.Cells[2];
            cell.AddParagraph(ticket.description);

            cell = row.Cells[3];
            cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));

            cell = row.Cells[4];
            cell.AddParagraph(ticket.billable_hrs.ToString());

            cell = row.Cells[5];
            cell.AddParagraph(".00");
            sum = sum + 60;
        }

我添加总计行并将数据附加到总计单元格的位置:

//add total
        row = table.AddRow();
        row.Cells[0].Borders.Visible = false;
        row.Cells[0].AddParagraph("TOTAL:");
        row.Cells[0].Format.Font.Bold = true;
        row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
        row.Cells[0].MergeRight = 4;

        row.Cells[5].AddParagraph(sum.ToString("[=11=].00"));