[简单]使用docx4j制作简单的word(2007)小结

编程入门 行业动态 更新时间:2024-10-27 18:32:58

[<a href=https://www.elefans.com/category/jswz/34/1770983.html style=简单]使用docx4j制作简单的word(2007)小结"/>

[简单]使用docx4j制作简单的word(2007)小结

 如果在使用docx4j的过程中有一些效果自己不会写,上网也没搜索到答案,怎么解决呢?
 可以把word 2007解压,word 2007解压后是很多xml,docx4j的sample例子上面有解压word的代码,如下:    

Java代码
  1. import java.io.File;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.FileOutputStream;   
  4.   
  5. import org.apachemons.io.IOUtils;   
  6. import org.docx4j.openpackaging.exceptions.Docx4JException;   
  7. import org.docx4j.openpackaging.io3.Load3;   
  8. import org.docx4j.openpackaging.io3.Save;   
  9. import org.docx4j.openpackaging.io3.stores.UnzippedPartStore;   
  10. import org.docx4j.openpackaging.io3.stores.ZipPartStore;   
  11. import org.docx4j.openpackaging.packages.OpcPackage;   
  12. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;   
  13.   
  14. public class Word_解压_Unzip_S3_Test {   
  15.     public static void main(String[] args) throws Exception {   
  16.         Word_解压_Unzip_S3_Test t = new Word_解压_Unzip_S3_Test();   
  17.         //t.unzipWord("f:/saveFile/temp/test_t.docx","f:/saveFile/temp/Unzip_3");   
  18.         t.unzipWord("f:/saveFile/temp/img_word.docx","f:/saveFile/temp/Unzip_8");   
  19.         //t.zipXml("f:/saveFile/temp/Unzip_2", "f:/saveFile/temp/test_t2.docx");   
  20.     }   
  21.   
  22.     public void unzipWord(String fileName,String outFilePath) throws Exception {   
  23.         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage   
  24.                 .load(new java.io.File(fileName));   
  25.         File baseDir = new File(outFilePath);   
  26.         baseDir.mkdir();   
  27.         UnzippedPartStore ups = new UnzippedPartStore(baseDir);   
  28.         ups.setSourcePartStore(wordMLPackage.getSourcePartStore());   
  29.         Save saver = new Save(wordMLPackage, ups);   
  30.         saver.save(null);   
  31.     }   
  32.   
  33.     public void zipXml(String inputfilepath, String outFilePath)   
  34.             throws Exception {   
  35.         System.out.println(inputfilepath);   
  36.   
  37.         // Load the docx   
  38.         File baseDir = new File(inputfilepath);   
  39.         UnzippedPartStore partLoader = new UnzippedPartStore(baseDir);   
  40.         final Load3 loader = new Load3(partLoader);   
  41.         OpcPackage opc = loader.get();   
  42.   
  43.         // Save it zipped   
  44.         ZipPartStore zps = new ZipPartStore();   
  45.         zps.setSourcePartStore(opc.getSourcePartStore());   
  46.         Save saver = new Save(opc, zps);   
  47.         FileOutputStream fos = null;   
  48.         try {   
  49.             fos = new FileOutputStream(new File(outFilePath));   
  50.             saver.save(fos);   
  51.         } catch (FileNotFoundException e) {   
  52.             throw new Docx4JException("Couldn't save " + outFilePath, e);   
  53.         } finally {   
  54.             IOUtils.closeQuietly(fos);   
  55.         }   
  56.     }   
  57. }  
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;import org.apachemons.io.IOUtils;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io3.Load3;
import org.docx4j.openpackaging.io3.Save;
import org.docx4j.openpackaging.io3.stores.UnzippedPartStore;
import org.docx4j.openpackaging.io3.stores.ZipPartStore;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;public class Word_解压_Unzip_S3_Test {public static void main(String[] args) throws Exception {Word_解压_Unzip_S3_Test t = new Word_解压_Unzip_S3_Test();//t.unzipWord("f:/saveFile/temp/test_t.docx","f:/saveFile/temp/Unzip_3");t.unzipWord("f:/saveFile/temp/img_word.docx","f:/saveFile/temp/Unzip_8");//t.zipXml("f:/saveFile/temp/Unzip_2", "f:/saveFile/temp/test_t2.docx");}public void unzipWord(String fileName,String outFilePath) throws Exception {WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(fileName));File baseDir = new File(outFilePath);baseDir.mkdir();UnzippedPartStore ups = new UnzippedPartStore(baseDir);ups.setSourcePartStore(wordMLPackage.getSourcePartStore());Save saver = new Save(wordMLPackage, ups);saver.save(null);}public void zipXml(String inputfilepath, String outFilePath)throws Exception {System.out.println(inputfilepath);// Load the docxFile baseDir = new File(inputfilepath);UnzippedPartStore partLoader = new UnzippedPartStore(baseDir);final Load3 loader = new Load3(partLoader);OpcPackage opc = loader.get();// Save it zippedZipPartStore zps = new ZipPartStore();zps.setSourcePartStore(opc.getSourcePartStore());Save saver = new Save(opc, zps);FileOutputStream fos = null;try {fos = new FileOutputStream(new File(outFilePath));saver.save(fos);} catch (FileNotFoundException e) {throw new Docx4JException("Couldn't save " + outFilePath, e);} finally {IOUtils.closeQuietly(fos);}}
}

   里面的内容为:

   

     

       和excel 2007解压的内容差不多。

       word内容在document.xml,页眉页脚在header.xml和footer.xml中,想要什么效果粗暴点就是把xml内容替换掉,后面会讲到这方面的内容,也可以这样做,先把原word解压一份,在word上面添加自己想要的效果,在解压,对比2份文件对应的xml内容就知道要修改那些类了,如下:
    

      想要好看点就先格式化xml,如下:

     

      图中就是设置页眉底部的边框xml代码部分,位置在p-->ppr->pbdr下面,名字叫between,相应的代码可以这么写:

    

Java代码
  1. P p = factory.createP();   
  2. PPr pPr = p.getPPr();   
  3. if (pPr == null) {   
  4.     pPr = factory.createPPr();   
  5. }   
  6. PBdr pBdr=pPr.getPBdr();   
  7. if(pBdr==null){   
  8.     pBdr=factory.createPPrBasePBdr();   
  9. }   
  10. CTBorder value=new CTBorder();   
  11. value.setVal(STBorder.SINGLE);   
  12. value.setColor("000000");   
  13. value.setSpace(new BigInteger("0"));   
  14. value.setSz(new BigInteger("3"));   
  15. pBdr.setBetween(value);   
  16. pPr.setPBdr(pBdr);  
P p = factory.createP();
PPr pPr = p.getPPr();
if (pPr == null) {pPr = factory.createPPr();
}
PBdr pBdr=pPr.getPBdr();
if(pBdr==null){pBdr=factory.createPPrBasePBdr();
}
CTBorder value=new CTBorder();
value.setVal(STBorder.SINGLE);
value.setColor("000000");
value.setSpace(new BigInteger("0"));
value.setSz(new BigInteger("3"));
pBdr.setBetween(value);
pPr.setPBdr(pBdr);

       setBetween的时候就知道里面是什么类型的变量了,其他的差不多也可以这么做。

       在使用docx4j的过程中,有种感觉就是docx4j其实是在拼xml,再压缩为word,有种自己替换变量制作word的冲动,这个想法已经有人写过了,他的做法如下, 

      1)制作一份模版word,变量名用${}括起来

      2)解压word为xml,替换xml内容,再压缩为word

       代码如下:
      

