Commit 1bd5782806bba89e1e59a7d86f24dba244246951
1 parent
db1010f5a2
Exists in
master
and in
6 other branches
儿保结案功能
Showing 5 changed files with 387 additions and 7 deletions
- platform-operate-api/pom.xml
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/BabyBuildController.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/WordTest.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/BabyBookbuildingFacade.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/WordUtil.java
platform-operate-api/pom.xml
View file @
1bd5782
| ... | ... | @@ -12,14 +12,14 @@ |
| 12 | 12 | |
| 13 | 13 | <dependencies> |
| 14 | 14 | <!--<dependency>--> |
| 15 | - <!--<groupId>org.apache.httpcomponents</groupId>--> | |
| 16 | - <!--<artifactId>httpclient</artifactId>--> | |
| 17 | - <!--<version>4.5.2</version>--> | |
| 15 | + <!--<groupId>org.apache.httpcomponents</groupId>--> | |
| 16 | + <!--<artifactId>httpclient</artifactId>--> | |
| 17 | + <!--<version>4.5.2</version>--> | |
| 18 | 18 | <!--</dependency>--> |
| 19 | 19 | |
| 20 | 20 | <!--<dependency>--> |
| 21 | - <!--<groupId>commons-httpclient</groupId>--> | |
| 22 | - <!--<artifactId>commons-httpclient</artifactId>--> | |
| 21 | + <!--<groupId>commons-httpclient</groupId>--> | |
| 22 | + <!--<artifactId>commons-httpclient</artifactId>--> | |
| 23 | 23 | <!--</dependency>--> |
| 24 | 24 | <dependency> |
| 25 | 25 | <groupId>com.aspose</groupId> |
| ... | ... | @@ -97,6 +97,21 @@ |
| 97 | 97 | <artifactId>jxl</artifactId> |
| 98 | 98 | <version>2.6.12</version> |
| 99 | 99 | </dependency> |
| 100 | + | |
| 101 | + <!-- https://mvnrepository.com/artifact/freemarker/freemarker --> | |
| 102 | + <!-- <dependency> | |
| 103 | + <groupId>freemarker</groupId> | |
| 104 | + <artifactId>freemarker</artifactId> | |
| 105 | + <version>2.3.9</version> | |
| 106 | + </dependency>--> | |
| 107 | + | |
| 108 | + | |
| 109 | + <!-- https://mvnrepository.com/artifact/ro.pippo/pippo-freemarker --> | |
| 110 | + <!-- <dependency> | |
| 111 | + <groupId>ro.pippo</groupId> | |
| 112 | + <artifactId>pippo-freemarker</artifactId> | |
| 113 | + <version>1.12.0</version> | |
| 114 | + </dependency>--> | |
| 100 | 115 | |
| 101 | 116 | </dependencies> |
| 102 | 117 | <build> |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/BabyBuildController.java
View file @
1bd5782
| 1 | 1 | package com.lyms.platform.operate.web.controller; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | +import com.aspose.words.*; | |
| 4 | 5 | import com.lyms.platform.common.annotation.TokenRequired; |
| 5 | 6 | import com.lyms.platform.common.base.BaseController; |
| 6 | 7 | import com.lyms.platform.common.base.LoginContext; |
| 7 | 8 | |
| ... | ... | @@ -28,8 +29,11 @@ |
| 28 | 29 | import javax.servlet.http.HttpServletRequest; |
| 29 | 30 | import javax.servlet.http.HttpServletResponse; |
| 30 | 31 | import javax.validation.Valid; |
| 32 | +import java.io.IOException; | |
| 33 | +import java.io.InputStream; | |
| 31 | 34 | import java.io.UnsupportedEncodingException; |
| 32 | 35 | import java.util.List; |
| 36 | +import java.util.Map; | |
| 33 | 37 | |
| 34 | 38 | |
| 35 | 39 | /* |
| ... | ... | @@ -558,6 +562,92 @@ |
| 558 | 562 | ) { |
| 559 | 563 | LoginContext loginState = (LoginContext) request.getAttribute("loginContext"); |
| 560 | 564 | babyBookbuildingFacade.exportWcBabyReportExcl(loginState.getId(), time, hospitalId, response); |
| 565 | + } | |
| 566 | + | |
| 567 | + //导出管理记录表 | |
| 568 | + @RequestMapping(value = "/exportWcBabyReportExcl", method = RequestMethod.GET) | |
| 569 | + @TokenRequired | |
| 570 | + public void exportManagementRecords(HttpServletRequest request, HttpServletResponse response, | |
| 571 | + @RequestParam(required = false) String id) { | |
| 572 | + LoginContext loginState = (LoginContext) request.getAttribute("loginContext"); | |
| 573 | + Map<String, Object> map = babyBookbuildingFacade.getData(id); | |
| 574 | + try { | |
| 575 | + // 验证License | |
| 576 | + if (!getLicense()) { | |
| 577 | + return; | |
| 578 | + } | |
| 579 | + Document doc = null;// 原始word路径 | |
| 580 | + try { | |
| 581 | + doc = new Document(ViewController.class.getClassLoader().getResourceAsStream("jlb.docx")); | |
| 582 | + markData(doc, map);//普通数据 | |
| 583 | + sendToBrowser(doc, "导出", "doc", true, response); | |
| 584 | + } catch (Exception e) { | |
| 585 | + e.printStackTrace(); | |
| 586 | + } | |
| 587 | + response.flushBuffer(); | |
| 588 | + } catch (IOException e) { | |
| 589 | + e.printStackTrace(); | |
| 590 | + } | |
| 591 | + | |
| 592 | + } | |
| 593 | + | |
| 594 | + public static boolean getLicense() { | |
| 595 | + boolean result = false; | |
| 596 | + try { | |
| 597 | + InputStream is = ViewController.class.getClassLoader().getResourceAsStream("license.xml"); | |
| 598 | + License aposeLic = new License(); | |
| 599 | + aposeLic.setLicense(is); | |
| 600 | + result = true; | |
| 601 | + } catch (Exception e) { | |
| 602 | + e.printStackTrace(); | |
| 603 | + } | |
| 604 | + return result; | |
| 605 | + } | |
| 606 | + | |
| 607 | + public void markData(Document doc, Map<String, Object> data) { | |
| 608 | + try { | |
| 609 | + for (Map.Entry<String, Object> m : data.entrySet()) { | |
| 610 | + if (m.getValue() == null || "null".equals(m.getValue())) { | |
| 611 | + continue; | |
| 612 | + } | |
| 613 | + BookmarkCollection books = doc.getRange().getBookmarks(); | |
| 614 | + Bookmark bookmark = books.get(m.getKey()); | |
| 615 | + if (bookmark != null) { | |
| 616 | + bookmark.setText(String.valueOf(m.getValue())); | |
| 617 | + } | |
| 618 | + } | |
| 619 | + } catch (Exception e) { | |
| 620 | + e.printStackTrace(); | |
| 621 | + } | |
| 622 | + } | |
| 623 | + | |
| 624 | + private void sendToBrowser(Document doc, String docName, String formatType, | |
| 625 | + boolean openNewWindow, HttpServletResponse response) | |
| 626 | + throws Exception { | |
| 627 | + String extension = formatType; | |
| 628 | + | |
| 629 | + if (formatType.equals("WML") || formatType.equals("FOPC")) | |
| 630 | + extension = "XML"; | |
| 631 | + | |
| 632 | + String fileName = docName + "." + extension; | |
| 633 | + | |
| 634 | + if (openNewWindow) | |
| 635 | + response.setHeader("content-disposition", "attachment; filename=" | |
| 636 | + + fileName); | |
| 637 | + else | |
| 638 | + response.addHeader("content-disposition", "inline; filename=" | |
| 639 | + + fileName); | |
| 640 | + | |
| 641 | + if ("doc".equals(formatType)) { | |
| 642 | + response.setContentType("application/msword"); | |
| 643 | + doc.save(response.getOutputStream(), SaveFormat.DOC); | |
| 644 | + } else if ("docx".equals(formatType)) { | |
| 645 | + response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); | |
| 646 | + doc.save(response.getOutputStream(), SaveFormat.DOCX); | |
| 647 | + } else if ("pdf".equals(formatType)) { | |
| 648 | + response.setContentType("application/pdf"); | |
| 649 | + doc.save(response.getOutputStream(), SaveFormat.PDF); | |
| 650 | + } | |
| 561 | 651 | } |
| 562 | 652 | |
| 563 | 653 |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/WordTest.java
View file @
1bd5782
| 1 | +/* | |
| 2 | +package com.lyms.platform.operate.web.controller; | |
| 3 | + | |
| 4 | + | |
| 5 | +import freemarker.cache.FileTemplateLoader; | |
| 6 | +import freemarker.cache.TemplateLoader; | |
| 7 | +import freemarker.template.Configuration; | |
| 8 | +import freemarker.template.Template; | |
| 9 | +import freemarker.template.TemplateException; | |
| 10 | + | |
| 11 | +import java.io.*; | |
| 12 | +import java.util.ArrayList; | |
| 13 | +import java.util.HashMap; | |
| 14 | +import java.util.List; | |
| 15 | +import java.util.Map; | |
| 16 | + | |
| 17 | + | |
| 18 | +public class WordTest { | |
| 19 | + | |
| 20 | + private Configuration configuration = null; | |
| 21 | + | |
| 22 | + public static void main(String[] args) { | |
| 23 | + com.lyms.platform.operate.web.controller.WordTest test = new com.lyms.platform.operate.web.controller.WordTest(); | |
| 24 | + test.createWord(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + | |
| 28 | + public void createWord() { | |
| 29 | + Configuration cfg = new Configuration(); | |
| 30 | + TemplateLoader templateLoader = null; | |
| 31 | + String path = ""; | |
| 32 | + Map<String, Object> dataMap = new HashMap<String, Object>(); | |
| 33 | + getData(dataMap); | |
| 34 | + //使用FileTemplateLoader | |
| 35 | + Template t = null; | |
| 36 | + try { | |
| 37 | + path = this.getClass().getResource("/").getPath(); | |
| 38 | + templateLoader = new FileTemplateLoader(new File(path)); | |
| 39 | + cfg.setTemplateLoader(templateLoader); | |
| 40 | + t = cfg.getTemplate("jlb.ftl", "UTF-8"); | |
| 41 | + } catch (IOException e) { | |
| 42 | + e.printStackTrace(); | |
| 43 | + } | |
| 44 | + File outFile = new File("D:/outFile" + Math.random() * 10000 + ".doc"); //导出文件 | |
| 45 | + Writer out = null; | |
| 46 | + try { | |
| 47 | + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); | |
| 48 | + } catch (FileNotFoundException e1) { | |
| 49 | + e1.printStackTrace(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + try { | |
| 53 | + t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 | |
| 54 | + } catch (TemplateException e) { | |
| 55 | + e.printStackTrace(); | |
| 56 | + } catch (IOException e) { | |
| 57 | + e.printStackTrace(); | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + private void getData(Map<String, Object> dataMap) { | |
| 62 | + dataMap.put("title", "标题"); | |
| 63 | + dataMap.put("nian", "2016"); | |
| 64 | + dataMap.put("yue", "3"); | |
| 65 | + dataMap.put("ri", "6"); | |
| 66 | + //dataMap.put("shenheren", "lc"); | |
| 67 | + List<Map<String, Object>> cheshi = new ArrayList<Map<String, Object>>(); | |
| 68 | + for (int i = 0; i < 10; i++) { | |
| 69 | + Map<String, Object> map = new HashMap<String, Object>(); | |
| 70 | + map.put("xuhao", "傻得"); | |
| 71 | + map.put("neirong", "内容" + i); | |
| 72 | + cheshi.add(map); | |
| 73 | + } | |
| 74 | + | |
| 75 | + dataMap.put("cheshi", cheshi); | |
| 76 | + } | |
| 77 | +}*/ |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/BabyBookbuildingFacade.java
View file @
1bd5782
| ... | ... | @@ -22,6 +22,7 @@ |
| 22 | 22 | import com.lyms.platform.operate.web.result.*; |
| 23 | 23 | import com.lyms.platform.operate.web.service.ITrackDownService; |
| 24 | 24 | import com.lyms.platform.operate.web.utils.BabyListTask; |
| 25 | +import com.lyms.platform.operate.web.utils.CommonsHelper; | |
| 25 | 26 | import com.lyms.platform.operate.web.utils.GrowthCountTask; |
| 26 | 27 | import com.lyms.platform.operate.web.utils.MongoUtil; |
| 27 | 28 | import com.lyms.platform.permission.model.*; |
| ... | ... | @@ -31,6 +32,11 @@ |
| 31 | 32 | import com.lyms.platform.permission.service.UsersService; |
| 32 | 33 | import com.lyms.platform.pojo.*; |
| 33 | 34 | import com.lyms.platform.query.*; |
| 35 | +import freemarker.cache.FileTemplateLoader; | |
| 36 | +import freemarker.cache.TemplateLoader; | |
| 37 | +import freemarker.template.Configuration; | |
| 38 | +import freemarker.template.Template; | |
| 39 | +import freemarker.template.TemplateException; | |
| 34 | 40 | import org.apache.commons.collections.CollectionUtils; |
| 35 | 41 | import org.apache.commons.lang.*; |
| 36 | 42 | import org.apache.commons.lang.math.NumberUtils; |
| 37 | 43 | |
| ... | ... | @@ -38,12 +44,13 @@ |
| 38 | 44 | import org.springframework.beans.factory.annotation.Qualifier; |
| 39 | 45 | import org.springframework.data.domain.Sort; |
| 40 | 46 | import org.springframework.data.mongodb.core.MongoTemplate; |
| 47 | +import org.springframework.data.mongodb.core.query.Criteria; | |
| 48 | +import org.springframework.data.mongodb.core.query.Query; | |
| 41 | 49 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 42 | 50 | import org.springframework.stereotype.Component; |
| 43 | 51 | |
| 44 | 52 | import javax.servlet.http.HttpServletResponse; |
| 45 | -import java.io.IOException; | |
| 46 | -import java.io.OutputStream; | |
| 53 | +import java.io.*; | |
| 47 | 54 | import java.util.*; |
| 48 | 55 | import java.util.concurrent.Callable; |
| 49 | 56 | import java.util.concurrent.Future; |
| ... | ... | @@ -64,6 +71,9 @@ |
| 64 | 71 | private HisService hisServiceV2; |
| 65 | 72 | |
| 66 | 73 | @Autowired |
| 74 | + private ViewFacade viewFacade; | |
| 75 | + | |
| 76 | + @Autowired | |
| 67 | 77 | private QingLongXianHisService qingLongXianHisService; |
| 68 | 78 | |
| 69 | 79 | @Autowired |
| 70 | 80 | |
| ... | ... | @@ -3853,6 +3863,121 @@ |
| 3853 | 3863 | return parentIds; |
| 3854 | 3864 | } |
| 3855 | 3865 | |
| 3866 | + /* public void exportManagementRecords(String id) { | |
| 3856 | 3867 | |
| 3868 | + } | |
| 3869 | + | |
| 3870 | + public void createWord(String id) { | |
| 3871 | + Configuration cfg = new Configuration(); | |
| 3872 | + TemplateLoader templateLoader = null; | |
| 3873 | + String path = ""; | |
| 3874 | + //Map<String, Object> dataMap = new HashMap<String, Object>(); | |
| 3875 | + Map<String, Object> dataMap = getData(id); | |
| 3876 | + //使用FileTemplateLoader | |
| 3877 | + Template t = null; | |
| 3878 | + try { | |
| 3879 | + path = this.getClass().getResource("/").getPath(); | |
| 3880 | + templateLoader = new FileTemplateLoader(new File(path)); | |
| 3881 | + cfg.setTemplateLoader(templateLoader); | |
| 3882 | + t = cfg.getTemplate("jlb.ftl", "UTF-8"); | |
| 3883 | + } catch (IOException e) { | |
| 3884 | + e.printStackTrace(); | |
| 3885 | + } | |
| 3886 | + File outFile = new File("D:/outFile" + Math.random() * 10000 + ".doc"); //导出文件 | |
| 3887 | + Writer out = null; | |
| 3888 | + try { | |
| 3889 | + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); | |
| 3890 | + } catch (FileNotFoundException e1) { | |
| 3891 | + e1.printStackTrace(); | |
| 3892 | + } | |
| 3893 | + | |
| 3894 | + try { | |
| 3895 | + t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 | |
| 3896 | + } catch (TemplateException e) { | |
| 3897 | + e.printStackTrace(); | |
| 3898 | + } catch (IOException e) { | |
| 3899 | + e.printStackTrace(); | |
| 3900 | + } | |
| 3901 | + }*/ | |
| 3902 | + | |
| 3903 | + public Map<String, Object> getData(String id) { | |
| 3904 | + Map<String, Object> dataMap = new HashMap<>(); | |
| 3905 | + if (StringUtils.isNotEmpty(id)) { | |
| 3906 | + BabyModel babyModel = mongoTemplate.findOne(Query.query(Criteria.where("_id").is(id)), BabyModel.class); | |
| 3907 | + dataMap.put("babyName", babyModel.getName()); | |
| 3908 | + dataMap.put("sex", babyModel.getSex() != 0 ? "男" : "女"); | |
| 3909 | + String[] times = DateUtil.getyyyy_MM_dd(babyModel.getBirth()).split("-"); | |
| 3910 | + dataMap.put("nian", times[0]); | |
| 3911 | + dataMap.put("yue", times[1]); | |
| 3912 | + dataMap.put("ri", times[2]); | |
| 3913 | + String[] created = DateUtil.getyyyy_MM_dd(babyModel.getCreated()).split("-"); | |
| 3914 | + dataMap.put("createdYear", created[0]); | |
| 3915 | + dataMap.put("createdMnth", created[1]); | |
| 3916 | + dataMap.put("createdDay", created[2]); | |
| 3917 | + if (null != babyModel.getEndCaseTime()) { | |
| 3918 | + String[] endTime = DateUtil.getyyyy_MM_dd(babyModel.getEndCaseTime()).split("-"); | |
| 3919 | + dataMap.put("ednYear", endTime[0]); | |
| 3920 | + dataMap.put("endMnth", endTime[1]); | |
| 3921 | + dataMap.put("endDay", endTime[2]); | |
| 3922 | + } | |
| 3923 | + if (babyModel.getHighRisk() != 0 && CollectionUtils.isNotEmpty(babyModel.getHighRiskInfo())) { | |
| 3924 | + dataMap.put("hig", "高危儿因素"); | |
| 3925 | + String highRiskInfos = basicConfigFacade.queryBaseInfoByStr(babyModel.getHighRiskInfo()); | |
| 3926 | + dataMap.put("higHrisk", highRiskInfos); | |
| 3927 | + } | |
| 3928 | + if (babyModel.getWeakSon() != 0 && CollectionUtils.isNotEmpty(babyModel.getWeakSonInfo())) { | |
| 3929 | + dataMap.put("wask", "体弱儿因素"); | |
| 3930 | + String weakSonsInfo = basicConfigFacade.queryBaseInfoByStr(babyModel.getWeakSonInfo()); | |
| 3931 | + dataMap.put("weakSone", weakSonsInfo); | |
| 3932 | + } | |
| 3933 | + dataMap.put("phone", babyModel.getFphone()); | |
| 3934 | + dataMap.put("parentName", babyModel.getMname()); | |
| 3935 | + List<BabyCheckModel> babyCheckModel = mongoTemplate.find(Query.query(Criteria.where("buildId").is(id)), BabyCheckModel.class); | |
| 3936 | + if (CollectionUtils.isNotEmpty(babyCheckModel)) { | |
| 3937 | + List<Map> listMap = new ArrayList<>(); | |
| 3938 | + for (BabyCheckModel babyCheck : babyCheckModel) { | |
| 3939 | + Map<String, Object> map = new HashMap<>(); | |
| 3940 | + dataMap.put("mainFoster", babyCheck.getMainFoster()); | |
| 3941 | + /* map.put("checkTime", DateUtil.getyyyy_MM_dd(babyCheck.getCheckDate())); | |
| 3942 | + map.put("age", DateUtil.getBabyMonthAge(babyModel.getBirth(), babyCheck.getCheckDate()));*/ | |
| 3943 | + | |
| 3944 | + } | |
| 3945 | + System.out.println(dataMap.get("mainFoster")); | |
| 3946 | + if ("0".equals(dataMap.get("mainFoster"))) { | |
| 3947 | + dataMap.put("filiation", "父子"); | |
| 3948 | + } else if ("1".equals(dataMap.get("mainFoster"))) { | |
| 3949 | + dataMap.put("filiation", "母子"); | |
| 3950 | + } else if ("2".equals(dataMap.get("mainFoster")) || "3".equals(dataMap.get("mainFoster"))) { | |
| 3951 | + dataMap.put("filiation", "祖孙"); | |
| 3952 | + } else if ("4".equals(dataMap.get("mainFoster")) || "5".equals(dataMap.get("mainFoster"))) { | |
| 3953 | + dataMap.put("filiation", "外祖孙"); | |
| 3954 | + } else if ("6".equals(dataMap.get("mainFoster"))) { | |
| 3955 | + dataMap.put("filiation", "其他"); | |
| 3956 | + } | |
| 3957 | + } | |
| 3958 | + Patients patients = mongoTemplate.findOne(Query.query(Criteria.where("_id").is(babyModel.getParentId())), Patients.class); | |
| 3959 | + //市区 | |
| 3960 | + dataMap.put("city", CommonsHelper.getName1(patients.getCityRegisterId(), basicConfigService)); | |
| 3961 | + //县 | |
| 3962 | + dataMap.put("county", CommonsHelper.getName1(patients.getAreaRegisterId(), basicConfigService)); | |
| 3963 | + //乡镇 | |
| 3964 | + dataMap.put("township", CommonsHelper.getName1(patients.getStreetRegisterId(), basicConfigService)); | |
| 3965 | + //村 | |
| 3966 | + dataMap.put("village", patients.getAddressRegister()); | |
| 3967 | + dataMap.put("education", getBasicConfig(babyModel.getmLevelId())); | |
| 3968 | + } | |
| 3969 | + return dataMap; | |
| 3970 | + } | |
| 3971 | + | |
| 3972 | + private String getBasicConfig(String id) { | |
| 3973 | + if (com.lyms.platform.common.utils.StringUtils.isEmpty(id)) { | |
| 3974 | + return ""; | |
| 3975 | + } | |
| 3976 | + BasicConfig basicConfig = basicConfigService.getOneBasicConfigById(id); | |
| 3977 | + if (null != basicConfig) { | |
| 3978 | + return basicConfig.getName(); | |
| 3979 | + } | |
| 3980 | + return ""; | |
| 3981 | + } | |
| 3857 | 3982 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/WordUtil.java
View file @
1bd5782
| 1 | +/* | |
| 2 | +package com.lyms.platform.operate.web.facade; | |
| 3 | + | |
| 4 | +import freemarker.cache.FileTemplateLoader; | |
| 5 | +import freemarker.cache.TemplateLoader; | |
| 6 | +import freemarker.template.Configuration; | |
| 7 | +import freemarker.template.Template; | |
| 8 | +import freemarker.template.TemplateException; | |
| 9 | + | |
| 10 | +import java.io.*; | |
| 11 | +import java.util.ArrayList; | |
| 12 | +import java.util.HashMap; | |
| 13 | +import java.util.List; | |
| 14 | +import java.util.Map; | |
| 15 | + | |
| 16 | +public class WordUtil { | |
| 17 | + private Configuration configuration = null; | |
| 18 | + | |
| 19 | + public WordUtil() { | |
| 20 | + configuration = new Configuration(); | |
| 21 | + configuration.setDefaultEncoding("UTF-8"); | |
| 22 | + } | |
| 23 | + public void createWord() { | |
| 24 | + Configuration cfg = new Configuration(); | |
| 25 | + TemplateLoader templateLoader = null; | |
| 26 | + String path = ""; | |
| 27 | + Map<String, Object> dataMap = new HashMap<String, Object>(); | |
| 28 | + getData(dataMap); | |
| 29 | + //使用FileTemplateLoader | |
| 30 | + Template t = null; | |
| 31 | + try { | |
| 32 | + path = this.getClass().getResource("/").getPath(); | |
| 33 | + templateLoader = new FileTemplateLoader(new File(path)); | |
| 34 | + cfg.setTemplateLoader(templateLoader); | |
| 35 | + t = cfg.getTemplate("jlb.ftl", "UTF-8"); | |
| 36 | + } catch (IOException e) { | |
| 37 | + e.printStackTrace(); | |
| 38 | + } | |
| 39 | + File outFile = new File("D:/outFile" + Math.random() * 10000 + ".doc"); //导出文件 | |
| 40 | + Writer out = null; | |
| 41 | + try { | |
| 42 | + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); | |
| 43 | + } catch (FileNotFoundException e1) { | |
| 44 | + e1.printStackTrace(); | |
| 45 | + } | |
| 46 | + | |
| 47 | + try { | |
| 48 | + t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 | |
| 49 | + } catch (TemplateException e) { | |
| 50 | + e.printStackTrace(); | |
| 51 | + } catch (IOException e) { | |
| 52 | + e.printStackTrace(); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + private void getData(Map<String, Object> dataMap) { | |
| 57 | + dataMap.put("title", "标题"); | |
| 58 | + dataMap.put("nian", "2016"); | |
| 59 | + dataMap.put("yue", "3"); | |
| 60 | + dataMap.put("ri", "6"); | |
| 61 | + //dataMap.put("shenheren", "lc"); | |
| 62 | + List<Map<String, Object>> cheshi = new ArrayList<Map<String, Object>>(); | |
| 63 | + for (int i = 0; i < 10; i++) { | |
| 64 | + Map<String, Object> map = new HashMap<String, Object>(); | |
| 65 | + map.put("xuhao", "傻得"); | |
| 66 | + map.put("neirong", "内容" + i); | |
| 67 | + cheshi.add(map); | |
| 68 | + } | |
| 69 | + | |
| 70 | + dataMap.put("cheshi", cheshi); | |
| 71 | + } | |
| 72 | +} | |
| 73 | +*/ |