2012-12-06 90 views
1


我該如何對齊我的表在xml文檔中的文檔?
這是我的代碼,我想在中間對齊表。Open XML(Microsoft Word):C#表格理由

這是我的代碼。我試圖用TableJustification,但似乎它不工作

using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) 
      { 
       MainDocumentPart mainPart = wordDoc.AddMainDocumentPart(); 
       mainPart.Document = new Document(); 
       Body body = mainPart.Document.AppendChild(new Body()); 

       Run run = new Run(); 
       // Goes through all of the forms 
       foreach (var form in forms) 
       { 

        Table table = new Table(); 
        // Initialize all of the table properties 
        TableProperties tblProp = new TableProperties(
         new TableBorders(
          new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 }, 
          new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 }, 
          new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 }, 
          new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 } 
         ) 

        ); 

        // Align the table to the center 
        TableJustification justs_center = new TableJustification() { Val = _____ }; 

        table.AppendChild(justs_center); 
        table.AppendChild<TableProperties>(tblProp); 

        Paragraph para = body.AppendChild(new Paragraph()); 
        Run run_header = para.AppendChild(new Run()); 
        RunProperties runProps = run_header.AppendChild(new RunProperties(new Bold())); 


        string username = form.Username; 
        string proces_header = form.HeaderTitle; 

        run_header.AppendChild(new Text(proces_header + " | " + username)); 

        for (int i = 0; i < form.FieldList.Count; i++) 
        { 
         if (!(form.FieldList[i].Token == "USR" || form.FieldList[i].Token == "SNT")) 
         { 
          TableRow tr = new TableRow(); 

          TableCell header_cell = new TableCell(); 

          header_cell.Append(new Paragraph(new Run(new Text(form.FieldList[i].Label + ": " + form.FieldList[i].Value)))); 

          tr.Append(header_cell); 
          table.Append(tr); 
         } 
        } 
        wordDoc.MainDocumentPart.Document.Body.Append(table); 

       } 

       mainPart.Document.Save(); 
       wordDoc.Close(); 
       return "Success"; 
      } 
     } 

回答

3
Table t = new Table(
    new TableProperties(
     ... 
     new TableJustification() { Val = TableRowAlignmentValues.Center}, 
     ...), 
    new TableRow(...), 
); 
0

試圖中心與接受的答案表似乎沒有工作。我需要到中心對齊適用於所有的行以及

var t = new Table(); 
var tableProps = new TableProperties(); 
tableProps.TableJustification = new TableJustification { Val = TableRowAlignmentValues.Center }; 
t.Append(tableProps); 

var row = new TableRow(); 
var rowProps = new TableRowProperties(); 
rowProps.Append(new TableJustification { Val = TableRowAlignmentValues.Center }); 
row.Append(rowProps); 

使用Open XML生產力工具

找到