Java代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5. package com.test;   
  6. import java.io.File;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.util.Enumeration;   
  11. import java.util.zip.ZipEntry;   
  12. import java.util.zip.ZipException;   
  13. import java.util.zip.ZipFile;   
  14. import org.apachemons.io.FileUtils;   
  15. /**  
  16.  *   
  17.  * @author eg  
  18.  */  
  19. public class ZipHelper {   
  20.     private static final int BUFFER = 1024;   
  21.     public void zipFolder(String folderToZip, String destZip,   
  22.             boolean includeInitialFolder) throws Exception {   
  23.         zipFolder(new File(folderToZip), new File(destZip),   
  24.                 includeInitialFolder);   
  25.     }   
  26.     public void zipFolder(String folderToZip, String destZip) throws Exception {   
  27.         zipFolder(new File(folderToZip), new File(destZip));   
  28.     }   
  29.     public static void zipFolder(File folderToZip, File destZip)   
  30.             throws Exception {   
  31.         zipFolder(folderToZip, destZip, true);   
  32.     }   
  33.     public static void zipFolder(File folderToZip, File destZip,   
  34.             boolean includeInitialFolder) throws Exception {   
  35.         ZipFolderHelper helper = new ZipFolderHelper();   
  36.         helper.setIncludeInitialFolder(includeInitialFolder);   
  37.         helper.process(folderToZip, destZip);   
  38.     }   
  39.     public static void unzip(String inFile, String outFolder)   
  40.             throws IOException {   
  41.         unzip(new File(inFile), new File(outFolder));   
  42.     }   
  43.     /**  
  44.      * @param zipFile  
  45.      * @param dest  
  46.      */  
  47.     public static void unzip(File in, File dest) throws ZipException,   
  48.             IOException {   
  49.         FileUtils.deleteDirectory(dest);   
  50.         ZipFile zipFile = new ZipFile(in);   
  51.         Enumeration<?> files = zipFile.entries();   
  52.         File f = null;   
  53.         FileOutputStream fos = null;   
  54.         while (files.hasMoreElements()) {   
  55.             try {   
  56.                 ZipEntry entry = (ZipEntry) files.nextElement();   
  57.                 InputStream eis = zipFile.getInputStream(entry);   
  58.                 byte[] buffer = new byte[BUFFER];   
  59.                 int bytesRead = 0;   
  60.                 f = new File(dest.getAbsolutePath() + File.separator   
  61.                         + entry.getName());   
  62.                 if (entry.isDirectory()) {   
  63.                     f.mkdirs();   
  64.                     continue;   
  65.                 } else {   
  66.                     f.getParentFile().mkdirs();   
  67.                     f.createNewFile();   
  68.                 }   
  69.                 fos = new FileOutputStream(f);   
  70.                 while ((bytesRead = eis.read(buffer)) != -1) {   
  71.                     fos.write(buffer, 0, bytesRead);   
  72.                 }   
  73.             } finally {   
  74.                 if (fos != null) {   
  75.                     try {   
  76.                         fos.close();   
  77.                     } catch (IOException e) {   
  78.                         // ignore   
  79.                     }   
  80.                 }   
  81.             }   
  82.         }   
  83.     }   
  84. }  
