java - How to add text to a specific position in the word file using Apache poi? -


i have presence list need change depending if student present or not. need student names , putt 'ok' if student present , ' / ' if student absent. wonder how search text in doc file , how put text in particular position.

here word file use

here word file use

i can read docx file, still don't know how add text table.

public class wordfile {     public static void main(string[] args) {         try {              string filepath = system.getproperty("user.home") + "\\desktop\\admin dokument\\em.docx";             fileinputstream fis = new fileinputstream(filepath);             xwpfdocument xdoc = new xwpfdocument(opcpackage.open(fis));             iterator < ibodyelement > bodyelementiterator = xdoc.getbodyelementsiterator();              while (bodyelementiterator.hasnext()) {                 ibodyelement element = bodyelementiterator.next();                  if ("table".equalsignorecase(element.getelementtype().name())) {                     list < xwpftable > tablelist = element.getbody().gettables();                     (xwpftable table: tablelist) {                         system.out.println("total number of rows of table:" + table.getnumberofrows());                         system.out.println(table.gettext());                     }                 }             }         } catch (exception ex) {             ex.printstacktrace();         }     } } 

ok, have found need.

 public class tabletest {      public tabletest() throws ioexception {         string filename = system.getproperty("user.home") + "\\desktop\\admin dokument\\em.docx";         inputstream fis = new fileinputstream(filename);         xwpfdocument document = new xwpfdocument(fis);         list<xwpfparagraph> paragraphs = document.getparagraphs();          (int x=0; x<paragraphs.size();x++)         {             xwpfparagraph paragraph = paragraphs.get(x);             system.out.println(paragraph.getparagraphtext());         }         list<xwpftable> tables = document.gettables();         (int x=0; x<tables.size();x++) {             xwpftable table = tables.get(x);             list<xwpftablerow> tablerows = table.getrows();             tablerows.remove(x);             (int r=0; r<tablerows.size();r++) {                 system.out.println("row " + (r+1) + ":");                 xwpftablerow tablerow = tablerows.get(r);                 list<xwpftablecell> tablecells = tablerow.gettablecells();                 (int c=0; c<tablecells.size(); c++) {                     system.out.print("column "+ (c+1)+ ": ");                     xwpftablecell tablecell = tablecells.get(c);                      string tablecellval = tablecell.gettext();                      if(tablecellval.equals("david tomasson")) {                         string s2 = "/";                         tablecell = table.getrow(r).getcell(c);                         system.out.print("column "+ (c+1));                         system.out.print("row "+ (r+1));                         tablecell = tablecells.get(c+2);// move 1 step right                         if(!tablecellval.isempty()) {                             removeparagraphs(tablecell);                         }                         tablecell.settext(s2);                         tablecell = tablecells.get(c+3);                          if(!tablecellval.isempty()) {                             removeparagraphs(tablecell);//delete old values in cell                         }                         tablecell.settext(s2);                     }                      if ((c+1)==4){                         if (!(tablecellval.equals("hans hansson"))) {                         //if (tablecellval.length()>0){                         //char c1 = tablecellval.charat(0);                         string s2 = "ok";                         //char c2 = s2.charat(0);                         //string test = tablecell.gettext().replace("o"," ");                         if(!tablecellval.isempty()) {                             removeparagraphs(tablecell);                         }                         tablecell.settext(s2);                         }                         //else{                         //tablecell.settext("null");                         // }                         //}                     }                     system.out.println("tablecell.gettext(" + (c) + "):" + tablecellval);                 }             }             system.out.println("\n");         }         outputstream out = new fileoutputstream(filename);         document.write(out);         out.close();     }      private static void removeparagraphs(xwpftablecell tablecell) {     int count = tablecell.getparagraphs().size();     for(int = 0; < count; i++){       tablecell.removeparagraph(i);     }   } } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -