/// <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); }
/// <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); }
/// <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); }
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; }
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; }
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(); }
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"); } }