2014-04-19 1 views
0

Open XML을 사용하여 Word 문서에 테이블을 삽입하는 방법을 작성했습니다. 이 메서드는 일반 목록과 열 수, 열 머리글 등을 제어하는 ​​몇 가지 매개 변수를 허용합니다.일반 목록을 사용하여 Open XML 테이블 생성

모두 제대로 작동합니다.

그러나 테이블에 셀을 채울 때 각 행의 값을 가져 와서 해당 열에 배치하고 싶습니다. 속성의 이름이 일반 목록의 내용에 따라 변경 될 것이므로이 작업을 수행하는 방법을 잘 모르겠습니다.

나는 올바른 방향으로 나를 가리킬 수있는 사람이라면 인정 될 것이다.

void InsertTable<T>(List<T> tableData, int[] tableHeadingCount, string[] columnHeadings, string locationInDocument) 
      { 
       using (WordprocessingDocument myDoc = WordprocessingDocument.Open(_newDocument, true)) 
       { 
        var docPart = myDoc.MainDocumentPart; 
        var doc = docPart.Document; 

        var table = new Table(); 
        var tableBorderTop = new TopBorder(); 
        var tableBorderBottom = new BottomBorder(); 
        var tableBorderLeft = new LeftBorder(); 
        var tableBorderRight = new RightBorder(); 
        var tableBorderHorizontal = new InsideHorizontalBorder(); 
        var tableBorderVertical = new InsideVerticalBorder(); 
        var tableProperties = new TableProperties(); 
        var borders = new TableBorders(); 


        // Set Border Styles for Table 
        tableBorderTop.Val = BorderValues.Single; 
        tableBorderTop.Size = 6; 
        tableBorderBottom.Val = BorderValues.Single; 
        tableBorderBottom.Size = 6; 
        tableBorderLeft.Val = BorderValues.Single; 
        tableBorderLeft.Size = 6; 
        tableBorderRight.Val = BorderValues.Single; 
        tableBorderRight.Size = 6; 
        tableBorderHorizontal.Val = BorderValues.Single; 
        tableBorderHorizontal.Size = 6; 
        tableBorderVertical.Val = BorderValues.Single; 
        tableBorderVertical.Size = 6; 

        // Assign Border Styles to Table Borders 
        borders.TopBorder = tableBorderTop; 
        borders.BottomBorder = tableBorderBottom; 
        borders.LeftBorder = tableBorderLeft; 
        borders.RightBorder = tableBorderRight; 
        borders.InsideHorizontalBorder = tableBorderHorizontal; 
        borders.InsideVerticalBorder = tableBorderVertical; 


        // Append Border Styles to Table Properties 
        tableProperties.Append(borders); 

        // Assign Table Properties to Table 
        table.Append(tableProperties); 

        var tableRowHeader = new TableRow(); 
        tableRowHeader.Append(new TableRowHeight() { Val = 2000 }); 

        for (int i = 0; i < tableHeadingCount.Length; i++) 
        { 
         var tableCellHeader = new TableCell(); 

         //Assign Font Properties to Run 
         var runPropHeader = new RunProperties(); 
         runPropHeader.Append(new Bold()); 
         runPropHeader.Append(new Color() { Val = "000000" }); 

         //Create New Run 
         var runHeader = new Run(); 
         //Assign Font Properties to Run 
         runHeader.Append(runPropHeader); 

         var columnHeader = new Text(); 
         //Assign the Pay Rule Name to the Run 
         columnHeader = new Text(columnHeadings[i]); 

         runHeader.Append(columnHeader); 

         //Create Properties for Paragraph 
         var justificationHeader = new Justification(); 
         justificationHeader.Val = JustificationValues.Left; 

         var paraPropsHeader = new ParagraphProperties(justificationHeader); 
         SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" }; 
         paraPropsHeader.Append(spacing); 

         var paragraphHeader = new Paragraph(); 

         paragraphHeader.Append(paraPropsHeader); 
         paragraphHeader.Append(runHeader); 
         tableCellHeader.Append(paragraphHeader); 

         var tableCellPropertiesHeader = new TableCellProperties(); 
         var tableCellWidthHeader = new TableCellWidth(); 

         tableCellPropertiesHeader.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "#C0C0C0" }); 

         var textDirectionHeader = new TextDirection(); 
         textDirectionHeader.Val = TextDirectionValues.BottomToTopLeftToRight; 
         tableCellPropertiesHeader.Append(textDirectionHeader); 

         tableCellWidthHeader.Type = TableWidthUnitValues.Dxa; 
         tableCellWidthHeader.Width = "2000"; 

         tableCellPropertiesHeader.Append(tableCellWidthHeader); 

         tableCellHeader.Append(tableCellPropertiesHeader); 
         tableRowHeader.Append(tableCellHeader); 

        } 

        tableRowHeader.AppendChild(new TableHeader()); 

        table.Append(tableRowHeader); 

        //Create New Row in Table for Each Record 

        foreach (var record in tableData) 
        { 
         var tableRow = new TableRow(); 
         for (int i = 0; i < tableHeadingCount.Length; i++) 
         { 

          //**** This is where I dynamically want to iterate through selected properties and output the value **** 

          var propertyText = "Test"; 

          var tableCell = new TableCell(); 

          //Assign Font Properties to Run 
          var runProp = new RunProperties(); 
          runProp.Append(new Bold()); 
          runProp.Append(new Color() { Val = "000000" }); 


          //Create New Run 
          var run = new Run(); 
          //Assign Font Properties to Run 
          run.Append(runProp); 

          //Assign the text to the Run 
          var text = new Text(propertyText); 
          run.Append(text); 

          //Create Properties for Paragraph 
          var justification = new Justification(); 
          justification.Val = JustificationValues.Left; 
          var paraProps = new ParagraphProperties(justification); 

          var paragraph = new Paragraph(); 

          paragraph.Append(paraProps); 
          paragraph.Append(run); 
          tableCell.Append(paragraph); 

          var tableCellProperties = new TableCellProperties(); 
          var tableCellWidth = new TableCellWidth(); 
          tableCellWidth.Type = TableWidthUnitValues.Dxa; 
          tableCellWidth.Width = "2000"; 
          tableCellProperties.Append(tableCellWidth); 
          tableCell.Append(tableCellProperties); 
          tableRow.Append(tableCell); 
         } 

         table.Append(tableRow); 
        } 

        var res = from bm in docPart.Document.Body.Descendants<BookmarkStart>() 
           where bm.Name == locationInDocument 
           select bm; 
        var bookmark = res.SingleOrDefault(); 
        var parent = bookmark.Parent; // bookmark's parent element 
        Paragraph newParagraph = new Paragraph(); 
        parent.InsertAfterSelf(newParagraph); 
        if (bookmark != null) 
        { 
         newParagraph.InsertBeforeSelf(table); 
        } 

       } 
      } 

답변

0

이 문제는 다른 방법으로 해결되었습니다. 본질적으로 표 삽입 방법으로 목록을 전달하는 것이 아닙니다. 필자는 표 머리글을 포함하여 테이블에 필요한 모든 데이터가있는 다차원 배열을 전달하기로 결정했습니다. 이는 본질적으로 Insert Table 메서드가보다 일반화되고 사용자 정의 (즉, 데이터 생성, 열 머리글 지정 등)이 Insert Insert Table에서 수행된다는 것을 의미했습니다.