当前位置: 首页 > 编程语言 > C#

向Word文档中的现有表添加行(打开XML)分享

时间:2023-04-10 12:02:26 C#

C#学习教程:在Word文档中向已有表格添加行(OpenXML))有一个Word文档(.docx)并向表中添加一个新行。有没有办法做到这一点?我正在使用OpenXML我正在创建这样的表(第一次):Tabletbl=newTable();//设置表格的样式和宽度。TablePropertiestableProp=newTableProperties();TableStyletableStyle=newTableStyle(){Val="TableGrid"};//使表格宽度为页面宽度的100%。TableWidthtableWidth=newTableWidth(){Width="5000",Type=TableWidthUnitValues.Pct};//应用tableProp.Append(tableStyle,tableWidth);tbl.AppendChild(tableProp);//向表中添加3列。TableGridtg=newTableGrid(newGridColumn(),newGridColumn(),newGridColumn());tbl.AppendChild(tg);//为表创建1行。TableRowtr1=newTableRow();//向行中的每一列添加一个单元格。TableCelltc1=newTableCell(newParagraph(newRun(newText("1"))));TableCelltc2=newTableCell(新段落(新运行(新文本(“2”))));TableCelltc3=newTableCell(新段落(新运行(新文本("3"))));tr1.Append(tc1,tc2,tc3);//向表中添加行。tbl.AppendChild(tr1);返回表;干得好,身体bod=doc.MainDocumentPart.Document.Body;foreach(表tinbod.Descendants()){t.Append(newTableRow(newTableCell(newParagraph(newParagraph(newRun(newText("test")))))));}使用LINQ获得正确的表格编辑:假设您想要获得一个包含4列的表格。正文bod=doc.MainDocumentPart.Document.Body;foreach(表t在bod.Descendants().Where(tbl=>tbl.GetFirstChild().Descendants().Count()==4)){t.Append(newTableRow(newTableCell(newParagraph(newRun(newText("test")))));}假设你想获取包含单词“mytable”的表。Bodybod=doc.MainDocumentPart.Document.Body;foreach(Tabletinbod.Descendants().Where(tbl=>tbl.InnerText.Contains("myTable"))){t.Append(newTableRow(newTableCell(newParagraph(newRun(newText("test"))))));}这里是一个更详细的示例,您可以在其中向现有表添加5行。这假设该表是文档中的第一个表。如果不是,则必须找到您的表。代码获取表的最后一行并将其复制.之后,您只需在单元格中填写数据即可。表myTable=doc.Body.Descendants().First();TableRowtheRow=myTable.Elements().Last();for(inti=0;i(runProperties);rowCopy.Descendants().ElementAt(0).RemoveAllChildren();//删除复制单元格的文本rowCopy.Descendants().ElementAt(0).Append(newParagraph(run));//在这个例子中,我只从第一个单元格获取运行属性,其余单元格使用文档默认样式。rowCopy.Descendants().ElementAt(1).RemoveAllChildren();rowCopy.Descendants().ElementAt(1).Append(newParagraph(newRun(newText(i.ToString()+"2"))));rowCopy.Descendants().ElementAt(2).RemoveAllChildren();rowCopy.Descendants().ElementAt(2).Append(newParagraph(newRun(newText(i.ToString()+"3"))));myTable.AppendChild(rowCopy);}myTable.RemoveChild(theRow);//youmaywanttoremovethisline.IhaveitbecauseinmycodeIalwayshaveaemptyrowlastinthetablethatIcopy.GetRunPropertiesFromTableCell是我尝试使用与现有行相同的文本格式的快速黑客试。privatestaticRunPropertiesGetRunPropertyFromTableCell(TableRowrowCopy,intcellIndex){varrunProperties=newRunProperties();varfontname="Calibri";varfontSize="18";尝试{fontname=rowCopy.Descendants().ElementAt(cellChild).GetFirst().GetFirstChild().GetFirstChild().GetFirstChild().Ascii;}catch{//swallow}try{fontSize=rowCopy.Descendants().ElementAt(cellIndex).GetFirstChild().GetFirstChild().GetFirstChild().GetFirstChild().Val;}catch{//swallow}runProperties.AppendChild(newRunFonts(){Ascii=fontname});runProperties.AppendChild(newFontSize(){Val=fontSize});返回运行属性;}这里Rowsaddedfromadatasettoanexistingtable以上是C#学习教程:AddingrowstoanexistingtableinaWorddocument(openXML)全部内容分享出来,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注——DataTabledt=newDataTable();dt.Columns.Add("性别");dt.Columns.Add("护照");dt.Columns.Add("姓名");foreach(rptemplist.Items中的RepeaterItem项目){TextBoxtxtGender=(TextBox)item.FindControl("txtGender");TextBoxtxtPassport=(TextBox)item.FindControl("txtPassport");TextBoxtxtName=(TextBox)item.FindControl("txtName");dt.Rows.Add(newobject[]{txtGender.Text,txtPassport.Text,txtName.Text});}使用(WordprocessingDocumentwordDoc2=WordprocessingDocument.Open(file,true)){vardoc=wordDoc2.MainDocumentPart.Document;DocumentFormat.OpenXml.Wordprocessing.Tabletable=doc.MainDocumentPart.Document.Body.Elements().FirstOrDefault();inticounterfortableservice;for(icounterfortableservice=0;icounterfortableservice