Esempio n. 1
0
        public static void WriteJsonTable(this SdtElement element, string path, MainDocumentPart mainPart)
        {
            var tablejson = JObject.Parse(File.ReadAllText(path));

            var boderSize = tablejson.SelectToken("$.border.outer.size").ToObject<UInt32>();
            var borderColor = tablejson.SelectToken("$.border.outer.color").ToObject<string>();
            var borderType = (BorderValues)tablejson.SelectToken("$.border.outer.type").ToObject<int>();
            // Create an empty table.
            Table table = new Table();

            // Create a TableProperties object and specify its border information.
            TableProperties tblProp = new TableProperties(
                new TableBorders(
                    new TopBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    },
                    new BottomBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    },
                    new LeftBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    },
                    new RightBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    },
                    new InsideHorizontalBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    },
                    new InsideVerticalBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(borderType),
                        Size = boderSize,
                        Color = borderColor,
                    }
                )
            );

            // Append the TableProperties object to the empty table.
            table.AppendChild<TableProperties>(tblProp);

            CreateTableHeader(tablejson, table);

            var rows = tablejson.SelectToken("$.body.rows").ToArray();
            var i = 0;
            foreach (var row in rows)
            {

                TableRow tr = new TableRow();
                foreach (var column in row)
                {
                    var run = new Run(new RunProperties(new Bold(), new Color() { Val = "#000000" }), new Text(column.ToString()));

                    if (column is JObject columnObj && column.SelectToken("$.type").ToString() == "image")
                    {

                        InsertAPicture(mainPart, column.SelectToken("$.path").ToString(), run, column.SelectToken("$.width").ToString());

                    }

                    tr.Append(new TableCell(
                          new TableCellProperties(
                               new Shading()
                               {
                                   Color = "auto",
                                   Fill = i % 2 == 0 ? "auto" : "#eeeeee",
                                   Val = ShadingPatternValues.Clear
                               }),
                          new Paragraph(new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), run
                  )
                          ));
                }
                i++;
                table.Append(tr);
            }

            element.RemoveAllChildren();
            element.AppendChild(table);
        }
Esempio n. 2
0
        /// <summary>
        /// Fill data from lists of items into sourceStream with tableName tag and list of column names.
        /// </summary>
        /// <param name="items">The list of data items that need fill into sourceStream.</param>
        /// <param name="sourceStream">The stream source that is needed to fill.</param>
        /// <param name="tableName">The name of control that contrain for data table.</param>
        /// <param name="columns">The list of column name.</param>
        public void FillDataListOfObject(IEnumerable items, Stream sourceStream, string tableName, string[] columns, string[] widths = null)
        {
            if (items != null)
            {
                if (sourceStream != null)
                {
                    //use OpenXml to process
                    using (WordprocessingDocument word = WordprocessingDocument.Open(sourceStream, true))
                    {
                        var part = word.MainDocumentPart;
                        // SdtBlock block = part.Document.Body.Descendants<SdtBlock>().Where(p => p.SdtProperties.GetFirstChild<Tag>().Val == tableName).FirstOrDefault();
                        SdtElement tableElement = part.Document.Body.Descendants <SdtBlock>().Where(p => p.SdtProperties.GetFirstChild <Tag>().Val == tableName).FirstOrDefault();
                        if (tableElement == null)
                        {
                            tableElement = part.Document.Body.Descendants <SdtRun>().Where(p => p.SdtProperties.GetFirstChild <Tag>().Val == tableName).FirstOrDefault();
                        }

                        if (tableElement != null)
                        {
                            var table = NewTable(columns, widths);

                            #region Fill data
                            foreach (var item in items)
                            {
                                TableRow dataRow = NewEmptyRow();

                                string[] stringValues = GetStringValuesFromObject(item);
                                if (widths != null)
                                {
                                    for (var i = 0; i < columns.Length; i++)
                                    {
                                        var value = stringValues[i];
                                        //create cell object and its properties
                                        TableCell tableCellData = NewTableCellData(value, widths[i]);
                                        // add to row
                                        dataRow.Append(tableCellData);
                                    }
                                }
                                else
                                {
                                    for (var i = 0; i < columns.Length; i++)
                                    {
                                        var value = stringValues[i];
                                        //create cell object and its properties
                                        TableCell tableCellData = NewTableCellData(value);
                                        // add to row
                                        dataRow.Append(tableCellData);
                                    }
                                }

                                table.Append(dataRow);
                            }
                            #endregion

                            tableElement.RemoveAllChildren();
                            tableElement.Append(table);
                            part.Document.Save();
                        }
                    }
                }
            }
        }