| | |
| | | |
| | | @Service |
| | | public class WordFileService { |
| | | |
| | | public void export(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws IOException { |
| | | |
| | | Map<String, Object> datas = new HashMap<String, Object>(); |
| | | // 将word内容json字符串转成json对象 |
| | | |
| | | Field[] fields = ReflectUtil.getFields(wordFile.getClass()); |
| | | for (Field field : fields) { |
| | | try { |
| | | field.setAccessible(true); |
| | | //Object value = field.get(wordFile); |
| | | datas.put(field.getName(), field.get(wordFile)); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 不支持doc的格式 |
| | | // 加载word导出模板,读入导出内容 |
| | | |
| | | XWPFTemplate template = XWPFTemplate.compile(wordFile.getModulePath()); |
| | | template.render(datas); |
| | | //FileOutputStream out = new FileOutputStream("D:\\out_template.docx"); |
| | | //template.write(out); |
| | | //out.flush(); |
| | | //out.close(); |
| | | //template.close(); |
| | | DownloadService.download(request, response, template, wordFile.getWordName()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 打印word(单个表格数据) |
| | | * |
| | | * @param request |
| | | * @param wordFile |
| | | * @param response |
| | | */ |
| | | public void exportForTables(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException { |
| | | |
| | | public void exportWordFile(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException { |
| | | // 数据map集合 |
| | | Map<String, Object> data = new HashMap<>(); |
| | | //HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy(); |
| | | Configure config = null; |
| | | ConfigureBuilder builder = Configure.newBuilder(); |
| | | this.formatWordData(wordFile, data, builder); |
| | | |
| | | Field[] fields = ReflectUtil.getFields(wordFile.getClass()); |
| | | for (Field field : fields) { |
| | | field.setAccessible(true); // 对私有属性进行访问 |
| | | String fieldName = field.getName(); // 属性名 |
| | | Object staticFieldValue = ReflectUtil.getFieldValue(wordFile, field.getName()); // 属性对应的数据 |
| | | if (field.getType().getName().equals("java.util.List")) {//如果该属性是list类型 |
| | | builder.bind(fieldName, new HackLoopTableRenderPolicy()); |
| | | }else if(staticFieldValue!=null) { |
| | | String valStr = staticFieldValue.toString(); |
| | | if (valStr.contains("签名图片:")) { |
| | | valStr = valStr.replace("签名图片:",""); |
| | | staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr); |
| | | /* put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png"))); |
| | | */ |
| | | } |
| | | } |
| | | data.put(fieldName, staticFieldValue); |
| | | data.put(fieldName, staticFieldValue); |
| | | } |
| | | config = builder.build(); |
| | | File fl = new File(wordFile.getModulePath()); |
| | | FileInputStream fs = new FileInputStream(fl); |
| | |
| | | template.render(data); |
| | | DownloadService.download(request, response, template, wordFile.getWordName()); |
| | | } |
| | | |
| | | public void formatWordData(Object obj, Map<String, Object> data, ConfigureBuilder builder) { |
| | | Field[] fields = ReflectUtil.getFields(obj.getClass()); |
| | | for (Field field : fields) { |
| | | field.setAccessible(true); |
| | | String fieldName = field.getName(); |
| | | String typeName = field.getType().getName(); |
| | | Object staticFieldValue = ReflectUtil.getFieldValue(obj, field.getName()); // 属性对应的数据 |
| | | if (typeName.contains("com.zt.life.modules")) { |
| | | formatWordData(staticFieldValue, data, builder); |
| | | } else { |
| | | if (field.getType().getName().equals("java.util.List")) { |
| | | builder.bind(fieldName, new HackLoopTableRenderPolicy()); |
| | | } else if (staticFieldValue != null) { |
| | | String valStr = staticFieldValue.toString(); |
| | | if (valStr.contains("签名图片:")) { |
| | | valStr = valStr.replace("签名图片:", ""); |
| | | staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr); |
| | | /*put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));*/ |
| | | } |
| | | } |
| | | if (data.get(fieldName) == null) |
| | | data.put(fieldName, staticFieldValue); |
| | | } |
| | | } |
| | | } |
| | | } |