In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use poi-tl to operate word template, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Use poi-tl to manipulate word template 1. Importing jar packages supports com.deepoove poi-tl 1.8.22. Create a word template
Create a word template in computer E disk: "E:\ templete.docx"
Modify template content
Title: {{title}} work order No.: {{workNo}} invoice date: {{issueDate}} list data: {{* list}} 3. Generate code method / * * generate word document * @ param request * @ param response * / @ RequestMapping (value = "expdoc") public void expdoc (HttpServletRequest request, HttpServletResponse response) {try {String tempFile= "E:\\ templete.docx" according to the word template XWPFTemplate template = XWPFTemplate.compile (tempFile). Render (new HashMap () {put ("title", "Hi, poi-tl Word template engine"); put ("workNo", "20200910-001") Put ("issueDate", "10 September 2020") Put ("list" New NumbericRenderData (new ArrayList () {{/ / Loop list to assign for (String detail:list) { Add (new TextRenderData (detail)) ));}}); response.setContentType ("application/octet-stream"); response.setHeader ("Content-disposition", "attachment") Filename=\ "+" out_template.docx "+"\ "); OutputStream out = response.getOutputStream (); BufferedOutputStream bos = new BufferedOutputStream (out); template.write (bos); template.close (); bos.flush (); bos.close (); out.flush () Out.close ();} catch (Exception e) {e.printStackTrace ();}} 4. Request access
Enter the request address: http://localhost:8080/expdoc in the browser, and the browser automatically downloads the word document, which is named out_template.docx.
5. Data wrapper 5.1 associated entity public class YdgdTemplete {private String demand;// requires private List detailList;// list data} 5.2 access layer / * generate data information according to word template, return document access address * @ param request * @ param response * @ return document access address * @ throws Exception * / @ RequestMapping ("expdoc") @ ResponseBodypublic BasePageData expdoc (HttpServletRequest request, HttpServletResponse response) throws Exception {BasePageData data = new BasePageData () Try {/ / get the root directory String realPath = request.getSession () .getServletContext () .getRealPath ("/"); / / define the saved document directory String fileDir = "/ fileData/doc/"; File saveFile = new File (realPath + fileDir) If (! saveFile.exists ()) {/ / if saveFile.mkdirs () does not exist in the directory; / / create folder} / / generate word document name String fileName = System.currentTimeMillis () + ".docx" / / path information of saved file String docPath = realPath + fileDir + fileName; / / return document path String backPath= fileDir + fileName; / / encapsulated data YdgdTemplete ydgdTemplete = new YdgdTemplete (); ydgdTemplete.setDemand ("generate test data") List list = new ArrayList (); list.add ("Article 1 record"); list.add ("Article 2 record"); ydgdTemplete.setDetailList (list); / / generate word document Boolean falg = FileUtil.createdoc (docPath,ydgdTemplete) from the template / / the call returns if (falg) {data.setData (backPath); data.setCode (WebResponseCode.APPSUCCESS); data.setMsg ("successful operation!") ;} else {data.setData (null); data.setCode (WebResponseCode.APPFAIL); data.setMsg ("Operation failed!") ;} return data;} catch (Exception e) {e.printStackTrace (); data.setCode (WebResponseCode.APPFAIL); data.setMsg ("Operation exception!") ; return data;}} 5.3.Generation method / * * generate word file according to word template * @ param docPath * @ param ydgdTemplete * @ return * / public static boolean createdoc (String docPath, YdgdTemplete ydgdTemplete) {try {if (StringUtil.isBlank (docPath)) {return false } / / read template File file = ResourceUtils.getFile ("classpath:templates/ydgd_templete.docx") XWPFTemplate template = XWPFTemplate.compile (file) .render (new HashMap () {{/ / generate data put ("demand", ydgdTemplete.getDemand ()) / / generate list data put ("detailList") New NumbericRenderData (new ArrayList () {{if (nullroomroomydgdTemplete. GetDetailList () & & ydgdTemplete.getDetailList (). Size () > 0) {for (String detail: YdgdTemplete.getDetailList () {add (new TextRenderData (detail)) }));) FileOutputStream out = new FileOutputStream (docPath); template.write (out); out.flush (); out.close (); template.close (); return true;} catch (Exception e) {e.printStackTrace (); return false }} the answer to the question about how to use poi-tl to operate the word template is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.