none
Range concept in OpenXML for word automation using .net RRS feed

  • Question

  • Hi,

    I have convert word automation with open Xml technology.I have add some word templates to single word document and populate values to fields in that document.in following scenario if single values for make,model,serial,year,nnumber and seat fields then no problem for populate values to that field.but if more than one values available for that fields then i need to copy that particular range and paste below with table and populate values to that field.Dynamically how to populate for more than value (See image below) to single field with loop using openXML?How to implement above scenario? please provide solution

    For template:

    

    For Multiple value:

    

    For Single Value:


    • Edited by GC_04 Wednesday, July 22, 2015 1:16 PM
    Wednesday, July 22, 2015 1:14 PM

Answers

  • Hi GC_04,

    >>but if more than one values available for that fields then i need to copy that particular range and paste below with table and populate values to that field.Dynamically how to populate for more than value (See image below) to single field with loop using openXML?How to implement above scenario?<<

    According to the description, it seems you want to add rows to table and update the values of cells.

    Based on my understanding, we need to "copy the range" using Open XML too. To append a row into a table using Open XML, I suggest that you use open XML SDK 2.5 Productivity Tool which shif with Open XML SDK to compare changes of the documents insert rows before and after. And it also can refect the code to help us to start up.

    Hope it is helpful.

    Regards & Fei 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 23, 2015 6:52 AM
    Moderator
  • Rather than building each row piece-by-piece for every data item, the code copies the empty row (the last row). This ensures that the new row always contains the correct number of columns. For every data item, you add another copy of the empty placeholder row. You add the new rows in the foreach loop in the following code by using the overridden Append method, which takes a parameter array of objects of type OpenXmlElement. To modify text in a table cell by using the Open XML Format SDK, you need to use a Text object, which is a property of the Run object, so the code supplies nested constructors for Paragraph, Run, and then Text objects. The Text object has a constructor overload that takes a string as its only argument, so the code calls that constructor and supplies a value from the data source. Once the code has written a value in each cell of the new row, the row is appended to the table, as shown in the following code.

    // Get the last row in the table.
    TableRow theRow = theTable.Elements<TableRow>().Last();
    foreach (var data in datas)
    {
           TableRow rowCopy = (TableRow)theRow.CloneNode(true);
           rowCopy.Descendants<TableCell>().ElementAt(0).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           rowCopy.Descendants<TableCell>().ElementAt(1).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           rowCopy.Descendants<TableCell>().ElementAt(2).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           theTable.AppendChild(rowCopy);
    }
    
    // Remove the empty placeholder row from the table.
    theTable.RemoveChild(theRow);
    // Save the changes to the table back into the document.
    mainPart.Document.Save();
    
    
    For more information, click here to refer about Inserting Repeating Data Items into a Word 2007 Table by Using the Open XML API.

    Thursday, July 23, 2015 9:47 AM

All replies

  • Hi,

    I have convert word automation with open Xml technology.I have add some word templates to single word document and populate values to fields in that document.in following scenario if single values for make,model,serial,year,nnumber and seat fields then no problem for populate values to that field.but if more than one values available for that fields then i need to copy that particular range and paste below with table and populate values to that field.Dynamically how to populate for more than value (See image below) to single field with loop using openXML?How to implement above scenario? please provide solution

    For template:

    

    For Multiple value:

    

    For Single Value:

    Wednesday, July 22, 2015 1:15 PM
  • Hi GC_04,

    >>but if more than one values available for that fields then i need to copy that particular range and paste below with table and populate values to that field.Dynamically how to populate for more than value (See image below) to single field with loop using openXML?How to implement above scenario?<<

    According to the description, it seems you want to add rows to table and update the values of cells.

    Based on my understanding, we need to "copy the range" using Open XML too. To append a row into a table using Open XML, I suggest that you use open XML SDK 2.5 Productivity Tool which shif with Open XML SDK to compare changes of the documents insert rows before and after. And it also can refect the code to help us to start up.

    Hope it is helpful.

    Regards & Fei 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 23, 2015 6:52 AM
    Moderator
  • Rather than building each row piece-by-piece for every data item, the code copies the empty row (the last row). This ensures that the new row always contains the correct number of columns. For every data item, you add another copy of the empty placeholder row. You add the new rows in the foreach loop in the following code by using the overridden Append method, which takes a parameter array of objects of type OpenXmlElement. To modify text in a table cell by using the Open XML Format SDK, you need to use a Text object, which is a property of the Run object, so the code supplies nested constructors for Paragraph, Run, and then Text objects. The Text object has a constructor overload that takes a string as its only argument, so the code calls that constructor and supplies a value from the data source. Once the code has written a value in each cell of the new row, the row is appended to the table, as shown in the following code.

    // Get the last row in the table.
    TableRow theRow = theTable.Elements<TableRow>().Last();
    foreach (var data in datas)
    {
           TableRow rowCopy = (TableRow)theRow.CloneNode(true);
           rowCopy.Descendants<TableCell>().ElementAt(0).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           rowCopy.Descendants<TableCell>().ElementAt(1).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           rowCopy.Descendants<TableCell>().ElementAt(2).Append(new Paragraph
           (new Run(new Text("Data of Value"))));
           theTable.AppendChild(rowCopy);
    }
    
    // Remove the empty placeholder row from the table.
    theTable.RemoveChild(theRow);
    // Save the changes to the table back into the document.
    mainPart.Document.Save();
    
    
    For more information, click here to refer about Inserting Repeating Data Items into a Word 2007 Table by Using the Open XML API.

    Thursday, July 23, 2015 9:47 AM