1. 程式人生 > >使用模板生成word文檔

使用模板生成word文檔

dna head exists pri reat tables hang exceptio website

使用poi-tl根據模板生成word文檔,在porm.xml中添加poi-tl依賴,直接上代碼

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.render.RenderAPI;
import com.thinkgem.jeesite.common.config.Global;

public class WordModule {
    
    
/*public static void main(String[] args) throws Exception { //構造數據 XWPFTemplate doc = XWPFTemplate .create("D:\\test\\2.docx"); List<RenderData> headers = new ArrayList<RenderData>(); headers.add(new TextRenderData("d0d0d0","number")); headers.add(new TextRenderData("d0d0d0", "content")); List<Object> data = new ArrayList<Object>(); data.add("1;add new grammer"); data.add("2;support insert table"); data.add("3;support more style"); TableRenderData tables = new TableRenderData(headers, data,"",2017); Map<String, Object> datas = new HashMap<String, Object>(); datas.put("logo", new PictureRenderData(600,480,"D:\\test\\1.jpg")); datas.put("title", new TextRenderData("9d55b8", "Deeply in love with the things you love,\\n just deepoove.")); datas.put("changeLog",tables); datas.put("website", "
http://www.baidu.com"); //讀取模板,進行渲染 RenderAPI.render(doc, datas); System.out.println(tables.getWidth()); //輸出渲染後的文件 File file = new File("D:\\test\\out.docx"); if(file.exists()){ file.delete(); } FileOutputStream out = new FileOutputStream("D:\\test\\out.docx"); doc.write(out); out.flush(); out.close(); }
*/ /** * data為添加文字或者圖片對象 * 文字: * */ public static void Module2Word(Map<String ,Object> datas,String moduleFileName,String docSavedName) throws IOException{ XWPFTemplate doc = XWPFTemplate .create(Global.getConfig("bizz.modulepath")+moduleFileName); RenderAPI.render(doc, datas); File file = new File(Global.getConfig("bizz.docpath")+docSavedName); if(file.exists()){ file.delete(); } FileOutputStream out = new FileOutputStream("D:\\test\\out.docx"); doc.write(out); out.flush(); out.close(); } }

使用模板生成word文檔