java - How does POI generate a word document? And dynamically add data? I did the following -


first, need create file in program, this:

file file = new file(jchooser.getselectedfile().getpath()+".docx"); if (!file.exists()) {     try {         file.createnewfile();     } catch (ioexception e1) {         e1.printstacktrace();     }  } 

then, need add information this:

opcpackage pack = poixmldocument.openpackage(path); //this path file's path;   xwpfdocument doc = new xwpfdocument(pack); xwpfparagraph paragraph = doc.createparagraph(); paragraph.setalignment(wordfont.getalign()); xwpfrun run = paragraph.insertnewrun(paragraph.getruns().size()); run.settext(data); 

when i'm running it, has error this:

exception in thread "awt-eventqueue-0"org.apache.poi.openxml4j.exceptions.invalidoperationexception: can't open specified file: 'c:\users\sks\desktop\17.docx' @ org.apache.poi.openxml4j.opc.zippackage.<init>(zippackage.java:103) @ org.apache.poi.openxml4j.opc.opcpackage.open(opcpackage.java:207) @ org.apache.poi.openxml4j.opc.opcpackage.open(opcpackage.java:186) @ org.apache.poi.poixmldocument.openpackage(poixmldocument.java:67) 

how can solve this?

i have solved problem, use xwpfdocument when create it:

                xwpfdocument xwpfdocument = new xwpfdocument();                 file file = new  file("hello.docx");                 if (!file.exists()) {                     try {                         file.createnewfile();                     } catch (ioexception e1) {                         e1.printstacktrace();                     }                  }                 try {                     xwpfdocument.write(new fileoutputstream(file));                 } catch (filenotfoundexception e1) {                     // todo auto-generated catch block                     e1.printstacktrace();                 } catch (ioexception e1) {                     // todo auto-generated catch block                     e1.printstacktrace();                 } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -