Ejemplo n.º 1
0
        private static void FillDetailsTable(ref DocX templateDoc)
        {
            // The datas that will fill the details table.
            var datas = MiscellaneousSample.GetDetailsData();

            // T he table from the templated invoice.
            var detailsTable = templateDoc.Tables.LastOrDefault();

            if (detailsTable == null)
            {
                return;
            }

            // Remove all rows of the details table, except the header one.
            while (detailsTable.Rows.Count > 1)
            {
                detailsTable.RemoveRow();
            }

            // Loop through each data rows and use them to add new rows in the detailsTable.
            foreach (DataRow data in datas.Rows)
            {
                var newRow = detailsTable.InsertRow();
                newRow.Cells.First().InsertParagraph(data.ItemArray.First().ToString());
                newRow.Cells.Last().InsertParagraph(data.ItemArray.Last().ToString());
            }

            // Calculate the total amount.
            var amountStrings = detailsTable.Rows.Select(r => r.Cells.Last().Paragraphs.Last().Text.Remove(0, 1)).ToList();

            amountStrings.RemoveAt(0); // remove the header
            var totalAmount = amountStrings.Select(s => double.Parse(s)).Sum();

            // Add a Total row in the details table.
            var totalRow = detailsTable.InsertRow();

            totalRow.Cells.First().InsertParagraph("TOTAL:");
            totalRow.Cells.Last().InsertParagraph(string.Format("${0}", totalAmount));
        }