Im trying to populate 2 tables in .docx file from a controller. I have no problem populating the first table. But I can't seem to find a way to populate the second one. Here is my controller code.
using (WordprocessingDocument sourceDoc = WordprocessingDocument.Open(Server.MapPath("~/doc/temp.docx"), false))
using (var resultDoc = WordprocessingDocument.Create(stream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
{
//Declare a TABLE class(This will get you first table of docx)
Table table = body.Elements<Table>().First();
//Accessing 1st row of the table
TableRow row = table.Elements<TableRow>().ElementAt(0);
//Accessing 2nd cell of that row
TableCell cell = row.Elements<TableCell>().ElementAt(0);
Paragraph p = cell.Elements<Paragraph>().First();
Run r = p.Elements<Run>().First();
Text text1 = r.Elements<Text>().First();
//replacing text in that CELL
text1.Text = text1.Text.Replace("xTitlex", "Title");
//Accessing 2nd ROW of That table
row = table.Elements<TableRow>().ElementAt(1);
//Accessing 2nd CELL of that row
cell = row.Elements<TableCell>().ElementAt(1);
p = cell.Elements<Paragraph>().First();
r = p.Elements<Run>().First();
text1 = r.Elements<Text>().First();
//Replacing text in that CELL
text1.Text = text1.Text.Replace("xVal1x", "Item1");
//This dont seem to work
//Declare a 2nd TABLE class(This will get you second table of docx)
Table table2 = body.Elements<Table>().Last();
TableRow row2 = table2.Elements<TableRow>().ElementAt(0);
////Accessing 2nd cell of that row
TableCell cell2 = row2.Elements<TableCell>().ElementAt(1);
Paragraph p2 = cell.Elements<Paragraph>().Last();
Run r2 = p.Elements<Run>().Last();
Text text12 = r.Elements<Text>().Last();
////replacing text in that CELL
text12.Text = text1.Text.Replace("xVal2x", "Item2");
}
stream.Seek(0, SeekOrigin.Begin);
Firstly, some code seems to be missing from your example. For example, I can't see how you initialized your body
variable. So let's assume you initialized body
essentially as follows:
Body body = sourceDoc.MainDocumentPart.Document.Body;
Next, looking at your code, you mixed up some variables and, thus, reference the wrong table. Specifically, the following lines from your code correctly select the last (second in this case) table, then the first row, and finally, the second cell of that row.
//This dont seem to work
//Declare a 2nd TABLE class(This will get you second table of docx)
Table table2 = body.Elements<Table>().Last();
TableRow row2 = table2.Elements<TableRow>().ElementAt(0);
////Accessing 2nd cell of that row
TableCell cell2 = row2.Elements<TableCell>().ElementAt(1);
Then, however, you initialize the next Paragraph
, Run
, and Text
instances p2
, r2
, and text12
based on first table's cell
, p
, and r
references, completely ignoring cell2
, p2
, and r2
.
Paragraph p2 = cell.Elements<Paragraph>().Last();
Run r2 = p.Elements<Run>().Last();
Text text12 = r.Elements<Text>().Last();
This means text12
references a Text
instance contained in your first table. So, ultimately, replacing "xVal2x"
does not have any effect, because that Text
instance does not contain that string of characters.
I got help from someone. This is the solution.
//Declare a 2nd TABLE class(This will get you second table of docx)
Table table2 = body.Elements<Table>().ElementAt(1);
//Access first row
row = table2.Elements<TableRow>().ElementAt(0);
//Accessing 2nd cell of that row
cell = row.Elements<TableCell>().ElementAt(1);
p = cell.Elements<Paragraph>().First();
r = p.Elements<Run>().First();
text1 = r.Elements<Text>().First();
//Replacing text in that CELL
text1.Text = text1.Text.Replace("xVal2x", "Item2");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.