Java reads and writes the word. java dynamically writes the template file. javaword

Source: Internet
Author: User

Java reads and writes the word. java dynamically writes the template file. javaword

Import java. io. byteArrayOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. util. hashMap; import java. util. iterator; import java. util. map; import org. apache. poi. hwpf. HWPFDocument; import org. apache. poi. hwpf. model. fieldsDocumentPart; import org. apache. poi. hwpf. usermodel. field; import org. apache. poi. hwpf. usermodel. fields; import org. apache. poi. hwpf. usermodel. paragraph; import org. apache. poi. hwpf. usermodel. range; import org. apache. poi. hwpf. usermodel. table; import org. apache. poi. hwpf. usermodel. tableCell; import org. apache. poi. hwpf. usermodel. tableIterator; import org. apache. poi. hwpf. usermodel. tableRow;/*** @ author zhangchaochao * @ date 2015-12-4 10:30:59 */public class MSWordPoi4 {/*** @ param args */public static void main (String [] args) {Map <String, String> map = new HashMap <String, String> (); map. put ("$ {sub}", "Hunan University"); map. put ("$ {item2.school}", "Hunan University"); map. put ("$ {item2.address}", "Hunan"); // map. put ("$ {item1.name}", "Wang Wu 1"); // map. put ("$ {item1.numberStudent}", "002"); // map. put ("$ {item1.sex}", "male 2"); // map. put ("$ {item1.age}", "19"); String srcPath = "D: \ casting process card .doc"; readwriteWord (srcPath, map );} /***** read and modify the word ** @ param filePath * word template path and name * @ param map * data to be filled, read */public static void readwriteWord (String filePath, Map <String, String> map) from the database {// read the word template // String fileDir = new // File (base. getFile (), "http://www.cnblogs.com/http://www.cnblogs.com/../doc "). getCanonicalPath (); FileInputStream in = null; try {in = new FileInputStream (new File (filePath);} catch (FileNotFoundException e1) {e1.printStackTrace ();} HWPFDocument hdt = null; try {hdt = new HWPFDocument (in);} catch (IOException e1) {e1.printStackTrace ();} Fields fields = hdt. getFields (); Iterator <Field> it = fields. getFields (FieldsDocumentPart. MAIN ). iterator (); while (it. hasNext () {System. out. println (it. next (). getType ();} // read the content of the word text Range = hdt. getRange (); TableIterator tableIt = new TableIterator (range); // In the iteration document, table int ii = 0; while (tableIt. hasNext () {Table tb = (Table) tableIt. next (); ii ++; System. out. println ("no." + ii + "table data ................... "); // iteration row, starting from 0 by default for (int I = 0; I <tb. numRows (); I ++) {TableRow tr = tb. getRow (I); // read-only first 8 rows, subject part if (I> = 8) break; // iteration column, starting from 0 by default for (int j = 0; j <tr. numCells (); j ++) {TableCell td = tr. getCell (j); // obtain the cell content. // obtain the cell content for (int k = 0; k <td. numParagraphs (); k ++) {Paragraph para = td. getParagraph (k); String s = para. text (); System. out. println (s);} // end for} // end while // System. out. println (range. text (); // Replace the text content for (Map. entry <String, String> entry: map. entrySet () {range. replaceText (entry. getKey (), entry. getValue ();} ByteArrayOutputStream ostream = new ByteArrayOutputStream (); String fileName = "" + System. currentTimeMillis (); fileName + = ". doc "; FileOutputStream out = null; try {out = new FileOutputStream (" D:/"+ fileName, true);} catch (FileNotFoundException e) {e. printStackTrace ();} try {hdt. write (ostream);} catch (IOException e) {e. printStackTrace ();} // output byte stream try {out. write (ostream. toByteArray ();} catch (IOException e) {e. printStackTrace ();} try {out. close ();} catch (IOException e) {e. printStackTrace ();} try {ostream. close ();} catch (IOException e) {e. printStackTrace ();}}}

Java read/write word java Dynamic Write template file 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.