コード例 #1
0
        public Word.TableCell GenerateCell(string val, string w, int span = 0, bool bold = false, string sz = "12", string s = "style22", string f = "Courier New", Word.JustificationValues halign = Word.JustificationValues.Left)
        {
            Word.LeftMargin margin = new Word.LeftMargin {
                Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = new StringValue("10")
            };
            Word.TableCellWidth width = new Word.TableCellWidth {
                Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = w
            };
            Word.Shading shading = new Word.Shading {
                Fill = "auto", Val = new EnumValue <Word.ShadingPatternValues>(Word.ShadingPatternValues.Clear)
            };
            Word.TableCellMargin             cellmargin = new Word.TableCellMargin(margin);
            Word.VerticalTextAlignmentOnPage align      = new Word.VerticalTextAlignmentOnPage {
                Val = new EnumValue <Word.VerticalJustificationValues>(Word.VerticalJustificationValues.Center)
            };
            Word.GridSpan gspan = new Word.GridSpan {
                Val = span
            };

            Word.TableCellProperties props = new Word.TableCellProperties(width, shading, cellmargin, align, gspan);

            Word.Paragraph paragraph = GenerateParagraph(val, bold, sz, s, f, halign);

            Word.TableCell cell = new Word.TableCell(props, paragraph);
            return(cell);
        }
コード例 #2
0
ファイル: Md2MlCore.cs プロジェクト: NetDevArt/md2ml
        /// <summary>
        /// Add a row to a table, defining of not the justification for each cell
        /// </summary>
        /// <param name="table">The table on which append a TableRow</param>
        /// <param name="values">List of values to be inserted col by col in a row</param>
        /// <param name="justif">The list of justification value</param>
        public void AddTableRow(Table table, List <string> values, List <JustificationValues> justif = null)
        {
            TableRow tableRow = new TableRow();
            int      i        = 0;

            foreach (var value in values)
            {
                TableCell           tableCell           = new TableCell();
                TableCellProperties tableCellProperties = new TableCellProperties();
                TableCellWidth      tableCellWidth      = new TableCellWidth()
                {
                    Type = TableWidthUnitValues.Auto
                };
                tableCellProperties.Append(tableCellWidth);
                tableCell.Append(tableCellProperties);
                tableRow.Append(tableCell);

                var para = justif != null?CreateNonBodyParagraph(justif[i]) : CreateNonBodyParagraph();

                WriteText(para, value.Trim());
                tableCell.Append(para);

                i++;
            }
            table.Append(tableRow);
        }
コード例 #3
0
        private static TableCellProperties WordCellProperties(Campus.Report.Base.TableStyle style)
        {
            var leftBorder             = new DocumentFormat.OpenXml.Wordprocessing.LeftBorder();
            var rightBorder            = new DocumentFormat.OpenXml.Wordprocessing.RightBorder();
            var topBorder              = new DocumentFormat.OpenXml.Wordprocessing.TopBorder();
            var bottomBorder           = new DocumentFormat.OpenXml.Wordprocessing.BottomBorder();
            var insideHorizontalBorder = new DocumentFormat.OpenXml.Wordprocessing.InsideHorizontalBorder();
            var insideVerticalBorder   = new DocumentFormat.OpenXml.Wordprocessing.InsideVerticalBorder();

            leftBorder.Val             = WordLineType(style.BorderLine);
            rightBorder.Val            = WordLineType(style.BorderLine);
            topBorder.Val              = WordLineType(style.BorderLine);
            bottomBorder.Val           = WordLineType(style.BorderLine);
            insideHorizontalBorder.Val = WordLineType(style.BorderLine);
            insideVerticalBorder.Val   = WordLineType(style.BorderLine);

            leftBorder.Color             = ColorToRgb(style.BorderColor);
            rightBorder.Color            = ColorToRgb(style.BorderColor);
            topBorder.Color              = ColorToRgb(style.BorderColor);
            bottomBorder.Color           = ColorToRgb(style.BorderColor);
            insideHorizontalBorder.Color = ColorToRgb(style.BorderColor);
            insideVerticalBorder.Color   = ColorToRgb(style.BorderColor);

            leftBorder.Size             = WordBorderWaight(style.BorderLine);
            rightBorder.Size            = WordBorderWaight(style.BorderLine);
            topBorder.Size              = WordBorderWaight(style.BorderLine);
            bottomBorder.Size           = WordBorderWaight(style.BorderLine);
            insideHorizontalBorder.Size = WordBorderWaight(style.BorderLine);
            insideVerticalBorder.Size   = WordBorderWaight(style.BorderLine);


            var cellprop = new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties();
            var shading  = new DocumentFormat.OpenXml.Wordprocessing.Shading();

            shading.Val  = DocumentFormat.OpenXml.Wordprocessing.ShadingPatternValues.Clear;
            shading.Fill = ColorToRgb(style.Foreground);

            var tableCellBorders = new DocumentFormat.OpenXml.Wordprocessing.TableCellBorders
            {
                LeftBorder             = leftBorder,
                RightBorder            = rightBorder,
                TopBorder              = topBorder,
                BottomBorder           = bottomBorder,
                InsideHorizontalBorder = insideHorizontalBorder,
                InsideVerticalBorder   = insideVerticalBorder
            };

            cellprop.Shading          = shading;
            cellprop.TableCellBorders = tableCellBorders;

            return(cellprop);
        }
