使用freemark模板生成word文件

  项目中,客户要求填写完资料后生成word文件,我采用的方案是从网上找的。

  1.现将客户提供的生成后的文档通过word2010另存为xml格式的文件,然后通过编辑工具打开,我的是Notepad++,

    2.生成xml文件后由于里面的文件没有格式化会比较乱,推荐大家进行在线xml格式化,然后复制到编辑器,进行参数的设置。

  3.因为生成后的word文件里可能包含图片,这里可以先进行图片位置设置,然后删除生成的64位编码,设成图片参数。

  4.设置完参数后,再另存为ftl文件存放到项目对应的目录项目

  5.在ftl文件中的元素是采用list进行遍历的

  下面是代码片段:

<#if (madeli?size>0) >  
                <w:tr wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidTr="00BA6ED9">
                    <w:trPr>
                        <w:trHeight w:val="411"/>
                        <w:jc w:val="center"/>
                    </w:trPr>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="2315" w:type="dxa"/>
                            <w:gridSpan w:val="2"/>
                            <w:tcBorders>
                                <w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:b/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:b/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>注册方式</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="7785" w:type="dxa"/>
                            <w:gridSpan w:val="2"/>
                            <w:tcBorders>
                                <w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:left w:val="nil"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:b/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:b/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>马德里途径</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
                <w:tr wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidTr="00BA6ED9">
                    <w:trPr>
                        <w:trHeight w:val="420"/>
                        <w:jc w:val="center"/>
                    </w:trPr>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="755" w:type="dxa"/>
                            <w:tcBorders>
                                <w:top w:val="nil"/>
                                <w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>序号</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="1560" w:type="dxa"/>
                            <w:tcBorders>
                                <w:top w:val="nil"/>
                                <w:left w:val="nil"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>国家或地区</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="1559" w:type="dxa"/>
                            <w:tcBorders>
                                <w:top w:val="nil"/>
                                <w:left w:val="nil"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>类别</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="6226" w:type="dxa"/>
                            <w:tcBorders>
                                <w:top w:val="nil"/>
                                <w:left w:val="nil"/>
                                <w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                                <w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
                            </w:tcBorders>
                            <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                            <w:vAlign w:val="center"/>
                        </w:tcPr>
                        <w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidRDefault="005A00D5" wsp:rsidP="00BA6ED9">
                            <w:pPr>
                                <w:widowControl/>
                                <w:jc w:val="center"/>
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                            </w:pPr>
                            <w:r wsp:rsidRPr="00AC6493">
                                <w:rPr>
                                    <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
                                    <wx:font wx:val="微软雅黑"/>
                                    <w:color w:val="000000"/>
                                    <w:kern w:val="0"/>
                                    <w:sz w:val="22"/>
                                </w:rPr>
                                <w:t>注册注意事项</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
               <#list madeli as dataMap>
				<w:tr wsp:rsidR="005A00D5" wsp:rsidRPr="00AC6493" wsp:rsidTr="00BA6ED9">
				  <w:trPr>
					<w:trHeight w:val="720"/>
					<w:jc w:val="center"/>
				  </w:trPr>
				  <w:tc>
					<w:tcPr>
					  <w:tcW w:w="755" w:type="dxa"/>
					  <w:tcBorders>
						<w:top w:val="nil"/>
						<w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
						<w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
						<w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
					  </w:tcBorders>
					  <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
					  <w:vAlign w:val="center"/>
					</w:tcPr>
					
					<w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00302537" wsp:rsidRDefault="00C568D0" wsp:rsidP="00C003FB">
					  <w:pPr>
						<w:widowControl/>
						<w:spacing w:line="320" w:line-rule="exact"/>
						<w:jc w:val="center"/>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="22"/>
						</w:rPr>
					  </w:pPr>
					 
					  <w:r>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="22"/>
						</w:rPr>
						<w:t>${dataMap.code}</w:t>
					  </w:r>
					  
					</w:p>
					
				  </w:tc>
				  <w:tc>
					<w:tcPr>
					  <w:tcW w:w="1560" w:type="dxa"/>
					  <w:tcBorders>
						<w:top w:val="nil"/>
						<w:left w:val="nil"/>
						<w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
						<w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
					  </w:tcBorders>
					  <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
					  <w:vAlign w:val="center"/>
					</w:tcPr>
					<w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00302537" wsp:rsidRDefault="00C568D0" wsp:rsidP="00C003FB">
					  <w:pPr>
						<w:widowControl/>
						<w:spacing w:line="320" w:line-rule="exact"/>
						<w:jc w:val="left"/>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="20"/>
						</w:rPr>
					  </w:pPr>
					  <w:r>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="20"/>
						</w:rPr>
						<w:t>${dataMap.country}</w:t>
					  </w:r>
					</w:p>
				  </w:tc>
				  <w:tc>
					<w:tcPr>
					  <w:tcW w:w="1559" w:type="dxa"/>
					  <w:tcBorders>
						<w:top w:val="nil"/>
						<w:left w:val="nil"/>
						<w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
						<w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
					  </w:tcBorders>
					  <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
					  <w:vAlign w:val="center"/>
					</w:tcPr>
					<w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00302537" wsp:rsidRDefault="00C568D0" wsp:rsidP="00C003FB">
					  <w:pPr>
						<w:widowControl/>
						<w:spacing w:line="320" w:line-rule="exact"/>
						<w:jc w:val="left"/>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="22"/>
						</w:rPr>
					  </w:pPr>
					  <w:r>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="22"/>
						</w:rPr>
						<w:t>${dataMap.type}</w:t>
					  </w:r>
					</w:p>
				  </w:tc>
				  <w:tc>
					<w:tcPr>
					  <w:tcW w:w="6226" w:type="dxa"/>
					  <w:tcBorders>
						<w:top w:val="nil"/>
						<w:left w:val="nil"/>
						<w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
						<w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/>
					  </w:tcBorders>
					  <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
					  <w:vAlign w:val="center"/>
					</w:tcPr>
					<w:p wsp:rsidR="005A00D5" wsp:rsidRPr="00302537" wsp:rsidRDefault="00C568D0" wsp:rsidP="00C003FB">
					  <w:pPr>
						<w:widowControl/>
						<w:spacing w:line="320" w:line-rule="exact"/>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="20"/>
						</w:rPr>
					  </w:pPr>
					  <w:r>
						<w:rPr>
						  <w:rFonts w:ascii="微软雅黑" w:fareast="微软雅黑" w:h-ansi="微软雅黑" w:cs="宋体" w:hint="fareast"/>
						  <wx:font wx:val="微软雅黑"/>
						  <w:color w:val="FF0000"/>
						  <w:kern w:val="0"/>
						  <w:sz w:val="20"/>
						</w:rPr>
						<w:t>${dataMap.tips}</w:t>
					  </w:r>
					</w:p>
				  </w:tc>
				</w:tr>
				</#list>
				</#if>

 

  1 /**
  2      * 创建方案
  3      * 
  4      * @param paln_type
  5      * @param export_status
  6      * @param export_countries
  7      * @param hot_countries
  8      * @param ent_category
  9      * @param tm_word
 10      * @param tm_file
 11      * @param china_reg_status
 12      * @param law_suit
 13      * @param law_suit_countries
 14      * @param gen_type
 15      * @return
 16      */
 17     public Map<String, String> createPlan(TmPlan planVo,
 18             String ip) {
 19         Map<String, String> resultMap = new HashMap<>();
 20         resultMap.put("status", "fail");
 21         TmPlan plan = new TmPlan();
 22         try {
 23             BeanUtils.copyProperties(plan, planVo);
 24             plan.setId(OidMgr.requestOID("tm_plan").toString());
 25             plan.setPersisted(false);
 26             plan.setCreated_time(DateUtil.getNow());
 27             plan.setIp(ip);
 28             if (plan.getGen_type().equals("1")) {
 29                 plan.setSn(StrUtils.getYzm(6));
 30                 // 0已提交 1已上传
 31                 plan.setStatus("0");
 32             }
 33             tmPlanRepository.save(plan);
 34 
 35             resultMap.put("status", "ok");
 36             if (plan.getGen_type().equals("1")) {
 37                 resultMap.put("gentype", plan.getGen_type());
 38                 resultMap.put("sn", plan.getSn());
 39                 resultMap.put("id", plan.getId());
 40             } else {
 41                 Map<String, String> createdMap = createOnlinePlan(plan);
 42                 resultMap.put("gentype", plan.getGen_type());
 43                 resultMap.put("fpath", createdMap.get("fpath"));
 44             }
 45         } catch (IllegalAccessException e) {
 46             // TODO Auto-generated catch block
 47             e.printStackTrace();
 48         } catch (InvocationTargetException e) {
 49             // TODO Auto-generated catch block
 50             e.printStackTrace();
 51         }
 52 
 53         return resultMap;
 54     }
 55 
 56     private Map<String, String> createOnlinePlan(TmPlan plan) {
 57         Map<String, String> resultMap = new HashMap<>();
 58         String fname = UUID.randomUUID().toString();
 59 
 60         try {
 61             Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
 62             cfg.setDefaultEncoding("UTF-8");
 63             HashMap<String, Object> dataMap = new HashMap<String, Object>();
 64             dataMap.put("trademark_name", plan.getTm_word());
 65             dataMap.put("telephone", plan.getMobile() == null ? "未提供" : plan.getMobile());
 66             dataMap.put("email", plan.getEmail() == null ? "未提供" : plan.getEmail());
 67             dataMap.put("plan_code", "20160700000012");
 68             dataMap.put("plan_gen_date", DateUtil.getNow("yyyy-MM-dd"));
 69             Map<String, List<PlanDocRecord>> docMap = convertPlanDocMap(plan);
 70             dataMap.put("madeli", docMap.get("madeli"));
 71             dataMap.put("single", docMap.get("single"));
 72             String imageBase64="";
 73             if(plan.getTm_file()!=null&&!plan.getTm_file().trim().equals("")){
 74                 String webroot = ApplicationContextHolder.getContext().getServletContext().getRealPath(ApplicationContextHolder.getContext().get("webroot_image_relative"));
 75                 if(new File(webroot+File.separator+plan.getTm_file()).exists()){
 76                     imageBase64=CollectUtil.getImageStr(webroot+File.separator+plan.getTm_file());
 77                     if(imageBase64==null){
 78                         imageBase64=CollectUtil.getImageStr(ApplicationContextHolder.getContext().getServletContext().getRealPath("/")+"tm_image_default.gif");
 79                     }
 80                 }else{
 81                     imageBase64=CollectUtil.getImageStr(ApplicationContextHolder.getContext().getServletContext().getRealPath("/")+"tm_image_default.gif");
 82                 }
 83             }
 84             dataMap.put("tm_image", imageBase64);
 85             
 86             logger.error(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "WEB-INF/ftl_template");
 87             cfg.setDirectoryForTemplateLoading(new File(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "WEB-INF/ftl_template"));
 88             Template template = cfg.getTemplate("plan.ftl");
 89             String plan_root = ApplicationContextHolder.getContext().get("plan_root");
 90             // File outFile = new
 91             // File("E://unitalen_workspace//unitalen_nginx//WebContent//download//"
 92             // + fname + ".doc");
 93             File outFile = new File(plan_root + fname + ".doc");
 94             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
 95             template.process(dataMap, out);
 96             out.close();
 97 
 98             com.aspose.words.Document conversiondoc = new com.aspose.words.Document(plan_root + fname + ".doc");
 99             conversiondoc.save(plan_root + fname + ".pdf", SaveFormat.PDF);
100         } catch (IOException | TemplateException e) {
101             e.printStackTrace();
102         } catch (Exception e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         }
106 
107         resultMap.put("fpath", fname + ".pdf");
108         return resultMap;
109     }
110 
111     /**
112      * 根据用户提交表单信息创建ftl输入数据
113      * 
114      * @param plan
115      * @return
116      */
117     private Map<String, List<PlanDocRecord>> convertPlanDocMap(TmPlan plan) {
118         Map<String, List<PlanDocRecord>> docMap = new HashMap<>();
119         String plan_caetgory = getResultCategory(plan.getEnt_category());
120         List<PlanDocRecord> madeli = new ArrayList<>();
121         List<PlanDocRecord> single = new ArrayList<>();
122         // 1和2 是单一国申请
123         if (plan.getExport_status().equals("1") || plan.getExport_status().equals("2")) {
124             StringBuffer total = new StringBuffer();
125             // 单一国申请
126             String hot_country = plan.getHot_countries();
127             if (hot_country.endsWith(",")) {
128                 total.append(hot_country);
129             } else {
130                 total.append(hot_country).append(",");
131             }
132             String export_country = plan.getExport_countries();
133             if (export_country.endsWith(",")) {
134                 total.append(export_country);
135             } else {
136                 total.append(export_country).append(",");
137             }
138             if (plan.getChina_reg_status().equals("2")) {
139                 // 中国未申请
140                 total.append("TM_CHIN");
141             }
142             String realTotal = delDuplicateItem(total.toString());
143             String insql = StrUtils.formatToInSql(realTotal, ",");
144             String sql = "select * from tm_db where code " + insql;
145             List<TmDb> dbs = tmDbRepository.findAll(sql, new Object[] {});
146             int i = 1;
147             if (dbs != null && dbs.size() > 0) {
148                 for (TmDb db : dbs) {
149                     PlanDocRecord record = new PlanDocRecord();
150                     record.setCode(String.valueOf(i));
151                     record.setCountry(db.getName());
152                     record.setTips(db.getTips());
153                     record.setType(plan_caetgory);
154                     single.add(record);
155                     i++;
156                 }
157             }
158             docMap.put("single", single);
159             docMap.put("madeli", new ArrayList<>());
160 
161         } else {
162             StringBuffer total = new StringBuffer();
163             // 马德里申请
164             String hot_country = plan.getHot_countries();
165 
166             if (hot_country.endsWith(",")) {
167                 total.append(hot_country);
168             } else {
169                 total.append(hot_country).append(",");
170             }
171             if (plan.getChina_reg_status().equals("2")) {
172                 // 中国未申请
173                 total.append("TM_CHIN");
174             }
175             String insql = StrUtils.formatToInSql(total.toString(), ",");
176             String sql = "select * from tm_db where code " + insql;
177             List<TmDb> dbs = tmDbRepository.findAll(sql, new Object[] {});
178             int i = 1;
179             int j = 1;
180             if (dbs != null && dbs.size() > 0) {
181                 for (TmDb db : dbs) {
182                     if (db.getIs_madeli().equals("1")) {
183                         PlanDocRecord record = new PlanDocRecord();
184                         record.setCode(String.valueOf(i));
185                         record.setCountry(db.getName());
186                         record.setTips(db.getTips());
187                         record.setType(plan_caetgory);
188                         madeli.add(record);
189                         i++;
190                     } else {
191                         PlanDocRecord record = new PlanDocRecord();
192                         record.setCode(String.valueOf(j));
193                         record.setCountry(db.getName());
194                         record.setTips(db.getTips());
195                         record.setType(plan_caetgory);
196                         single.add(record);
197                         j++;
198                     }
199                 }
200             }
201             docMap.put("single", single);
202             docMap.put("madeli", madeli);
203         }
204         return docMap;
205     }

 

posted @ 2016-07-29 14:06  daoning  阅读(922)  评论(0编辑  收藏  举报