easypoi导出word表格
生活随笔
收集整理的這篇文章主要介紹了
easypoi导出word表格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、maven依賴
復制 <!--easypoi 自動POI依賴版本為3.15--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.3.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.3.0</version></dependency><!--注意:word中要使用循環等標簽必須單獨導入以下依賴--><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version></dependency>?
二、word模板制作
?
word模板
提示:某些時候寫的表達式感覺總是不能被替換為值,試著把表達式復制到文本工具里面再復制回來。
三、工具類
復制package net.ifok.demo.easypoiword;import cn.afterturn.easypoi.word.WordExportUtil; import org.apache.poi.xwpf.usermodel.XWPFDocument;import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;public class WordUtil {/*** 導出word(2007版本docx)** @param templateWordPath* @param dataMap* @return* @throws Exception*/public static byte[] exportWord(String templateWordPath, Map<String, Object> dataMap) throws Exception {File tf = new File(templateWordPath);if (!tf.exists() || !tf.isFile()) {throw new RuntimeException("File [" + templateWordPath + "] Not Found Or Not File.");}XWPFDocument document = WordExportUtil.exportWord07(templateWordPath, dataMap);ByteArrayOutputStream bos = new ByteArrayOutputStream();document.write(bos);return bos.toByteArray();}public static void main(String[] args) {try {try (FileOutputStream fos = new FileOutputStream("d:/test/exp.docx")) {Map<String, Object> dataMap = new HashMap<>();dataMap.put("title", "個人信息");Map<String, String> user = new HashMap<>();user.put("name", "張三");user.put("age", "22");user.put("address", "重慶渝北區");user.put("other", "籃球");dataMap.put("user", user);List<Map<String, String>> jobs = new ArrayList<>();Map<String, String> job;for (int i = 0; i < 5; i++) {job = new HashMap<>();job.put("name", "公司名稱-" + i);job.put("address", "公司地址:" + i);jobs.add(job);}dataMap.put("jobs",jobs);byte[] doc = exportWord("d:/test/template.docx", dataMap);fos.write(doc);fos.flush();}} catch (Exception e) {e.printStackTrace();}} }四、執行工具類測試
結果圖:
總結
以上是生活随笔為你收集整理的easypoi导出word表格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于扩展卡尔曼滤波EKF和模型预测控制M
- 下一篇: QuotesPlugin for WLW