Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Mar 07, 2017 1:25 pm

Hello,

I'm wondering how you could remove a specific row in an existing table in Word.

Imagine, in row five is a cell with the string 'Hello' in it.
How can I find this specific row and delete the row with that string in it?
My programming language is C# and I'm using Visual Studio 2015 Community.

Thanks in advance!

Mark van Winkelen

m.vanwinkelen@schoutenkampen.nl
 
Posts: 5
Joined: Tue Mar 07, 2017 9:57 am

Wed Mar 08, 2017 3:07 am

Dear Mark,

Thanks for your inquiry.
Here is sample code for your reference.
Code: Select all
            Document doc = new Document(@"F:\testing\Sample.docx");
            foreach (Section sec in doc.Sections)
            {
                //find the text
                TextSelection[] selections = doc.FindAllString("Hello", true, true);
                foreach (TextSelection selection in selections)
                {
                    TextRange tr = selection.GetAsOneRange();
                    Paragraph par = tr.OwnerParagraph;
                    if (par.IsInCell)
                    {
                            //par.Owner.Owner means table row
                            TableRow row = par.Owner.Owner as TableRow;
                            Table table = row.Owner as Table;
                            table.Rows.Remove(row);
                    }
                }
            }
            doc.SaveToFile("9966.docx", FileFormat.Docx);

Hope this can helps, if there is still issue, please let me know.

Sincerely,
Betsy
E-iceblue support team
Last edited by Betsy.jiang on Fri Mar 10, 2017 3:52 am, edited 2 times in total.
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Mar 09, 2017 7:45 am

Dear Mark,

Did you test the code I provided ? Did it help you solve your issue ?
We will appreciate it if you could give us some feedback.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Mar 09, 2017 8:07 am

It's been tested and it worked! Thank you very much for your help!

m.vanwinkelen@schoutenkampen.nl
 
Posts: 5
Joined: Tue Mar 07, 2017 9:57 am

Thu Mar 09, 2017 8:23 am

Hi,

Thanks for your feedback.
Any question, please let us know, we will be happy to help you :) .

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Mar 11, 2020 5:58 am

Is this code for java too,
if not can you write the code for java.
Thank U!

masehullahhami
 
Posts: 3
Joined: Thu Feb 27, 2020 12:58 am

Wed Mar 11, 2020 7:30 am

Hi Masehullah,

Thanks for your inquiry.
Below is the corresponding code in Java. Please download the latest Spire.Doc for Java Version:3.3.0 to have a try.
If there is any question, please provide your input file as well as your desired output. So that we could provide you with a corresponding solution.
Code: Select all
    Document doc = new Document("sample.docx");
    for (Object section : doc.getSections())
    {
        Section sec = (Section)section;
        //Find the text
        TextSelection[] selections = doc.findAllString("Hello", true, true);
        for(TextSelection selection : selections)
        {
            TextRange tr = selection.getAsOneRange();
            Paragraph par = tr.getOwnerParagraph();
            if (par.isInCell())
            {
                //Get table row of the paragraph
                TableRow row = (TableRow)par.getOwner().getOwner()  ;
                Table table = (Table)row.getOwner();
                table.getRows().remove(row);
            }
        }
    }
    doc.saveToFile("result.docx", FileFormat.Docx);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Mar 12, 2020 6:38 am

I am very happy to your quick responses
for our inquiries.
Thank U!

masehullahhami
 
Posts: 3
Joined: Thu Feb 27, 2020 12:58 am

Thu Mar 12, 2020 6:42 am

Dears, Kindly help me with this.
If someone wants to just use spire.doc and
Spire.xls on one java project without spire.office
Is there any other solution.
Kindly let me know.
Thank U!

masehullahhami
 
Posts: 3
Joined: Thu Feb 27, 2020 12:58 am

Thu Mar 12, 2020 7:34 am

Hi Masehullah,

Thanks for your response.
Since each of our products are independent and incompatible, using two products in the same project may cause compatibility issues. Thus, using our Spire.Office is the best solution for you.
Besides, in your case, if you'd like to purchase, you just need to purchase Spire.XLS and Spire.Doc but not need to purchase all the products of our Spire.Office.
Any question, just feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc