2017-09-19 1 views
0

J'essaie de créer une table de mots à partir d'un fichier XML, mais les lignes ne sont pas dans leur bon ordre, c'est-à-dire que le premier élément apparaît comme dernière ligne, 2ème comme première ligne et ainsi de suite. Où vais-je mal?Table XML vers Word

Word.Table oTable; 
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
oTable = oDoc.Tables.Add(wrdRng, ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count, ref oMissing, ref oMissing); 
oTable.Range.ParagraphFormat.SpaceAfter = 6; 
oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; 
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; 
for (int r = 1; r <= ds.Tables[0].Rows.Count; r++) 
{ 
    for (int c = 1; c <= ds.Tables[0].Columns.Count; c++) 
    { 
     oTable.Cell(r, c).Range.Text = ds.Tables[0].Rows[r-1].ItemArray[c-1].ToString(); 
    } 
} 

Répondre

0

Voici l'exemple de code permettant de créer un fichier de mots simple avec Table. Il vous donnera une indication de boucle comment procéder avec lui.

class Program 
{ 
    static void Main(string[] args) 

    { 
     Document doc = new Document(); 
     Section s = doc.AddSection(); 
     Table table = s.AddTable(true); 

     //Create Header and Data 
     String[] header = { "Item", "Description", "Qty", "Unit Price", "Price" }; 
     String[][] data = { 
           new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"}, 
           new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"}, 
           new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"}, 
           new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"}, 
          }; 
     //Add Cells 
     table.ResetCells(data.Length + 1, header.Length); 

     //Header Row 
     TableRow fRow = table.Rows[0]; 
     fRow.IsHeader = true; 
     //Row Height 
     fRow.Height = 23; 
     //Header Format 
     fRow.RowFormat.BackColor = Color.AliceBlue; 
     for (int i = 0; i < header.Length; i++) 
     { 
      //Cell Alignment 
      Paragraph p = fRow.Cells[i].AddParagraph(); 
      fRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; 
      p.Format.HorizontalAlignment = HorizontalAlignment.Center; 
      //Data Format 
      TextRange TR = p.AppendText(header[i]); 
      TR.CharacterFormat.FontName = "Calibri"; 
      TR.CharacterFormat.FontSize = 14; 
      TR.CharacterFormat.TextColor = Color.Teal; 
      TR.CharacterFormat.Bold = true; 
     } 

     //Data Row 
     for (int r = 0; r < data.Length; r++) 
     { 
      TableRow dataRow = table.Rows[r + 1]; 

      //Row Height 
      dataRow.Height = 20; 

      //C Represents Column. 
      for (int c = 0; c < data[r].Length; c++) 
      { 
       //Cell Alignment 
       dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; 
       //Fill Data in Rows 
       Paragraph p2 = dataRow.Cells[c].AddParagraph(); 
       TextRange TR2 = p2.AppendText(data[r][c]); 
       //Format Cells 
       p2.Format.HorizontalAlignment = HorizontalAlignment.Center; 
       TR2.CharacterFormat.FontName = "Calibri"; 
       TR2.CharacterFormat.FontSize = 12; 
       TR2.CharacterFormat.TextColor = Color.Brown; 
      } 
     } 

     //Save and Launch 
     doc.SaveToFile("WordTable.docx", FileFormat.Docx2013); 
     System.Diagnostics.Process.Start("WordTable.docx"); 


     Console.ReadLine(); 

    }