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.

Mon Mar 27, 2023 10:59 am

Hi,

I'm using Spire.office (8.3.2).
I want to delete a row based on a searched text.

Code: Select all
            Document document = new Document(true);
            document.LoadFromFile(contentRootPath + "/Files/invoice.docx");
            TextSelection selection = document.FindString("[Name]", true, true);


How can I get the cell/column the found text contains?

Thanks

twindberg
 
Posts: 4
Joined: Fri May 25, 2018 1:24 pm

Tue Mar 28, 2023 2:08 am

Thanks for your inquiry.
Please refer to the following code.
Code: Select all
            Document document = new Document("sample.docx");
            // Find all instances of "text_to_find" in the document
            TextSelection[] selections = document.FindAllString("text_to_find", false, true);
            foreach (TextSelection selection in selections)
            {
               
                Spire.Doc.Fields.TextRange textRange = selection.GetAsOneRange();
                Paragraph paragraph = textRange.Owner as Paragraph;
                // get the owner of the paragraph
                DocumentObject owner = paragraph.Owner;
                if (owner.DocumentObjectType.Equals(DocumentObjectType.TableCell))
                {
                    // get the tableCell
                    TableCell tableCell = owner as TableCell;

                    // Get the row that the cell is in
                    TableRow tableRow = tableCell.Owner as TableRow;

                    // Remove the entire row from the table
                    Table table = tableRow.Owner as Table;
                    table.Rows.Remove(tableRow);
                }
            }

If the code does not meet your requirement or you have any other questions, just feel free to contact us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Mar 28, 2023 8:01 am

Hi Triste,

thank you very much. The code is exactly what i was looking for!

twindberg
 
Posts: 4
Joined: Fri May 25, 2018 1:24 pm

Tue Mar 28, 2023 8:10 am

Hi,

Glad to hear that my code works for your issue. If you have any other questions related to our products, just feel free contact us, we are always glad to assist you.
Have a nice day! :D

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc