Deleting existing table and adding a new table in DOCX file using C#
-
martedì 21 agosto 2012 20:59
Hi,
I have a requirement where I need to open a docx file and search , replace certain tables. These tables appear in the next line after a certain text for eg ~CAIXXXX. Now I need to delete these tables that appear in next line after say ~CAIXXXX and replace it with a new table.
Can someone please help me. The requirement is hard for me and any help would be greatly appreciated.
Thanks,
GM
Tutte le risposte
-
mercoledì 22 agosto 2012 02:51
Hello, you can use open xml Sdk for this
http://msdn.microsoft.com/en-us/library/office/cc850841
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
-
mercoledì 22 agosto 2012 14:31
Hi Sachyn,
Thanks for the help :) .. I was able to open a word doc and insert a table in it using the examples.. I have a small additional question. My requirement is to find tables within a word doc below a specific text say ~CIB, delete the existing table and insert a new table.
Any help on how to scroll within a word doc and delete specific tables and insert new table at that particular location.
Thanks,
Gaurav
-
mercoledì 22 agosto 2012 18:30
Hello Gaurav, following code is sample on how to search and delete the table in Word Document.
const string fileName = @"document Path"; var document = WordprocessingDocument.Open(fileName, true); var doc = document.MainDocumentPart.Document; var table = doc.Body.Elements<Table>(); foreach (var i in table) { if (i.InnerText.Contains("~CIB")) { i.Remove(); } } doc.Save();Note: I didn't test this code but i think it will work.
HTH
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
- Modificato Sachin P mercoledì 22 agosto 2012 18:36
- Contrassegnato come risposta GauravMehta giovedì 23 agosto 2012 17:25
-
mercoledì 22 agosto 2012 19:16
Hi Sachyn,
Thanks to you I am almost there. I tried the code fragmant. However, this seems to delete only the first table that has a ~CIB in it. It is not running through the loop for remaining tables. Any idea why.
Also, I am writing a code to create table right after the i.Remove.However, I am not able to insert the table at the exact same place where the table was deleted. I am using the following code. The current code is adding a new table in the end. Any help on how to put a new table at the same place where original table was delete.
if (i.InnerText.Contains("~CIB")) { i.Remove(); Table table1 = new Table(); // Create a row. TableRow tr1 = new TableRow(); // Create a cell. TableCell tc = new TableCell(); tc.Append(new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })); tc.Append(new Paragraph(new Run(new Text("TESTTEXT")))); tr1.Append(tc); table1.Append(tr1); doc.MainDocumentPart.Document.Body.Append(table1); }
Thanks,
Gaurav
- Modificato GauravMehta mercoledì 22 agosto 2012 20:22
-
giovedì 23 agosto 2012 17:24
Hi Sachyn,
I made some modifications to the code and it worked. Thanks for your all your help. I am posting the code for future reference.
using (WordprocessingDocument doc = WordprocessingDocument.Open(@"c:\testfiles\temp.docx",true)) { var document = doc.MainDocumentPart.Document; var tab = document.Body.Elements<Table>(); foreach (var i in tab) { if (i.InnerText.Contains("~CIB")) { //i.Remove(); Table table1 = new Table(); // Create a row. TableRow tr1 = new TableRow(); // Create a cell. TableCell tc = new TableCell(); tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })); tc.Append(new Paragraph(new Run(new Text("TESTTEXT")))); tr1.Append(tc); table1.Append(tr1); //doc.MainDocumentPart.Document.Body.Append(table1); //doc.MainDocumentPart.Document.Body.InsertBeforeSelf(table1); //doc.MainDocumentPart.Document.Body.InsertAfterSelf(table1); //doc.MainDocumentPart.Document.Body.InsertAt<i>(table1); i.InnerXml = table1.InnerXml; } }
Thanks,
Gaurav
- Contrassegnato come risposta GauravMehta giovedì 23 agosto 2012 17:25