/** To change this template, choose Tools | Templates* and open the template in the editor.*/
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apachemons.io.FileUtils;
/*** * @author eg*/
public class ZipHelper {private static final int BUFFER = 1024;public void zipFolder(String folderToZip, String destZip,boolean includeInitialFolder) throws Exception {zipFolder(new File(folderToZip), new File(destZip),includeInitialFolder);}public void zipFolder(String folderToZip, String destZip) throws Exception {zipFolder(new File(folderToZip), new File(destZip));}public static void zipFolder(File folderToZip, File destZip)throws Exception {zipFolder(folderToZip, destZip, true);}public static void zipFolder(File folderToZip, File destZip,boolean includeInitialFolder) throws Exception {ZipFolderHelper helper = new ZipFolderHelper();helper.setIncludeInitialFolder(includeInitialFolder);helper.process(folderToZip, destZip);}public static void unzip(String inFile, String outFolder)throws IOException {unzip(new File(inFile), new File(outFolder));}/*** @param zipFile* @param dest*/public static void unzip(File in, File dest) throws ZipException,IOException {FileUtils.deleteDirectory(dest);ZipFile zipFile = new ZipFile(in);Enumeration<?> files = zipFile.entries();File f = null;FileOutputStream fos = null;while (files.hasMoreElements()) {try {ZipEntry entry = (ZipEntry) files.nextElement();InputStream eis = zipFile.getInputStream(entry);byte[] buffer = new byte[BUFFER];int bytesRead = 0;f = new File(dest.getAbsolutePath() + File.separator+ entry.getName());if (entry.isDirectory()) {f.mkdirs();continue;} else {f.getParentFile().mkdirs();f.createNewFile();}fos = new FileOutputStream(f);while ((bytesRead = eis.read(buffer)) != -1) {fos.write(buffer, 0, bytesRead);}} finally {if (fos != null) {try {fos.close();} catch (IOException e) {// ignore}}}}}
}

  

Java代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5. package com.test;   
  6. import java.io.File;   
  7. import java.io.FileInputStream;   
  8. import java.io.FileOutputStream;   
  9. import java.util.zip.ZipEntry;   
  10. import java.util.zip.ZipOutputStream;   
  11. /**  
  12.  *   
  13.  * @author eg  
  14.  */  
  15. class ZipFolderHelper {   
  16.     private boolean includeInitialFolder;   
  17.     public ZipFolderHelper() {   
  18.     }   
  19.     public void setIncludeInitialFolder(boolean includeInitialFolder) {   
  20.         this.includeInitialFolder = includeInitialFolder;   
  21.     }   
  22.     public void process(File folderToZip, File destZip) throws Exception {   
  23.         if (destZip.exists()) {   
  24.             destZip.delete();   
  25.         }   
  26.         ZipOutputStream zip = null;   
  27.         FileOutputStream fileWriter = null;   
  28.         fileWriter = new FileOutputStream(destZip);   
  29.         zip = new ZipOutputStream(fileWriter);   
  30.         addFolderToZip("", folderToZip.getPath(), zip);   
  31.         zip.flush();   
  32.         zip.close();   
  33.     }   
  34.     private void addFileToZip(String path, String srcFile, ZipOutputStream zip)   
  35.             throws Exception {   
  36.         File folder = new File(srcFile);   
  37.         if (folder.isDirectory()) {   
  38.             addFolderToZip(path, srcFile, zip);   
  39.         } else {   
  40.             FileInputStream in = null;   
  41.             try {   
  42.                 byte[] buf = new byte[1024];   
  43.                 int len;   
  44.                 in = new FileInputStream(srcFile);   
  45.                 String zeName = path + "/" + folder.getName();   
  46.                 if (!includeInitialFolder) {   
  47.                     int idx = zeName.indexOf("/");   
  48.                     zeName = zeName.substring(idx + 1);   
  49.                 }   
  50.                 zip.putNextEntry(new ZipEntry(zeName));   
  51.                 while ((len = in.read(buf)) > 0) {   
  52.                     zip.write(buf, 0, len);   
  53.                 }   
  54.             } finally {   
  55.                 if (in != null) {   
  56.                     in.close();   
  57.                 }   
  58.             }   
  59.         }   
  60.     }   
  61.     private void addFolderToZip(String path, String srcFolder,   
  62.             ZipOutputStream zip) throws Exception {   
  63.         File folder = new File(srcFolder);   
  64.         for (String fileName : folder.list()) {   
  65.             if (path.equals("")) {   
  66.                 addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);   
  67.             } else {   
  68.                 addFileToZip(path + "/" + folder.getName(), srcFolder + "/"  
  69.                         + fileName, zip);   
  70.             }   
  71.         }   
  72.     }   
  73. }  
/** To change this template, choose Tools | Templates* and open the template in the editor.*/
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*** * @author eg*/
class ZipFolderHelper {private boolean includeInitialFolder;public ZipFolderHelper() {}public void setIncludeInitialFolder(boolean includeInitialFolder) {this.includeInitialFolder = includeInitialFolder;}public void process(File folderToZip, File destZip) throws Exception {if (destZip.exists()) {destZip.delete();}ZipOutputStream zip = null;FileOutputStream fileWriter = null;fileWriter = new FileOutputStream(destZip);zip = new ZipOutputStream(fileWriter);addFolderToZip("", folderToZip.getPath(), zip);zip.flush();zip.close();}private void addFileToZip(String path, String srcFile, ZipOutputStream zip)throws Exception {File folder = new File(srcFile);if (folder.isDirectory()) {addFolderToZip(path, srcFile, zip);} else {FileInputStream in = null;try {byte[] buf = new byte[1024];int len;in = new FileInputStream(srcFile);String zeName = path + "/" + folder.getName();if (!includeInitialFolder) {int idx = zeName.indexOf("/");zeName = zeName.substring(idx + 1);}zip.putNextEntry(new ZipEntry(zeName));while ((len = in.read(buf)) > 0) {zip.write(buf, 0, len);}} finally {if (in != null) {in.close();}}}}private void addFolderToZip(String path, String srcFolder,ZipOutputStream zip) throws Exception {File folder = new File(srcFolder);for (String fileName : folder.list()) {if (path.equals("")) {addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);} else {addFileToZip(path + "/" + folder.getName(), srcFolder + "/"+ fileName, zip);}}}
}

 

Java代码
  1. package com.test;   
  2. import java.io.File;   
  3. import java.io.FileNotFoundException;   
  4. import java.io.IOException;   
  5. import java.util.Map;   
  6. import org.apachemons.io.FileUtils;   
  7. import org.apachemons.lang3.StringUtils;   
  8. /**  
  9.  *   
  10.  * @author eg  
  11.  */  
  12. public class DocxTemplater {   
  13.     private File unzipFolder;   
  14.     private File contentXmlFile;   
  15.     private String placeholderBefore = "${";   
  16.     private String placeholderAfter = "}";   
  17.     private static final String PATH_TO_CONTENT = "word/document.xml";   
  18.     public DocxTemplater(String pathToDocx, String before, String after) {   
  19.         this(new File(pathToDocx), before, after);   
  20.     }   
  21.     public DocxTemplater(String pathToDocx) {   
  22.         this(new File(pathToDocx));   
  23.     }   
  24.     public DocxTemplater() {   
  25.     }   
  26.     public DocxTemplater(File unzipFolder) {   
  27.         this.unzipFolder = unzipFolder;   
  28.     }   
  29.     public DocxTemplater(File unzipFolder, String before, String after) {   
  30.         this.unzipFolder = unzipFolder;   
  31.         this.placeholderBefore = before;   
  32.         this.placeholderAfter = after;   
  33.     }   
  34.     public void process(String destDocx, Map<String, Object> params) {   
  35.         process(new File(destDocx), params);   
  36.     }   
  37.     private void setup(File destDocx) throws IOException {   
  38.         ZipHelper.unzip(destDocx, unzipFolder);   
  39.         contentXmlFile = new File(unzipFolder, PATH_TO_CONTENT);   
  40.     }   
  41.     public void process(File destDocx, Map<String, Object> params) {   
  42.         try {   
  43.             setup(destDocx);   
  44.             if (!unzipFolder.exists()) {   
  45.                 unzipFolder.mkdir();   
  46.             }   
  47.             if (!contentXmlFile.exists()) {   
  48.                 throw new FileNotFoundException(   
  49.                         contentXmlFile.getAbsolutePath());   
  50.             }   
  51.             String template = FileUtils.readFileToString(contentXmlFile,   
  52.                     "UTF-8");   
  53.             for (Map.Entry<String, Object> entry : params.entrySet()) {   
  54.                 template = StringUtils.replace(template, placeholderBefore   
  55.                         + entry.getKey() + placeholderAfter,   
  56.                         String.valueOf(entry.getValue()));   
  57.             }   
  58.             String destDocxString = destDocx.getPath();   
  59.             String noExtPathString = destDocxString.substring(0,   
  60.                     destDocxString.lastIndexOf("."))   
  61.                     + "_bak";   
  62.             File noExtPath = new File(noExtPathString);   
  63.             destDocx.delete();   
  64.             FileUtils.deleteDirectory(noExtPath);   
  65.             FileUtils.copyDirectory(unzipFolder, noExtPath);   
  66.             FileUtils.writeStringToFile(new File(noExtPath, PATH_TO_CONTENT),   
  67.                     template, "UTF-8");   
  68.             ZipHelper.zipFolder(noExtPath, destDocx, false);   
  69.             FileUtils.deleteDirectory(noExtPath);   
  70.         } catch (Exception e) {   
  71.             throw new RuntimeException(e);   
  72.         }   
  73.     }   
  74. }  
package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import org.apachemons.io.FileUtils;
import org.apachemons.lang3.StringUtils;
/*** * @author eg*/
public class DocxTemplater {private File unzipFolder;private File contentXmlFile;private String placeholderBefore = "${";private String placeholderAfter = "}";private static final String PATH_TO_CONTENT = "word/document.xml";public DocxTemplater(String pathToDocx, String before, String after) {this(new File(pathToDocx), before, after);}public DocxTemplater(String pathToDocx) {this(new File(pathToDocx));}public DocxTemplater() {}public DocxTemplater(File unzipFolder) {this.unzipFolder = unzipFolder;}public DocxTemplater(File unzipFolder, String before, String after) {this.unzipFolder = unzipFolder;this.placeholderBefore = before;this.placeholderAfter = after;}public void process(String destDocx, Map<String, Object> params) {process(new File(destDocx), params);}private void setup(File destDocx) throws IOException {ZipHelper.unzip(destDocx, unzipFolder);contentXmlFile = new File(unzipFolder, PATH_TO_CONTENT);}public void process(File destDocx, Map<String, Object> params) {try {setup(destDocx);if (!unzipFolder.exists()) {unzipFolder.mkdir();}if (!contentXmlFile.exists()) {throw new FileNotFoundException(contentXmlFile.getAbsolutePath());}String template = FileUtils.readFileToString(contentXmlFile,"UTF-8");for (Map.Entry<String, Object> entry : params.entrySet()) {template = StringUtils.replace(template, placeholderBefore+ entry.getKey() + placeholderAfter,String.valueOf(entry.getValue()));}String destDocxString = destDocx.getPath();String noExtPathString = destDocxString.substring(0,destDocxString.lastIndexOf("."))+ "_bak";File noExtPath = new File(noExtPathString);destDocx.delete();FileUtils.deleteDirectory(noExtPath);FileUtils.copyDirectory(unzipFolder, noExtPath);FileUtils.writeStringToFile(new File(noExtPath, PATH_TO_CONTENT),template, "UTF-8");ZipHelper.zipFolder(noExtPath, destDocx, false);FileUtils.deleteDirectory(noExtPath);} catch (Exception e) {throw new RuntimeException(e);}}
}

  

Java代码
  1. package com.test;   
  2.   
  3. import java.io.File;   
  4. import java.util.HashMap;   
  5. //例子来自Github,原地址忘了,代码被我改过一部分   
  6. public class DocxTemplater_Test {   
  7.     public static void main(String[] args) {   
  8.         DocxTemplater templater = new DocxTemplater("f:/saveFile/temp/test_c");   
  9.         File f = new File(   
  10.                 "f:/saveFile/temp/doc_template.docx");   
  11.         HashMap<String, Object> map = new HashMap<String, Object>();   
  12.         map.put("TITLE", "这是替换后的文档");   
  13.         map.put("XUHAO", "1");   
  14.         map.put("NAME", "梅花");   
  15.         map.put("NAME2", "杏花");   
  16.         map.put("WORD", "问世间情为何物");   
  17.         map.put("DATA", "2014-9-28");   
  18.         map.put("BOSS", "Github");   
  19.         templater.process(f, map);   
  20.     }   
  21. }  
package com.test;import java.io.File;
import java.util.HashMap;
//例子来自Github,原地址忘了,代码被我改过一部分
public class DocxTemplater_Test {public static void main(String[] args) {DocxTemplater templater = new DocxTemplater("f:/saveFile/temp/test_c");File f = new File("f:/saveFile/temp/doc_template.docx");HashMap<String, Object> map = new HashMap<String, Object>();map.put("TITLE", "这是替换后的文档");map.put("XUHAO", "1");map.put("NAME", "梅花");map.put("NAME2", "杏花");map.put("WORD", "问世间情为何物");map.put("DATA", "2014-9-28");map.put("BOSS", "Github");templater.process(f, map);}
}

   效果如下:

  

 

      缺点:页眉页尾没替换,除非自己替换header.xml和footer.xml中的变量,对表格无效,想要替换变量制作表格,可以搜索下iteye的博文,以前有哥们写过,其实就是拿到table,然后自己往里面塞数据。
     全文完。

更多推荐

[简单]使用docx4j制作简单的word(2007)小结

本文发布于:2024-03-23 14:54:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单   小结   word   docx4j

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!