[英]Populate .docx second table using OpenXml SDK WordprocessingDocument
我試圖從控制器填充 .docx 文件中的 2 個表。 填充第一個表沒有問題。 但我似乎找不到填充第二個的方法。 這是我的控制器代碼。
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);
首先,您的示例中似乎缺少一些代碼。 例如,我看不到您是如何初始化body
變量的。 因此,讓我們假設您基本上按如下方式初始化了body
:
Body body = sourceDoc.MainDocumentPart.Document.Body;
接下來,查看您的代碼,您混淆了一些變量,因此引用了錯誤的表。 具體來說,代碼中的以下幾行正確選擇了最后一個(在本例中為第二個)表,然后是第一行,最后是該行的第二個單元格。
//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);
但是,然后,您根據第一個表的cell
、 p
和r
引用初始化下一個Paragraph
、 Run
和Text
實例p2
、 r2
和text12
,完全忽略cell2
、 p2
和r2
。
Paragraph p2 = cell.Elements<Paragraph>().Last();
Run r2 = p.Elements<Run>().Last();
Text text12 = r.Elements<Text>().Last();
這意味着text12
引用包含在您的第一個表中的Text
實例。 因此,最終,替換"xVal2x"
沒有任何效果,因為該Text
實例不包含該字符串。
我得到了某人的幫助。 這就是解決方案。
//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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.