当前位置:网站首页 > Java基础 > 正文

java写docx教程



import com.example.meeting.util.FreeMarkUtils;

import java.io.; import java.util.; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream;

public class FreemarkerTest {

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String document="document.xml"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">outputStream 输出流可以自己定义 浏览器或者文件输出流</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> createDocx(Map dataMap, OutputStream outputStream) { ZipOutputStream zipout </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">//图片配置文件模板 ByteArrayInputStream documentXmlRelsInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, documentXmlRels);</span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">内容模板</span> ByteArrayInputStream documentInput =<span style="color: rgba(0, 0, 0, 1)"> FreeMarkUtils.getFreemarkerContentInputStream(dataMap, document); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最初设计的模板 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">File docxFile = new File(WordUtils.class.getClassLoader().getResource(template).getPath());</span> File docxFile = <span style="color: rgba(0, 0, 255, 1)">new</span> File("xxxx\会议纪要.zip"<span style="color: rgba(0, 0, 0, 1)">);//换成自己的zip路径 </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">docxFile.exists()) { docxFile.createNewFile(); } ZipFile zipFile </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipFile(docxFile); Enumeration</span>&lt;? <span style="color: rgba(0, 0, 255, 1)">extends</span> ZipEntry&gt; zipEntrys =<span style="color: rgba(0, 0, 0, 1)"> zipFile.entries(); zipout </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipOutputStream(outputStream); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始覆盖文档------------------</span> <span style="color: rgba(0, 0, 255, 1)">int</span> len = -1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] buffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[1024<span style="color: rgba(0, 0, 0, 1)">]; </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (zipEntrys.hasMoreElements()) { ZipEntry next </span>=<span style="color: rgba(0, 0, 0, 1)"> zipEntrys.nextElement(); InputStream is </span>=<span style="color: rgba(0, 0, 0, 1)"> zipFile.getInputStream(next); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (next.toString().indexOf("media") &lt; 0<span style="color: rgba(0, 0, 0, 1)">) { zipout.putNextEntry(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ZipEntry(next.getName())); </span><span style="color: rgba(0, 0, 255, 1)">if</span> ("word/document.xml".equals(next.getName())) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果是word/document.xml由我们输入</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (documentInput != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">while</span> ((len = documentInput.read(buffer)) != -1<span style="color: rgba(0, 0, 0, 1)">) { zipout.write(buffer, </span>0<span style="color: rgba(0, 0, 0, 1)">, len); } documentInput.close(); } } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">while</span> ((len = is.read(buffer)) != -1<span style="color: rgba(0, 0, 0, 1)">) { zipout.write(buffer, </span>0<span style="color: rgba(0, 0, 0, 1)">, len); } is.close(); } } } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { System.out.println(</span>"word导出失败:"+<span style="color: rgba(0, 0, 0, 1)">e.getStackTrace()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">logger.error();</span> }<span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(zipout!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { zipout.close(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { System.out.println(</span>"io异常"<span style="color: rgba(0, 0, 0, 1)">); } } </span><span style="color: rgba(0, 0, 255, 1)">if</span>(outputStream!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { outputStream.close(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { System.out.println(</span>"io异常"<span style="color: rgba(0, 0, 0, 1)">); } } } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String arg[]){ Map dataMap</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HashMap(); ArrayList minuteList</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayList(); MinuteTest minuteTest1</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MinuteTest(); minuteTest1.setMeeting_decision_content(</span>"决策1"<span style="color: rgba(0, 0, 0, 1)">); minuteTest1.setMeeting_decision_executor(</span>"执行者1"<span style="color: rgba(0, 0, 0, 1)">); minuteTest1.setMeeting_decision_deadline(</span>"截至日期1"<span style="color: rgba(0, 0, 0, 1)">); minuteList.add(minuteTest1); MinuteTest minuteTest2</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MinuteTest(); minuteTest2.setMeeting_decision_content(</span>"决策2"<span style="color: rgba(0, 0, 0, 1)">); minuteTest2.setMeeting_decision_executor(</span>"执行者2"<span style="color: rgba(0, 0, 0, 1)">); minuteTest2.setMeeting_decision_deadline(</span>"截至日期2"<span style="color: rgba(0, 0, 0, 1)">); minuteList.add(minuteTest2); dataMap.put(</span>"meeting_name", "如何使象牙山发展得更加美好"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"time", "2019-09-15 15:30"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"site", "会议室212"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"organizer", "张三"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"department", "策划部"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"attendee", "谢大脚、谢广坤、刘能"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"meeting_content", "关于象牙山发展中的每个人的义务"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"recorder", "王五"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"checker", "大老板"<span style="color: rgba(0, 0, 0, 1)">); dataMap.put(</span>"minuteList"<span style="color: rgba(0, 0, 0, 1)">,minuteList); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">指定输出docx路径</span> File outFile = <span style="color: rgba(0, 0, 255, 1)">new</span> File("xxx\test.docx"<span style="color: rgba(0, 0, 0, 1)">) ; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { createDocx(dataMap,</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileOutputStream(outFile)); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (FileNotFoundException e) { e.printStackTrace(); } } 

}

版权声明


相关文章:

  • java继承代码教程2026-01-26 19:34:02
  • 南京java教程咨询2026-01-26 19:34:02
  • 袭击java教程2026-01-26 19:34:02
  • nat与java教程2026-01-26 19:34:02
  • java431教程全集2026-01-26 19:34:02
  • java开发实战教程 pdf2026-01-26 19:34:02
  • java 中xml教程2026-01-26 19:34:02
  • java cad 教程2026-01-26 19:34:02
  • java游戏制作教程2026-01-26 19:34:02
  • java教程视频 北京2026-01-26 19:34:02