使用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(); } }
时间: 2024-10-11 07:12:21