コード例 #4
0
        /// <summary>
        /// The write table.
        /// </summary>
        /// <param name="t">
        /// The t.
        /// </param>
        public void WriteTable(Table t)
        {
            this.body.Append(CreateParagraph(t.GetFullCaption(this.style), TableCaptionID));

            var table = new DocumentFormat.OpenXml.Wordprocessing.Table();

            var tableProperties1 = new TableProperties();
            var tableStyle1 = new TableStyle { Val = "TableGrid" };
            var tableWidth1 = new TableWidth { Width = "0", Type = TableWidthUnitValues.Auto };
            var tableLook1 = new TableLook
                {
                    Val = "04A0",
                    FirstRow = true,
                    LastRow = false,
                    FirstColumn = true,
                    LastColumn = false,
                    NoHorizontalBand = false,
                    NoVerticalBand = true
                };

            tableProperties1.Append(tableStyle1);
            tableProperties1.Append(tableWidth1);
            tableProperties1.Append(tableLook1);

            var tableGrid1 = new TableGrid();
            foreach (var tc in t.Columns)
            {
                // tc.Width
                var gridColumn1 = new GridColumn { Width = "3070" };
                tableGrid1.Append(gridColumn1);
            }

            foreach (var row in t.Rows)
            {
                var tr = new TableRow();

                if (row.IsHeader)
                {
                    var trp = new TableRowProperties();
                    var tableHeader1 = new TableHeader();
                    trp.Append(tableHeader1);
                    tr.Append(trp);
                }

                int j = 0;
                foreach (var c in row.Cells)
                {
                    bool isHeader = row.IsHeader || t.Columns[j++].IsHeader;
                    var cell = new TableCell();
                    var tcp = new TableCellProperties();
                    var borders = new TableCellBorders();
                    borders.Append(
                        new BottomBorder
                            {
                                Val = BorderValues.Single,
                                Size = (UInt32Value)4U,
                                Space = (UInt32Value)0U,
                                Color = "auto"
                            });
                    borders.Append(
                        new TopBorder
                            {
                                Val = BorderValues.Single,
                                Size = (UInt32Value)4U,
                                Space = (UInt32Value)0U,
                                Color = "auto"
                            });
                    borders.Append(
                        new LeftBorder
                            {
                                Val = BorderValues.Single,
                                Size = (UInt32Value)4U,
                                Space = (UInt32Value)0U,
                                Color = "auto"
                            });
                    borders.Append(
                        new RightBorder
                            {
                                Val = BorderValues.Single,
                                Size = (UInt32Value)4U,
                                Space = (UInt32Value)0U,
                                Color = "auto"
                            });
                    tcp.Append(borders);

                    cell.Append(tcp);
                    string styleID = isHeader ? "TableHeader" : "TableText";
                    cell.Append(CreateParagraph(c.Content, styleID));
                    tr.Append(cell);
                }

                table.Append(tr);
            }

            this.body.Append(table);
        }
コード例 #5
0
        /// <summary>
        /// The write table.
        /// </summary>
        /// <param name="t">The t.</param>
        public void WriteTable(Table t)
        {
            this.body.AppendChild(CreateParagraph(t.GetFullCaption(this.style), TableCaptionId));

            var table = new DocumentFormat.OpenXml.Wordprocessing.Table();

            var tableProperties1 = new TableProperties();
            var tableStyle1      = new TableStyle {
                Val = "TableGrid"
            };
            var tableWidth1 = new TableWidth {
                Width = "0", Type = TableWidthUnitValues.Auto
            };
            var tableLook1 = new TableLook
            {
                Val              = "04A0",
                FirstRow         = true,
                LastRow          = false,
                FirstColumn      = true,
                LastColumn       = false,
                NoHorizontalBand = false,
                NoVerticalBand   = true
            };

            tableProperties1.AppendChild(tableStyle1);
            tableProperties1.AppendChild(tableWidth1);
            tableProperties1.AppendChild(tableLook1);

            var tableGrid1 = new TableGrid();

            // ReSharper disable once UnusedVariable
            foreach (var tc in t.Columns)
            {
                // TODO: use tc.Width to set the width of the column
                var gridColumn1 = new GridColumn {
                    Width = "3070"
                };
                tableGrid1.AppendChild(gridColumn1);
            }

            foreach (var row in t.Rows)
            {
                var tr = new TableRow();

                if (row.IsHeader)
                {
                    var trp          = new TableRowProperties();
                    var tableHeader1 = new TableHeader();
                    trp.AppendChild(tableHeader1);
                    tr.AppendChild(trp);
                }

                int j = 0;
                foreach (var c in row.Cells)
                {
                    bool isHeader = row.IsHeader || t.Columns[j++].IsHeader;
                    var  cell     = new TableCell();
                    var  tcp      = new TableCellProperties();
                    var  borders  = new TableCellBorders();
                    borders.AppendChild(
                        new BottomBorder
                    {
                        Val   = BorderValues.Single,
                        Size  = 4U,
                        Space = 0U,
                        Color = "auto"
                    });
                    borders.AppendChild(
                        new TopBorder
                    {
                        Val   = BorderValues.Single,
                        Size  = 4U,
                        Space = 0U,
                        Color = "auto"
                    });
                    borders.AppendChild(
                        new LeftBorder
                    {
                        Val   = BorderValues.Single,
                        Size  = 4U,
                        Space = 0U,
                        Color = "auto"
                    });
                    borders.AppendChild(
                        new RightBorder
                    {
                        Val   = BorderValues.Single,
                        Size  = 4U,
                        Space = 0U,
                        Color = "auto"
                    });
                    tcp.AppendChild(borders);

                    cell.AppendChild(tcp);
                    string styleId = isHeader ? "TableHeader" : "TableText";
                    cell.AppendChild(CreateParagraph(c.Content, styleId));
                    tr.AppendChild(cell);
                }

                table.AppendChild(tr);
            }

            this.body.AppendChild(table);
        }
コード例 #6
0
        private static bool Render(TableElement element, WordprocessingDocument document, Body body)
        {
            if (element.Table.Rows.Count == 0)
            {
                return(true);
            }

            var table = new DocumentFormat.OpenXml.Wordprocessing.Table();

            if (element.Headers.Count != 0)
            {
                var row = new DocumentFormat.OpenXml.Wordprocessing.TableRow();

                foreach (var head in element.Headers)
                {
                    var text      = new DocumentFormat.OpenXml.Wordprocessing.Text(head.ToString());
                    var run       = new DocumentFormat.OpenXml.Wordprocessing.Run(text);
                    var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);

                    paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties
                    {
                        ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId()
                        {
                            Val = WordStyleIdFromStyleName(document, element.HeaderStyle.Name)
                        },
                        WordWrap = new DocumentFormat.OpenXml.Wordprocessing.WordWrap {
                            Val = new OnOffValue {
                                Value = true
                            }
                        },
                        TextAlignment =
                            new DocumentFormat.OpenXml.Wordprocessing.TextAlignment
                        {
                            Val = DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignmentValues.Auto
                        },
                    };

                    var cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell(paragraph);

                    DocumentFormat.OpenXml.Wordprocessing.TableCellProperties cellprop = WordCellProperties(element.HeaderStyle);
                    cell.Append(cellprop);

                    row.Append(cell);
                }
                table.Append(row);
            }


            for (int i = 0; i < element.Table.Rows.Count; i++)
            {
                var row = new DocumentFormat.OpenXml.Wordprocessing.TableRow();

                for (int j = 0; j < element.Table.Columns.Count; j++)
                {
                    var text      = new DocumentFormat.OpenXml.Wordprocessing.Text(element.Table.Rows[i][j].ToString());
                    var run       = new DocumentFormat.OpenXml.Wordprocessing.Run(text);
                    var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);

                    paragraph.ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties
                    {
                        ParagraphStyleId = new DocumentFormat.OpenXml.Wordprocessing.ParagraphStyleId()
                        {
                            Val = WordStyleIdFromStyleName(document, element.TableStyle.Name)
                        },
                        WordWrap = new DocumentFormat.OpenXml.Wordprocessing.WordWrap {
                            Val = new OnOffValue {
                                Value = true
                            }
                        },
                        TextAlignment =
                            new DocumentFormat.OpenXml.Wordprocessing.TextAlignment
                        {
                            Val = DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignmentValues.Auto
                        },
                    };

                    var cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell(paragraph);

                    var cellprop = WordCellProperties(element.TableStyle);

                    cell.Append(cellprop);

                    row.Append(cell);
                }
                table.Append(row);
            }
            body.AppendChild <DocumentFormat.OpenXml.Wordprocessing.Table>(table);
            return(false);
        }
コード例 #7
0
 private static BorderStyle GetBorderStyle(this Word.TableCellProperties properties)
 {
     return(properties?.TableCellBorders.ToCellBorderStyle() ?? new BorderStyle(null));
 }
コード例 #8
0
        private Wordprocessing.Table doptable2(Wordprocessing.Table table)
        {
            for (int i = 1; i <= 5; i++)
            {
                //Первый столбец
                Wordprocessing.TableRow tableRow = new Wordprocessing.TableRow();
                Wordprocessing.TableCell tableCell1 = new Wordprocessing.TableCell();
                Wordprocessing.TableCellProperties tableCellProperties1 = new Wordprocessing.TableCellProperties();
                Wordprocessing.TableCellWidth tableCellWidth1 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                Wordprocessing.GridSpan gridSpan1 = new Wordprocessing.GridSpan() { Val = 3 };
                 Wordprocessing.VerticalMerge verticalMerge1;
                if (i==1)  verticalMerge1 = new Wordprocessing.VerticalMerge() { Val = Wordprocessing.MergedCellValues.Restart };
                else verticalMerge1 = new Wordprocessing.VerticalMerge();
                tableCellProperties1.Append(tableCellWidth1);
                tableCellProperties1.Append(gridSpan1);
                tableCellProperties1.Append(verticalMerge1);

                tableCell1.Append(tableCellProperties1);
                if (i == 1)
                {
                    for (int j=1; j<5; j++)
                      tableCell1.Append(retText(j, 1));
                }
                else tableCell1.Append(retText(i, 1));
                tableRow.Append(tableCell1);

                //Второй столбец
                Wordprocessing.TableCell tableCell2 = new Wordprocessing.TableCell();
                Wordprocessing.TableCellProperties tableCellProperties2 = new Wordprocessing.TableCellProperties();
                Wordprocessing.TableCellWidth tableCellWidth2 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                Wordprocessing.VerticalMerge verticalMerge2;
                if (i == 1) verticalMerge2 = new Wordprocessing.VerticalMerge() { Val = Wordprocessing.MergedCellValues.Restart };
                else verticalMerge2 = new Wordprocessing.VerticalMerge();
                tableCellProperties2.Append(tableCellWidth2);
                tableCellProperties2.Append(verticalMerge2);
                tableCell2.Append(tableCellProperties2);
                tableCell2.Append(retText(i, 2));
                tableRow.Append(tableCell2);

                for (int j = 0; j < 2; j++)
                {
                    Wordprocessing.TableCell tableCell3 = new Wordprocessing.TableCell();
                    Wordprocessing.TableCellProperties tableCellProperties3 = new Wordprocessing.TableCellProperties();
                    Wordprocessing.TableCellWidth tableCellWidth3 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                    tableCellProperties3.Append(tableCellWidth3);
                    tableCell3.Append(tableCellProperties3);
                    tableCell3.Append(retText(i, j + 3));
                    tableRow.Append(tableCell3);

                }

                table.Append(tableRow);

            }

            Wordprocessing.TableRow tableRowrlast = new Wordprocessing.TableRow();

            Wordprocessing.TableCell tableCellrlast = new Wordprocessing.TableCell();
            Wordprocessing.TableCellProperties tableCellPropertiesrlast = new Wordprocessing.TableCellProperties();
            Wordprocessing.TableCellWidth tableCellWidthrlast = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
            Wordprocessing.GridSpan gridSpanrlast = new Wordprocessing.GridSpan() { Val = 3 };
            Wordprocessing.VerticalMerge verticalMergerlast = new Wordprocessing.VerticalMerge();
            tableCellPropertiesrlast.Append(tableCellWidthrlast);
            tableCellPropertiesrlast.Append(gridSpanrlast);
            tableCellPropertiesrlast.Append(verticalMergerlast);
            tableCellrlast.Append(tableCellPropertiesrlast);
            tableCellrlast.Append(retText(-1, -1));

            Wordprocessing.TableCell tableCellrlast1 = new Wordprocessing.TableCell();
            Wordprocessing.TableCellProperties tableCellPropertiesrlast1 = new Wordprocessing.TableCellProperties();
            Wordprocessing.TableCellWidth tableCellWidthrlast1 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
            Wordprocessing.GridSpan gridSpanrlast1 = new Wordprocessing.GridSpan() { Val = 3 };
            tableCellPropertiesrlast1.Append(tableCellWidthrlast1);
            tableCellPropertiesrlast1.Append(gridSpanrlast1);
            tableCellrlast1.Append(tableCellPropertiesrlast1);
            tableCellrlast1.Append(retText(0, 0));

            tableRowrlast.Append(tableCellrlast);
            tableRowrlast.Append(tableCellrlast1);
            table.Append(tableRowrlast);

            return table;
        }
コード例 #9
0
 private Wordprocessing.Table doptable(Wordprocessing.Table table)
 {
     //Строка подпись декана
     Wordprocessing.TableRow tableRow2 = new Wordprocessing.TableRow() { RsidTableRowAddition = "00FF67BF", RsidTableRowProperties = "008E2483" };
     Wordprocessing.TableCell tableCell = new Wordprocessing.TableCell();
     Wordprocessing.TableCellProperties tableCellProperties = new Wordprocessing.TableCellProperties();
     Wordprocessing.TableCellWidth tableCellWidth = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
     Wordprocessing.GridSpan gridSpan = new Wordprocessing.GridSpan() { Val = 6 };
     Wordprocessing.TableCellVerticalAlignment tableCellVerticalAlignment = new Wordprocessing.TableCellVerticalAlignment() { Val = Wordprocessing.TableVerticalAlignmentValues.Center };
     tableCellProperties.Append(tableCellWidth);
     tableCellProperties.Append(gridSpan);
     tableCellProperties.Append(tableCellVerticalAlignment);
     Wordprocessing.Paragraph paragraph = new Wordprocessing.Paragraph();
     Wordprocessing.ParagraphProperties parprop = new Wordprocessing.ParagraphProperties();
     Wordprocessing.SpacingBetweenLines spacingBetweenLines2 = new Wordprocessing.SpacingBetweenLines() { After = "0" };
     parprop.Append(spacingBetweenLines2);
     paragraph.Append(parprop);
     Wordprocessing.Run run = new Wordprocessing.Run();
     Wordprocessing.Text text = new Wordprocessing.Text();
     text.Text = "Подпись декана";
     run.Append(text);
     paragraph.Append(run);
     tableCell.Append(tableCellProperties);
     tableCell.Append(paragraph);
     tableRow2.Append(tableCell);
     table.Append(tableRow2);
     return table;
 }
コード例 #10
0
        public void CreateWordDoc(DataTable data, decimal t, string user)
        {
            string pathUser     = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string pathDownload = Path.Combine(pathUser, "Downloads");
            //string pathDownload = Server.MapPath("~/Content/");
            decimal total = t;

            try
            {
                //Set the current directory.
                Directory.SetCurrentDirectory(pathDownload);
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine("The specified directory does not exist. {0}", e);
            }

            WordprocessingDocument doc         = WordprocessingDocument.Create("ExpenseReport.docx", WordprocessingDocumentType.Document);
            MainDocumentPart       mainDocPart = doc.AddMainDocumentPart();

            mainDocPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
            DocumentFormat.OpenXml.Wordprocessing.Body body = new DocumentFormat.OpenXml.Wordprocessing.Body();
            mainDocPart.Document.Append(body);
            DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();

            //border
            TableProperties tblProp = new TableProperties(
                new TableBorders(
                    new InsideHorizontalBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 10
            })
                );

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

            //setting header
            DocumentFormat.OpenXml.Wordprocessing.TableRow      tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
            DocumentFormat.OpenXml.Wordprocessing.RunProperties rp = new DocumentFormat.OpenXml.Wordprocessing.RunProperties();
            rp.Append(new DocumentFormat.OpenXml.Wordprocessing.Color()
            {
                Val = "#FF0000"
            });
            RunFonts rFont1 = new RunFonts();

            rFont1.Ascii = "Arial";
            rp.Append(rFont1);
            rp.Append(new Bold());
            rp.Append(new DocumentFormat.OpenXml.Wordprocessing.FontSize()
            {
                Val = "28"
            });

            DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
            run.RunProperties = rp;
            run.Append(new Text("Expense Report for " + user));
            DocumentFormat.OpenXml.Wordprocessing.Paragraph           para = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);
            DocumentFormat.OpenXml.Wordprocessing.TableCellProperties tcpp = new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties();
            tcpp.Append(new DocumentFormat.OpenXml.Wordprocessing.TableCellWidth {
                Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa, Width = "2200"
            });
            GridSpan gs = new GridSpan();

            gs.Val = 5;
            tcpp.Append(gs);
            DocumentFormat.OpenXml.Wordprocessing.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell(tcpp, para);
            tr.Append(tc);
            table.Append(tr);

            DocumentFormat.OpenXml.Wordprocessing.TableRow row_header = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
            foreach (DataColumn column in data.Columns)
            {
                DocumentFormat.OpenXml.Wordprocessing.TableCell cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                cell.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                                new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(column.ToString()))));
                cell.Append(new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties(new DocumentFormat.OpenXml.Wordprocessing.TableCellWidth {
                    Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa, Width = "2200"
                }));
                row_header.Append(cell);
            }

            table.Append(row_header);

            for (int i = 0; i < data.Rows.Count; ++i)
            {
                DocumentFormat.OpenXml.Wordprocessing.TableRow row = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
                for (int j = 0; j < data.Columns.Count; j++)
                {
                    DocumentFormat.OpenXml.Wordprocessing.TableCell cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                    cell.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows[i][j].ToString()))));
                    cell.Append(new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties(new DocumentFormat.OpenXml.Wordprocessing.TableCellWidth {
                        Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa, Width = "2200"
                    }));
                    row.Append(cell);
                }
                table.Append(row);
            }
            body.Append(table);

            Run       run1  = new Run();
            Paragraph para1 = new Paragraph(run1);

            run1.AppendChild(new Text("The total Expenditure is " + total));
            body.Append(para1);
            doc.MainDocumentPart.Document.Save();
            doc.Dispose();
        }
コード例 #11
0
ファイル: TableBlock.cs プロジェクト: kevinfs/ReportGenerator
        private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXW.Table)
            {
                OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table;

                OXW.TableRow headerRowTemplate  = table?.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow;
                OXW.TableRow contentRowTemplate = table?.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow;

                #region Column number management
                OXW.TableGrid         tablegrid = table?.Descendants <OXW.TableGrid>().FirstOrDefault();
                List <OXW.GridColumn> columns   = tablegrid?.Descendants <OXW.GridColumn>().ToList();
                if (columns != null && content.NbColumns != columns.Count)
                {
                    if (content.NbColumns < columns.Count)
                    {
                        for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--)
                        {
                            tablegrid.RemoveChild(columns[i]);
                        }
                    }
                    else
                    {
                        for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++)
                        {
                            tablegrid.AppendChild(new OXW.GridColumn()
                            {
                                Width = "200"
                            });
                        }
                    }
                }

                #endregion Column number management

                ModifyWordRowTextContent(headerRowTemplate, string.Empty, string.Empty, string.Empty);
                ModifyWordRowTextContent(contentRowTemplate, string.Empty, string.Empty, string.Empty);

                int idx   = 0;
                int nbrow = 0;
                List <OXW.TableCell> headerCells  = headerRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                List <OXW.TableCell> contentCells = contentRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                headerRowTemplate?.RemoveAllChildren <OXW.TableCell>();
                OXW.TableRow row = headerRowTemplate;
                if (headerCells != null)
                {
                    int headerCellsCount  = headerCells.Count;
                    int contentCellsCount = headerCells.Count;

                    table.RemoveAllChildren <OXW.TableRow>();
                    for (int i = 0; i < content.Data.Count(); i++)
                    {
                        var item = content.Data.ToArray()[i];
                        if (null != item)
                        {
                            OXW.TableCell cell;
                            if (content.HasColumnHeaders && 0 == nbrow)
                            {
                                cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell;
                            }
                            else
                            {
                                cell = contentCells?[idx % contentCellsCount].CloneNode(true) as OXW.TableCell;
                            }

                            string txtColor = string.Empty;
                            string effect   = string.Empty;
                            if (content.HasCellsAttributes())
                            {
                                CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i);
                                if (attributes != null)
                                {
                                    OXW.TableCellProperties tcp = new OXW.TableCellProperties(
                                        new OXW.TableCellWidth {
                                        Type = OXW.TableWidthUnitValues.Auto,
                                    }
                                        );
                                    // Create the Shading object
                                    OXW.Shading shading =
                                        new OXW.Shading()
                                    {
                                        Color = "auto",
                                        Fill  = ColorTranslator.ToHtml(attributes.BackgroundColor),
                                        Val   = OXW.ShadingPatternValues.Clear
                                    };
                                    // Add the Shading object to the TableCellProperties object
                                    tcp.Append(shading);
                                    // Add the TableCellProperties object to the TableCell object
                                    cell?.Append(tcp);

                                    txtColor = $"{attributes.FontColor.R:X2}{attributes.FontColor.G:X2}{attributes.FontColor.B:X2}";
                                    effect   = attributes.Effect;
                                }
                            }

                            ModifyWordCellTextContent(cell, item, txtColor, effect);
                            row?.Append(cell);
                        }

                        idx = ++idx % content.NbColumns;
                        if (0 != idx)
                        {
                            continue;
                        }
                        if (null != row)
                        {
                            table.Append(row);
                            nbrow++;
                        }
                        row = contentRowTemplate?.CloneNode(true) as OXW.TableRow;
                        row?.RemoveAllChildren <OXW.TableCell>();
                    }
                }
                var blockSdtAncestor = block.Ancestors <OXW.SdtBlock>();
                if (0 != blockSdtAncestor.ToList().Count)
                {
                    // case table is in a content control
                    var blockStd = block.Ancestors <OXW.SdtBlock>().First();
                    blockStd.Parent.ReplaceChild(table, blockStd);
                }
                else
                {
                    // case table is directly in the document
                    var blockStd = block;
                    blockStd.Parent.ReplaceChild(table, blockStd);
                }
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null");
            }
        }
コード例 #12
0
ファイル: StyleHelper.cs プロジェクト: xw-fighting/DocxToPdf
        /// <summary>
        /// Get the nearest applied element (e.g. RunFonts, Languages) in the style hierachy. It searches upstream from current $obj til reach the top of the hierachy if no found.
        /// </summary>
        /// <typeparam name="T">Target element type (e.g. RunFonts.GetType()).</typeparam>
        /// <param name="obj">The OpenXmlElemet to search from.</param>
        /// <returns>Return found element or null if not found.</returns>
        public T GetAppliedElement <T>(OpenXmlElement obj)
        {
            T ret = default(T);

            if (obj == null)
            {
                return(ret);
            }

            Type objType = obj.GetType();

            if (objType == typeof(Word.Run))
            { // Run.RunProperties > Run.RunProperties.rStyle > Paragraph.ParagraphProperties.pStyle (> default style > docDefaults)
                // ( ): done in paragraph level
                Word.RunProperties runpr = StyleHelper.GetElement <Word.RunProperties>(obj);
                if (runpr != null)
                {
                    ret = StyleHelper.GetDescendants <T>(runpr);

                    // If has rStyle, go through rStyle before go on.
                    // Use getAppliedStyleElement() is because it will go over all the basedOn styles.
                    if (ret == null && runpr.RunStyle != null)
                    {
                        ret = this.GetAppliedElement <T>(this.GetStyleById(runpr.RunStyle.Val));
                    }
                }

                if (ret == null)
                { // parent paragraph's pStyle
                    if (obj.Parent != null && obj.Parent.GetType() == typeof(Word.Paragraph))
                    {
                        Word.Paragraph pg = obj.Parent as Word.Paragraph;
                        if (pg.ParagraphProperties != null && pg.ParagraphProperties.ParagraphStyleId != null)
                        {
                            ret = this.GetAppliedElement <T>(this.GetStyleById(pg.ParagraphProperties.ParagraphStyleId.Val));
                        }
                    }
                }

                if (ret == null) // default run style
                {
                    ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Character));
                }

                if (ret == null) // docDefaults
                {
                    ret = StyleHelper.GetDescendants <T>(this.docDefaults.RunPropertiesDefault);
                }
            }
            else if (objType == typeof(Word.Paragraph))
            { // Paragraph.ParagraphProperties > Paragraph.ParagraphProperties.pStyle > default style > docDefaults
                Word.ParagraphProperties pgpr = StyleHelper.GetElement <Word.ParagraphProperties>(obj);
                if (pgpr != null)
                {
                    ret = StyleHelper.GetDescendants <T>(pgpr);

                    // If has pStyle, go through pStyle before go on.
                    // Use getAppliedStyleElement() is because it will go over the whole Style hierachy.
                    if (ret == null && pgpr.ParagraphStyleId != null)
                    {
                        ret = this.GetAppliedElement <T>(this.GetStyleById(pgpr.ParagraphStyleId.Val));
                    }
                }

                if (ret == null)
                {
                    if (obj.Parent != null && obj.Parent.GetType() == typeof(Word.TableCell))
                    {
                        for (int i = 0; i < 3 && obj != null; i++)
                        {
                            obj = (obj.Parent != null) ? obj.Parent : null;
                        }
                        if (obj != null && obj.GetType() == typeof(Word.Table))
                        {
                            ret = this.GetAppliedElement <T>(obj);
                        }
                    }
                }

                if (ret == null) // default paragraph style
                {
                    ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Paragraph));
                }

                if (ret == null) // docDefaults
                {
                    ret = StyleHelper.GetDescendants <T>(this.docDefaults);
                }
            }
            else if (objType == typeof(Word.Table))
            { // Table.TableProperties > Table.TableProperties.tblStyle > default style
                Word.TableProperties tblpr = StyleHelper.GetElement <Word.TableProperties>(obj);
                if (tblpr != null)
                {
                    ret = StyleHelper.GetDescendants <T>(tblpr);

                    // If has tblStyle, go through tblStyle before go on.
                    // Use getAppliedStyleElement() is because it will go over the whole Style hierachy.
                    if (ret == null && tblpr.TableStyle != null)
                    {
                        ret = this.GetAppliedElement <T>(this.GetStyleById(tblpr.TableStyle.Val));
                    }
                }

                if (ret == null) // default table style
                {
                    ret = this.GetAppliedElement <T>(this.GetDefaultStyle(DefaultStyleType.Table));
                }
            }
            else if (objType == typeof(Word.TableRow))
            { // TableRow.TableRowProperties > Table.TableProperties.tblStyle (> default style)
                // ( ): done in Table level
                Word.TableRowProperties rowpr = StyleHelper.GetElement <Word.TableRowProperties>(obj);
                if (rowpr != null)
                {
                    ret = StyleHelper.GetDescendants <T>(rowpr);
                }

                if (ret == null)
                {
                    for (int i = 0; i < 1 && obj != null; i++)
                    {
                        obj = (obj.Parent != null) ? obj.Parent : null;
                    }
                    if (obj != null && obj.GetType() == typeof(Word.Table))
                    {
                        ret = this.GetAppliedElement <T>(obj);
                    }
                }
            }
            else if (objType == typeof(Word.TableCell))
            { // TableCell.TableCellProperties > Table.TableProperties.tblStyle (> default style)
                // ( ): done in Table level
                Word.TableCellProperties cellpr = StyleHelper.GetElement <Word.TableCellProperties>(obj);
                if (cellpr != null)
                {
                    ret = StyleHelper.GetDescendants <T>(cellpr);
                }

                if (ret == null)
                {
                    for (int i = 0; i < 2 && obj != null; i++)
                    {
                        obj = (obj.Parent != null) ? obj.Parent : null;
                    }
                    if (obj != null && obj.GetType() == typeof(Word.Table))
                    {
                        ret = this.GetAppliedElement <T>(obj);
                    }
                }
            }
            else if (objType == typeof(Word.Style))
            {
                Word.Style st = obj as Word.Style;
                ret = StyleHelper.GetDescendants <T>(st);
                if (ret == null)
                {
                    if (st.BasedOn != null)
                    {
                        ret = this.GetAppliedElement <T>(this.GetStyleById(st.BasedOn.Val));
                    }
                }
            }
            else // unknown type, just get everything can get
            {
                ret = StyleHelper.GetDescendants <T>(obj);
            }

            return(ret);
        }