private Table CreateTable() { Table table = new Table(); TableProperties tableProperties = new TableProperties(); DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" }; TableWidth tableWidth = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }; TableLook tableLook = new TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties.Append(tableStyle); tableProperties.Append(tableWidth); tableProperties.Append(tableLook); table.Append(tableProperties); return(table); }
/// <summary> /// Creates a new Table from the supplied input data. /// </summary> /// <param name="json"> /// String in JSON format representing the tabular data for updating the Chart's cached data points. /// The JSON object must contain a "fields" attribute as an array containing the field/column names. /// The JSON object must contain a "rows" attribute as an array of arrays representing the rows and their values, with values matching the same order and cardinality of the field names. /// This is the same data as the underlying Excel spreadsheet contents.</param> /// <param name="tableStyle"> /// String containing the name of the Wordprocessing.TableStyle to apply to the table.</param> /// <returns> /// Returns a new Wordprocessing.Table containing the tabular data from the JSON input, formatted with the specified TableStyle.</returns> public static wp.Table BuildTable(string json, string tableStyle) { json = ((json == String.Empty) || (json == null)) ? "{\"fields\": [ \"No Results\" ], \"rows\": [[ \"No Results\" ]]}" : json; //Splunk JSON data is a series of objects consisting of multiple key(column)/value(row) pairs in the result attribute. dynamic input = JsonConvert.DeserializeObject <dynamic>(json); if (input["rows"].Count == 0) { json = "{\"fields\": [ \"No Results\" ], \"rows\": [[ \"No Results\" ]]}"; input = JsonConvert.DeserializeObject <dynamic>(json); } wp.Table result = new wp.Table(); wp.TableProperties tableProperties1 = new wp.TableProperties(); wp.TableStyle tableStyle1 = new wp.TableStyle() { Val = tableStyle }; wp.TableWidth tableWidth1 = new wp.TableWidth() { Width = "5000", Type = wp.TableWidthUnitValues.Pct }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); result.Append(tableProperties1); wp.TableGrid tableGrid = new wp.TableGrid(); //Build table header row wp.TableRow headerRow = new wp.TableRow(); foreach (var columnName in input["fields"]) { headerRow.Append(new wp.TableCell(new wp.Paragraph(new wp.Run(new wp.Text(columnName.ToString()))))); tableGrid.Append(new wp.GridColumn()); } result.Append(tableGrid); result.Append(headerRow); //Build table data rows foreach (var row in input["rows"]) { wp.TableRow tr = new wp.TableRow(); foreach (var cell in row) { tr.Append(new wp.TableCell(new wp.Paragraph(new wp.Run(new wp.Text(cell.ToString()))))); } result.Append(tr); } return(result); }
/// <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.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); }
private void WriteVariables(Body body) { Paragraph p = CreateMidashi1Paragraph("3. " + Resources.VariableSummary); //変数の概要 body.Append(p); ObservableCollection<QuestionVM> questions = studyUnit.AllQuestions; foreach (VariableVM variable in studyUnit.Variables) { StringBuilder buf = new StringBuilder(); buf.Append(variable.Title); buf.Append(" "); buf.Append(variable.Label); buf.Append(" ("); buf.Append(variable.Response.TypeName); buf.Append(" )"); p = CreateParagraph(buf.ToString()); body.Append(p); QuestionVM question = EDOUtils.Find<QuestionVM>(questions, variable.QuestionId); buf = new StringBuilder(); buf.Append(Resources.CorrespondingQuestionSentence); //対応する質問文 if (question != null) { buf.Append(" " + question.Text); } //質問文の段落 p = CreateParagraph(buf.ToString()); body.Append(p); if (variable.Response.IsTypeChoices) { p = CreateEmptyParagraph(); body.Append(p); Table table = new Table(); body.Append(table); TableProperties tableProperties = new TableProperties(); DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" }; TableWidth tableWidth = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }; TableLook tableLook = new TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties.Append(tableStyle); tableProperties.Append(tableWidth); tableProperties.Append(tableLook); table.Append(tableProperties); TableRow tableRow = new TableRow(); table.Append(tableRow); TableCell tableCell1 = new TableCell(); tableCell1.Append(CreateParagraph(Resources.Code)); tableRow.Append(tableCell1); TableCell tableCell2 = new TableCell(); tableCell2.Append(CreateParagraph(Resources.Category)); tableRow.Append(tableCell2); TableCell tableCell3 = new TableCell(); tableCell3.Append(CreateParagraph(Resources.Total)); tableRow.Append(tableCell3); ObservableCollection<CodeVM> codes = variable.Response.Codes; foreach (CodeVM code in codes) { tableRow = new TableRow(); table.Append(tableRow); tableCell1 = new TableCell(); tableCell1.Append(CreateParagraph(code.Value)); tableRow.Append(tableCell1); tableCell2 = new TableCell(); tableCell2.Append(CreateParagraph(code.Label)); tableRow.Append(tableCell2); tableCell3 = new TableCell(); tableCell3.Append(CreateEmptyParagraph()); tableRow.Append(tableCell3); } } //空の段落 p = CreateEmptyParagraph(); body.Append(p); } p = CreateBreakPageParagraph(); body.Append(p); }
void ImportProjectsAndMilestones(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile spreadsheetFileName) { ArrayList cellText = new ArrayList(); // Create a Word table. Word.Table tbl = new Word.Table(); Word.TableProperties tblPr = new Word.TableProperties(); Word.TableStyle tblStyle = new Word.TableStyle(); tblStyle.Val = "LightShading-Accent1"; tblPr.AppendChild(tblStyle); Word.TableWidth tblW = new Word.TableWidth(); tblW.Width = "5000"; tblW.Type = Word.TableWidthUnitValues.Pct; tblPr.Append(tblW); tbl.AppendChild(tblPr); byte[] byteArray = spreadsheetFileName.OpenBinary(); using (MemoryStream mem = new MemoryStream()) { mem.Write(byteArray, 0, (int)byteArray.Length); using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true)) { WorkbookPart workbookPart = mySpreadsheet.WorkbookPart; WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet, "Sheet1"); Excel.SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<Excel.SheetData>(); foreach (Excel.Row r in sheetData) { foreach (Excel.Cell c in r) { cellText.Add(XLGetCellValue(c, workbookPart)); } Word.TableRow tr = CreateRow(cellText); tbl.Append(tr); cellText = new ArrayList(); } } } // Swap out the content control for the SmartArt. OpenXmlElement parent = sdt.Parent; parent.InsertAfter(tbl, sdt); sdt.Remove(); }
private void AddSecondPageTable(Body body) { Table personalInfoTable = new Table(); TableProperties tableProps = new TableProperties(); AddTableBorders(tableProps); TableStyle tableStyle = new TableStyle() { Val = "TableGrid" }; TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }; tableProps.Append(tableStyle, tableWidth); TableGrid tableGrid = new TableGrid(); personalInfoTable.AppendChild(tableProps); for (int x = 0; x < 6; x++) { tableGrid.AppendChild(new GridColumn()); } personalInfoTable.AppendChild(tableGrid); for (int x = 0; x < 7; x++) { TableRow pesonalTableRow = new TableRow(); List <TableCell> cells = new List <TableCell>(); for (int i = 0; i < 6; i++) { TableCell cell = new TableCell(); TableCellProperties tableCellProperties = new TableCellProperties(); tableCellProperties.TableCellVerticalAlignment = new TableCellVerticalAlignment(); tableCellProperties.TableCellVerticalAlignment.Val = TableVerticalAlignmentValues.Center; VerticalMerge verticalMerge = new VerticalMerge(); HorizontalMerge horizontalMerge = new HorizontalMerge(); if (x == 0) { horizontalMerge.Val = i == 0 ? MergedCellValues.Restart : MergedCellValues.Continue; tableCellProperties.AppendChild(horizontalMerge); if (i == 0) { SetCellText(cell, "Time Table", true); } else { SetCellText(cell, "", false); } } else if (i == 0) { verticalMerge.Val = x == 1 ? MergedCellValues.Restart : MergedCellValues.Continue; tableCellProperties.AppendChild(verticalMerge); if (x == 1) { SetCellText(cell, "Hours", true); } else { SetCellText(cell, "", false); } } else if (x == 1) { switch (i) { case 1: SetCellText(cell, "Mon", true); break; case 2: SetCellText(cell, "Tue", true); break; case 3: SetCellText(cell, "Wed", true); break; case 4: SetCellText(cell, "Thu", true); break; case 5: SetCellText(cell, "Fri", true); break; } } else if (x == 4) { horizontalMerge.Val = i == 1 ? MergedCellValues.Restart : MergedCellValues.Continue; tableCellProperties.AppendChild(horizontalMerge); if (i == 1) { SetCellText(cell, "Lunch", true); } else { SetCellText(cell, "", false); } } else if (x == 2 || x == 5) { switch (i) { case 1: case 3: SetCellText(cell, "Science", false); break; case 2: case 4: SetCellText(cell, "Maths", false); break; } } else if (x == 3 || x == 6) { switch (i) { case 1: case 4: SetCellText(cell, "Social", false); break; case 2: SetCellText(cell, "History", false); break; case 3: SetCellText(cell, "English", false); break; } } if (x == 2 && i == 5) { SetCellText(cell, "Arts", false); } else if (x == 3 && i == 5) { SetCellText(cell, "Sports", false); } else if (i == 5 && (x == 5 || x == 6)) { if (x == 5) { verticalMerge.Val = MergedCellValues.Restart; SetCellText(cell, "Project", false); } else { verticalMerge.Val = MergedCellValues.Continue; SetCellText(cell, "", false); } tableCellProperties.AppendChild(verticalMerge); } cell.AppendChild(tableCellProperties); cells.Add(cell); } pesonalTableRow.Append(cells); personalInfoTable.AppendChild(pesonalTableRow); } body.AppendChild(personalInfoTable); }
private void InsertTableWord(WordprocessingDocument doc) { // Encuentra la segunda tabla en el documento. Table table = doc.MainDocumentPart.Document.Body.Elements <Table>().ElementAt(1); // Encuentra la segunda fila en la tabla. TableRow row = table.Elements <TableRow>().ElementAt(1); // Encuentra la celda a modificar. TableCell cell = row.Elements <TableCell>().First(); // Crea la tabla. Table tbl = new Table(); // Establece estiloy y anchura a la tabla. TableProperties tableProp = new TableProperties(); TableStyle tableStyle = new TableStyle() { Val = "TableGrid" }; // Hace que la tabla ocupe el 100% de la pagina. TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }; // Aplicar propiedades a la tabla. tableProp.Append(tableStyle, tableWidth); tbl.AppendChild(tableProp); // Define las columnas de la tabla. TableGrid tg = new TableGrid(); foreach (DataColumn column in ds.Tables[0].Columns) { tg.AppendChild(new GridColumn()); } tbl.AppendChild(tg); // Fila para las columnas de la tabla. TableRow tblRowColumns = new TableRow(); tbl.AppendChild(tblRowColumns); // Obtiene y asigna nombres a las columnas de la tabla. foreach (DataColumn column in ds.Tables[0].Columns) { TableCell tblCell = new TableCell(new Paragraph(new Run(new Text(column.ColumnName)))); tblRowColumns.AppendChild(tblCell); } // Agrega el resto de las filas a la tabla. foreach (DataRow dtRow in ds.Tables[0].Rows) { TableRow tblRow = new TableRow(); for (int i = 0; i < dtRow.Table.Columns.Count; i++) { TableCell tblCell = new TableCell(new Paragraph(new Run(new Text(dtRow[i].ToString())))); tblRow.AppendChild(tblCell); } tbl.AppendChild(tblRow); } // Agrega la tabla al placeholder correspondiente. cell.AppendChild(new Paragraph(new Run(tbl))); }
public Wordprocessing.Table CreateTable(int columnsCount) { Wordprocessing.Table table1 = new Wordprocessing.Table(); WorkbookPart workbookPart = _spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = worksheetPart.Worksheet.Elements<DocumentFormat.OpenXml.Spreadsheet.SheetData>().First(); //Задание свойств таблицы Wordprocessing.TableProperties tableProperties1 = new Wordprocessing.TableProperties(); Wordprocessing.TableStyle tableStyle1 = new Wordprocessing.TableStyle() { Val = "TableGrid" }; Wordprocessing.TableWidth tableWidth1 = new Wordprocessing.TableWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto }; Wordprocessing.TableBorders tableBorders1 = new Wordprocessing.TableBorders(); Wordprocessing.TopBorder topBorder1 = new Wordprocessing.TopBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.LeftBorder leftBorder1 = new Wordprocessing.LeftBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.BottomBorder bottomBorder1 = new Wordprocessing.BottomBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.RightBorder rightBorder1 = new Wordprocessing.RightBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.InsideHorizontalBorder insideHorizontalBorder1 = new Wordprocessing.InsideHorizontalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; Wordprocessing.InsideVerticalBorder insideVerticalBorder1 = new Wordprocessing.InsideVerticalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U }; tableBorders1.Append(topBorder1); tableBorders1.Append(leftBorder1); tableBorders1.Append(bottomBorder1); tableBorders1.Append(rightBorder1); tableBorders1.Append(insideHorizontalBorder1); tableBorders1.Append(insideVerticalBorder1); Wordprocessing.TableLook tableLook1 = new Wordprocessing.TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableBorders1); tableProperties1.Append(tableLook1); table1.Append(tableProperties1); //Создание структуры таблицы Wordprocessing.TableGrid tableGrid1 = new Wordprocessing.TableGrid(); Wordprocessing.GridColumn gridColumn = new Wordprocessing.GridColumn() { Width = "9571" }; tableGrid1.Append(gridColumn); table1.Append(tableGrid1); //Добавление информации из Excel int j = 0; foreach (Spreadsheet.Row r in sheetData.Elements<Spreadsheet.Row>()) { j = 0; Wordprocessing.TableRow tableRow1 = new Wordprocessing.TableRow(); foreach (Spreadsheet.Cell c in r.Elements<Spreadsheet.Cell>()) { j++; string value = c.InnerText; if (c.DataType!= null && c.DataType.Value == CellValues.SharedString) { var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault(); if (stringTable != null) value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; } TableRowExtension.AddCell(tableRow1,value); } for (int i=j; i < columnsCount; i++) TableRowExtension.AddCell(tableRow1, ""); table1.Append(tableRow1); } Wordprocessing.Table table2 = doptable(table1); Wordprocessing.Table table3 = doptable2(table2); return table3; }
private void button2_Click(object sender, EventArgs e) { string pathWord = "C:\\Users\\mishka\\Desktop\\UpWork\\wordTOXml\\wordTOXml\\bin\\Debug\\newword\\Before.docx"; string savePath = "C:\\Users\\mishka\\Desktop\\UpWork\\wordTOXml\\wordTOXml\\bin\\Debug\\newword\\After.docx"; using (var mainDoc = WordprocessingDocument.Open(pathWord, false)) using (var resultDoc = WordprocessingDocument.Create(savePath, WordprocessingDocumentType.Document)) { // copy parts from source document to new document foreach (var part in mainDoc.Parts) { resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId); } IEnumerable <DocumentFormat.OpenXml.Vml.Shape> shapes = resultDoc.MainDocumentPart.Document.Body.Descendants <DocumentFormat.OpenXml.Vml.Shape>(); UInt32 height = 0; foreach (var shape in shapes) { // Getting shape height for adjusting table height var val = shape.Style.Value; string height_str = val.Split(new string[] { "height:" }, StringSplitOptions.None)[1] .Split(new string[] { "pt;" }, StringSplitOptions.None)[0] .Trim(); double h1 = double.Parse(height_str, System.Globalization.CultureInfo.InvariantCulture); int h = Convert.ToInt32(h1); height = Convert.ToUInt32(h); //shape shape.HorizontalAlignment = DocumentFormat.OpenXml.Vml.Office.HorizontalRuleAlignmentValues.Center; } foreach (var cc in resultDoc.ContentControls()) { DocumentFormat.OpenXml.Wordprocessing.Table table1 = new DocumentFormat.OpenXml.Wordprocessing.Table(); TableProperties tableProperties1 = new TableProperties(); DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle1 = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "ad" }; BiDiVisual biDiVisual1 = new BiDiVisual(); TableWidth tableWidth1 = new TableWidth() { Width = "5200", Type = TableWidthUnitValues.Pct }; TableJustification tableJustification1 = new TableJustification() { Val = TableRowAlignmentValues.Center }; TableCellMarginDefault tableCellMarginDefault1 = new TableCellMarginDefault(); TableCellLeftMargin tableCellLeftMargin1 = new TableCellLeftMargin() { Width = 0, Type = TableWidthValues.Dxa }; TableCellRightMargin tableCellRightMargin1 = new TableCellRightMargin() { Width = 0, Type = TableWidthValues.Dxa }; tableCellMarginDefault1.Append(tableCellLeftMargin1); tableCellMarginDefault1.Append(tableCellRightMargin1); TableLook tableLook1 = new TableLook() { Val = "04A0" }; tableProperties1.Append(tableStyle1); tableProperties1.Append(biDiVisual1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableJustification1); tableProperties1.Append(tableCellMarginDefault1); tableProperties1.Append(tableLook1); TableGrid tableGrid1 = new TableGrid(); GridColumn gridColumn1 = new GridColumn() { Width = "555" }; GridColumn gridColumn2 = new GridColumn() { Width = "556" }; tableGrid1.Append(gridColumn1); tableGrid1.Append(gridColumn2); TableRow tableRow1 = new TableRow() { RsidTableRowAddition = "00AE49FD", RsidTableRowProperties = "00AE49FD" }; TableRowProperties tableRowProperties1 = new TableRowProperties(); TableRowHeight tableRowHeight1 = new TableRowHeight() { Val = 200, HeightType = HeightRuleValues.Exact }; TableJustification tableJustification2 = new TableJustification() { Val = TableRowAlignmentValues.Center }; tableRowProperties1.Append(tableRowHeight1); tableRowProperties1.Append(tableJustification2); TableCell tableCell1 = new TableCell(); TableCellProperties tableCellProperties1 = new TableCellProperties(); TableCellWidth tableCellWidth1 = new TableCellWidth() { Width = "100", Type = TableWidthUnitValues.Pct }; //tableCell1.TableCellProperties.TableCellMargin.BottomMargin = new BottomMargin() { Width = "0" }; //tableCell1.TableCellProperties.TableCellMargin.TopMargin = new TopMargin() { Width = "0" }; TableCellVerticalAlignment tableCellVerticalAlignment = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Top }; tableCellProperties1.Append(tableCellWidth1, tableCellVerticalAlignment); DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph2 = new DocumentFormat.OpenXml.Wordprocessing.Paragraph() { RsidParagraphAddition = "00AE49FD", RsidParagraphProperties = "00AE49FD", RsidRunAdditionDefault = "00AE49FD" }; ParagraphProperties paragraphProperties2 = new ParagraphProperties(); Justification justification1 = new Justification() { Val = JustificationValues.Right }; ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties(); RightToLeftText rightToLeftText2 = new RightToLeftText(); paragraphMarkRunProperties2.Append(rightToLeftText2); paragraphProperties2.Append(justification1); paragraphProperties2.Append(paragraphMarkRunProperties2); paragraph2.Append(paragraphProperties2); tableCell1.Append(tableCellProperties1); tableCell1.Append(paragraph2); TableCell tableCell2 = new TableCell(); TableCellProperties tableCellProperties2 = new TableCellProperties(); TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "100", Type = TableWidthUnitValues.Pct }; //tableCell2.TableCellProperties.TableCellMargin.BottomMargin = new BottomMargin() { Width = "0" }; //tableCell2.TableCellProperties.TableCellMargin.TopMargin = new TopMargin() { Width = "0" }; TableCellVerticalAlignment tableCellVerticalAlignment1 = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Bottom }; tableCellProperties2.Append(tableCellWidth2); tableCellProperties2.Append(tableCellVerticalAlignment1); DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph3 = new DocumentFormat.OpenXml.Wordprocessing.Paragraph() { RsidParagraphAddition = "00AE49FD", RsidParagraphProperties = "00AE49FD", RsidRunAdditionDefault = "00AE49FD" }; ParagraphProperties paragraphProperties3 = new ParagraphProperties(); Justification justification2 = new Justification() { Val = JustificationValues.Left }; ParagraphMarkRunProperties paragraphMarkRunProperties3 = new ParagraphMarkRunProperties(); RightToLeftText rightToLeftText3 = new RightToLeftText(); paragraphMarkRunProperties3.Append(rightToLeftText3); paragraphProperties3.Append(justification2); paragraphProperties3.Append(paragraphMarkRunProperties3); paragraph3.Append(paragraphProperties3); tableCell2.Append(tableCellProperties2); tableCell2.Append(paragraph3); tableRow1.Append(tableRowProperties1); tableRow1.Append(tableCell1); tableRow1.Append(tableCell2); table1.Append(tableProperties1); table1.Append(tableGrid1); table1.Append(tableRow1); cc.AppendChild(table1); } } }
// Creates an Document instance and adds its children. public void GenerateLabelsDocument(string fileName, List<Interview> candidateList) { Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" } }; document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Body body1 = new Body(); DocumentFormat.OpenXml.Wordprocessing.Table table1 = new DocumentFormat.OpenXml.Wordprocessing.Table(); TableProperties tableProperties1 = new TableProperties(); DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle1 = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" }; TableWidth tableWidth1 = new TableWidth(){ Width = "0", Type = TableWidthUnitValues.Auto }; TableBorders tableBorders1 = new TableBorders(); TopBorder topBorder1 = new TopBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; LeftBorder leftBorder1 = new LeftBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; BottomBorder bottomBorder1 = new BottomBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; RightBorder rightBorder1 = new RightBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; InsideHorizontalBorder insideHorizontalBorder1 = new InsideHorizontalBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; InsideVerticalBorder insideVerticalBorder1 = new InsideVerticalBorder(){ Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; tableBorders1.Append(topBorder1); tableBorders1.Append(leftBorder1); tableBorders1.Append(bottomBorder1); tableBorders1.Append(rightBorder1); tableBorders1.Append(insideHorizontalBorder1); tableBorders1.Append(insideVerticalBorder1); TableLayout tableLayout1 = new TableLayout(){ Type = TableLayoutValues.Fixed }; TableCellMarginDefault tableCellMarginDefault1 = new TableCellMarginDefault(); TableCellLeftMargin tableCellLeftMargin1 = new TableCellLeftMargin(){ Width = 15, Type = TableWidthValues.Dxa }; TableCellRightMargin tableCellRightMargin1 = new TableCellRightMargin(){ Width = 15, Type = TableWidthValues.Dxa }; tableCellMarginDefault1.Append(tableCellLeftMargin1); tableCellMarginDefault1.Append(tableCellRightMargin1); TableLook tableLook1 = new TableLook(){ Val = "0000", FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = false }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableBorders1); tableProperties1.Append(tableLayout1); tableProperties1.Append(tableCellMarginDefault1); tableProperties1.Append(tableLook1); table1.Append(tableProperties1); TableGrid tableGrid1 = new TableGrid(); GridColumn gridColumn1 = new GridColumn(){ Width = "5760" }; GridColumn gridColumn2 = new GridColumn(){ Width = "270" }; GridColumn gridColumn3 = new GridColumn(){ Width = "5760" }; tableGrid1.Append(gridColumn1); tableGrid1.Append(gridColumn2); tableGrid1.Append(gridColumn3); table1.Append(tableGrid1); string sex = Sex.M.ToString(); Interview interview1, interview2; BioData bioData1, bioData2; for(int i = 0; i < candidateList.Count(); i=i+2) { interview1 = candidateList.ElementAt(i); bioData1 = interview1.BioData; if ((i + 1) < candidateList.Count()) { interview2 = candidateList.ElementAt(i + 1); bioData2 = interview2.BioData; } else { interview2 = null; bioData2 = null; } DocumentFormat.OpenXml.Wordprocessing.TableRow tableRow1 = new DocumentFormat.OpenXml.Wordprocessing.TableRow() { RsidTableRowAddition = "003D48DC" }; TablePropertyExceptions tablePropertyExceptions1 = new TablePropertyExceptions(); TableCellMarginDefault tableCellMarginDefault2 = new TableCellMarginDefault(); TopMargin topMargin1 = new TopMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; BottomMargin bottomMargin1 = new BottomMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; tableCellMarginDefault2.Append(topMargin1); tableCellMarginDefault2.Append(bottomMargin1); tablePropertyExceptions1.Append(tableCellMarginDefault2); TableRowProperties tableRowProperties1 = new TableRowProperties(); CantSplit cantSplit1 = new CantSplit(); TableRowHeight tableRowHeight1 = new TableRowHeight() { Val = (UInt32Value)1440U, HeightType = HeightRuleValues.Exact }; tableRowProperties1.Append(cantSplit1); tableRowProperties1.Append(tableRowHeight1); DocumentFormat.OpenXml.Wordprocessing.TableCell tableCell1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); TableCellProperties tableCellProperties1 = new TableCellProperties(); TableCellWidth tableCellWidth1 = new TableCellWidth() { Width = "5760", Type = TableWidthUnitValues.Dxa }; tableCellProperties1.Append(tableCellWidth1); Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "003D48DC", RsidParagraphProperties = "003D48DC", RsidRunAdditionDefault = "003D48DC" }; ParagraphProperties paragraphProperties1 = new ParagraphProperties(); Indentation indentation1 = new Indentation() { Left = "144", Right = "144" }; paragraphProperties1.Append(indentation1); Run run1 = new Run(); Text text1 = new Text(); Text text1_2 = new Text(); text1.Text = bioData1.LName + ", " + bioData1.FName + ", " + bioData1.MName + " " + bioData1.Sources.SourcesValue; text1_2.Text = bioData1.SSN + " " + interview1.Date.Value.ToShortDateString(); run1.Append(text1); run1.Append(new Break()); run1.Append(text1_2); paragraph1.Append(paragraphProperties1); paragraph1.Append(run1); tableCell1.Append(tableCellProperties1); tableCell1.Append(paragraph1); DocumentFormat.OpenXml.Wordprocessing.TableCell tableCell2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); TableCellProperties tableCellProperties2 = new TableCellProperties(); TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "270", Type = TableWidthUnitValues.Dxa }; tableCellProperties2.Append(tableCellWidth2); Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "003D48DC", RsidParagraphProperties = "003D48DC", RsidRunAdditionDefault = "003D48DC" }; ParagraphProperties paragraphProperties2 = new ParagraphProperties(); Indentation indentation2 = new Indentation() { Left = "144", Right = "144" }; paragraphProperties2.Append(indentation2); paragraph2.Append(paragraphProperties2); tableCell2.Append(tableCellProperties2); tableCell2.Append(paragraph2); DocumentFormat.OpenXml.Wordprocessing.TableCell tableCell3 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); TableCellProperties tableCellProperties3 = new TableCellProperties(); TableCellWidth tableCellWidth3 = new TableCellWidth() { Width = "5760", Type = TableWidthUnitValues.Dxa }; tableCellProperties3.Append(tableCellWidth3); Paragraph paragraph3 = new Paragraph() { RsidParagraphAddition = "003D48DC", RsidParagraphProperties = "003D48DC", RsidRunAdditionDefault = "003D48DC" }; ParagraphProperties paragraphProperties3 = new ParagraphProperties(); Indentation indentation3 = new Indentation() { Left = "144", Right = "144" }; paragraphProperties3.Append(indentation3); Run run2 = new Run(); Text text2 = new Text(); Text text2_2 = new Text(); if (bioData2 != null) { text2.Text = bioData2.LName + ", " + bioData2.FName + ", " + bioData2.MName + " " + bioData2.Sources.SourcesValue; text2_2.Text = bioData2.SSN + " " + interview2.Date.Value.ToShortDateString(); } else { text2.Text = ""; text2_2.Text = ""; } run2.Append(text2); run2.Append(new Break()); run2.Append(text2_2); paragraph3.Append(paragraphProperties3); paragraph3.Append(run2); tableCell3.Append(tableCellProperties3); tableCell3.Append(paragraph3); tableRow1.Append(tablePropertyExceptions1); tableRow1.Append(tableRowProperties1); tableRow1.Append(tableCell1); tableRow1.Append(tableCell2); tableRow1.Append(tableCell3); table1.Append(tableRow1); } Paragraph paragraph31 = new Paragraph() { RsidParagraphMarkRevision = "003D48DC", RsidParagraphAddition = "003D48DC", RsidParagraphProperties = "003D48DC", RsidRunAdditionDefault = "003D48DC" }; ParagraphProperties paragraphProperties31 = new ParagraphProperties(); Indentation indentation31 = new Indentation() { Left = "144", Right = "144" }; ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); Vanish vanish1 = new Vanish(); paragraphMarkRunProperties1.Append(vanish1); paragraphProperties31.Append(indentation31); paragraphProperties31.Append(paragraphMarkRunProperties1); paragraph31.Append(paragraphProperties31); SectionProperties sectionProperties1 = new SectionProperties() { RsidRPr = "003D48DC", RsidR = "003D48DC", RsidSect = "003D48DC" }; SectionType sectionType1 = new SectionType() { Val = SectionMarkValues.Continuous }; PageSize pageSize1 = new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U }; PageMargin pageMargin1 = new PageMargin() { Top = 720, Right = (UInt32Value)240U, Bottom = 0, Left = (UInt32Value)240U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }; Columns columns1 = new Columns() { Space = "720" }; sectionProperties1.Append(sectionType1); sectionProperties1.Append(pageSize1); sectionProperties1.Append(pageMargin1); sectionProperties1.Append(columns1); body1.Append(table1); body1.Append(paragraph31); body1.Append(sectionProperties1); document1.Append(body1); //return document1; string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; using (MemoryStream generatedDocument = new MemoryStream()) { using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); document1.Save(mainPart); } Body body = mainPart.Document.Body; //I have no idea why it doesnt work when you try to use pageBreakParagraph... but it doesnt... so redeclare this same string here string lineBreakCharacter = "%$%lineBreak%$%"; List<Paragraph> pageBreakMarkers = new List<Paragraph>(); var lastP = mainPart.Document.Descendants<Paragraph>().LastOrDefault(); foreach (Paragraph P in mainPart.Document.Descendants<Paragraph>()) { foreach (Run R in P.Descendants<Run>()) { if (R.Descendants<Text>() .Where(T => T.Text == lineBreakCharacter).Count() > 0) { if (P != lastP) { P.InsertAfterSelf (new Paragraph( new Run( new Break() { Type = BreakValues.Page }))); } pageBreakMarkers.Add(P); } } } foreach (Paragraph P in pageBreakMarkers) { P.Remove(); } mainPart.Document.Save(); } byte[] bytesInStream = generatedDocument.ToArray(); // simpler way of converting to array generatedDocument.Close(); Response.Clear(); Response.ContentType = contentType; Response.AddHeader("content-disposition", "attachment;filename=" + fileName); //this will generate problems Response.BinaryWrite(bytesInStream); try { Response.End(); } catch (Exception ex) { //Response.End(); generates an exception. if you don't use it, you get some errors when Word opens the file... System.Diagnostics.Debug.WriteLine(ex.Message); } } }