如何向word文件寫入資料
阿新 • • 發佈:2019-02-13
大致流程:
1.建立模板docx並取出document.xml
新建一個docx文件,放在D盤命名test_template.docx
2.用winrar開啟test_template.docx,取出word/document.xml
把xml檔案格式化一下看起來更清晰,並且把要替換的內容用freemarker的指令代替,最後將檔案重新命名為test.xml放在D盤下面
3.準備工作完畢,上程式碼了,這個類是把內容填充到xml
- import java.io.File;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Map;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- import freemarker.template.TemplateException;
- public class XmlToExcel {
- private static XmlToExcel tplm = null;
- private Configuration cfg = null;
- private XmlToExcel() {
- cfg = new Configuration();
- try {
- // 註冊tmlplate的load路徑
- // cfg.setClassForTemplateLoading(this.getClass(), "/template/");
- cfg.setDirectoryForTemplateLoading(new File("D:/"));
- } catch (Exception e) {
- }
- }
- private
- if (tplm == null) {
- tplm = new XmlToExcel();
- }
- return tplm.cfg.getTemplate(name);
- }
- /**
- *
- * @param templatefile 模板檔案
- * @param param 需要填充的內容
- * @param out 填充完成輸出的檔案
- * @throws IOException
- * @throws TemplateException
- */
- public static void process(String templatefile, Map param, Writer out) throws IOException, TemplateException {
- // 獲取模板
- Template template = XmlToExcel.getTemplate(templatefile);
- template.setOutputEncoding("UTF-8");
- // 合併資料
- template.process(param, out);
- if (out != null) {
- out.close();
- }
- }
- }
4.這個類是把填充完畢的xml轉成docx
- import java.io.File;
- import java.io.FileInputStream;
- 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 java.util.zip.ZipOutputStream;
- /**
- * 其實docx屬於zip的一種,這裡只需要操作word/document.xml中的資料,其他的資料不用動
- *
- * @author yigehui
- *
- */
- public class XmlToDocx {
- /**
- *
- * @param documentFile 動態生成資料的docunment.xml檔案
- * @param docxTemplate docx的模板
- * @param toFileName 需要匯出的檔案路徑
- * @throws ZipException
- * @throws IOException
- */
- public void outDocx(File documentFile, String docxTemplate, String toFilePath) throws ZipException, IOException {
- try {
- File docxFile = new File(docxTemplate);
- ZipFile zipFile = new ZipFile(docxFile);
- Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
- ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(toFilePath));
- int len = -1;
- byte[] buffer = new byte[1024];
- while (zipEntrys.hasMoreElements()) {
- ZipEntry next = zipEntrys.nextElement();
- InputStream is = zipFile.getInputStream(next);
- // 把輸入流的檔案傳到輸出流中 如果是word/document.xml由我們輸入
- zipout.putNextEntry(new ZipEntry(next.toString()));
- if ("word/document.xml".equals(next.toString())) {
- InputStream in = new FileInputStream(documentFile);
- while ((len = in.read(buffer)) != -1) {
- zipout.write(buffer, 0, len);
- }
- in.close();
- } else {
- while ((len = is.read(buffer)) != -1) {
- zipout.write(buffer, 0, len);
- }
- is.close();
- }
- }
- zipout.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
5.main方法呼叫
- public static void main(String[] args) throws IOException, TemplateException {
- try {
- // xml的檔名
- String xmlTemplate = "test.xml";
- // docx的路徑和檔名
- String docxTemplate = "d:\\test_template.docx";
- // 填充完資料的臨時xml
- String xmlTemp = "d:\\temp.xml";
- // 目標檔名
- String toFilePath = "d:\\test.docx";
- Writer w = new FileWriter(new File(xmlTemp));
- // 1.需要動態傳入的資料
- Map<String, Object> p = new HashMap<String, Object>();
- List<String> students = new ArrayList<String>();
- students.add("張三");
- students.add("李四");
- students.add("王二");
- p.put("ddeptdept", "研發部門");
- p.put("ddatedate", "2016-12-15");
- p.put("dnamename", students);
- // 2.把map中的資料動態由freemarker傳給xml
- XmlToExcel.process(xmlTemplate, p, w);
- // 3.把填充完成的xml寫入到docx中
- XmlToDocx xtd = new XmlToDocx();
- xtd.outDocx(new File(xmlTemp), docxTemplate, toFilePath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }