java - XWPF - remove cell text -
i have .docx file contains single table. want remove text rows 2 end. method mytable.getrow(somecounter).getcell(somecounter2).settext("")
doesn't work concatenates " " existing value. tried making xwpfrun , doing run.settext("")
created mytable.getrow(sc).getcell(sc2).getparagraphs().get(0).createrun()
doesn't work aswell.
also tried solution this thread, no luck time :(
any ideas how remove text cell? idea make new table scratch , fill content seems arduous.
your requirement "to remove text rows 2 end" little bit complicated fulfil since word
table cell can contain other things text.
consider following table:
so if requirement remove all content rows 2 end, replace cells new clean ones. or @ least ones have empty paragraph in it.
import java.io.fileoutputstream; import java.io.fileinputstream; import java.util.list; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.cttc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.cttc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.ctp; /* needs full ooxml-schemas-1.3.jar mentioned in https://poi.apache.org/faq.html#faq-n10025 since ctrowimpl not shipped poi-ooxml-schemas-3.13-*.jar */ public class wordcleantablerows { public static void main(string[] args) throws exception { fileinputstream fis = new fileinputstream("document.docx"); xwpfdocument doc = new xwpfdocument(fis); list<xwpftable> tables = doc.gettables(); xwpftable table = tables.get(0); xwpftablerow[] rows = table.getrows().toarray(new xwpftablerow[0]); (int r = 0; r < rows.length; r++) { if (r > 0) { xwpftablerow row = rows[r]; cttc[] cells = row.getctrow().gettclist().toarray(new cttc[0]); (int c = 0; c < cells.length; c++) { cttc cttc = cells[c]; //clear paragraphs in cell, keep cell styles cttc.setparray(new ctp[] {ctp.factory.newinstance()}); cells[c] = cttc; } row.getctrow().settcarray(cells); //system.out.println(row.getctrow()); } } doc.write(new fileoutputstream("new document.docx")); } }
this needs full ooxml-schemas-1.3.jar mentioned in https://poi.apache.org/faq.html#faq-n10025 since ctrowimpl not shipped poi-ooxml-schemas-3.13-*.jar.
without full ooxml-schemas-1.3.jar remove rows except first 1 , add new ones.
import java.io.fileoutputstream; import java.io.fileinputstream; import java.util.list; import org.apache.poi.xwpf.usermodel.*; public class wordcleantablerows2 { public static void main(string[] args) throws exception { fileinputstream fis = new fileinputstream("document.docx"); xwpfdocument doc = new xwpfdocument(fis); list<xwpftable> tables = doc.gettables(); xwpftable table = tables.get(0); xwpftablerow[] rows = table.getrows().toarray(new xwpftablerow[0]); (int r = 0; r < rows.length; r++) { if (r > 0) { xwpftablerow row = rows[r]; table.removerow(1); //remove second row. others shift upwards table.createrow(); //add new row @ end } } doc.write(new fileoutputstream("new document.docx")); } }
edit:
the following should work without ooxml-schemas-1.3.jar , same first example.
import java.io.fileoutputstream; import java.io.fileinputstream; import java.util.list; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.ctp; import org.openxmlformats.schemas.wordprocessingml.x2006.main.stonoff; import java.math.biginteger; public class wordcleantablerows3 { public static void main(string[] args) throws exception { fileinputstream fis = new fileinputstream("document.docx"); xwpfdocument doc = new xwpfdocument(fis); list<xwpftable> tables = doc.gettables(); xwpftable table = tables.get(0); xwpftablerow[] rows = table.getrows().toarray(new xwpftablerow[0]); (int r = 0; r < rows.length; r++) { if (r > 0) { xwpftablerow row = rows[r]; list<xwpftablecell> cells = row.gettablecells(); (xwpftablecell cell : cells) { //get cttc , replace ctparray 1 empty ctp cell.getcttc().setparray(new ctp[] {ctp.factory.newinstance()}); //set default styles paragraphs in cells: //http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/wordprocessingml/x2006/main/ctpararpr.java ctp ctp = cell.getcttc().getparray(0); ctp.addnewppr(); ctp.getppr().addnewrpr(); ctp.getppr().getrpr().addnewb().setval(stonoff.on); ctp.getppr().getrpr().addnewcolor().setval("ff0000"); ctp.getppr().getrpr().addnewsz().setval(biginteger.valueof(40)); } } } doc.write(new fileoutputstream("new document.docx")); } }
the org.openxmlformats.schemas.wordprocessingml.x2006.main.ctp shipped poi-ooxml-schemas-3.13-*.jar.
Comments
Post a Comment