tcWw:
w="2181"w:
type="dxa"/>
gridSpanw:
val="3"/>
vAlignw:
val="center"/>
tcPr>
pwsp:
rsidR="001E023B"wsp:
rsidRDefault="001D2E3B"wsp:
rsidP="004E45BD">
r>
rPr>
rFontsw:
hint="fareast"/>
rPr>
t>${e.COLUMN_5}
t>
r>
p>
tc>
tr>
#list>
--循环结束-->
二、详细介绍
1、以上呢都是我看别人的东西复制过来的其实都一样FreeMaker最主要的一个特点就是整理模板数据替换用${data};data就是你的数据
那在java中保存的数据时用map来存放数据
如:
Maproot=newHashMap();、
如现在要保存一个实体user;
User的对象有id,name,sex,menun(菜单),picstr(头像)这些属性
UseruserObj=newUser();
在保存的时候只要将数据库中的数据封装到user中然后通过root.put(“user”,userObj);
在模板相应的位置中代替这些值如名称:
${user.name},性别${user.sex}等
当遇到对象有子集的时候也就是用户有几个菜单时可以使用<#listuser.menunasm>在这个中间又可以得到自己想要的东西如${m.menunName}#list>当然这个是循环的对于它循环的索引直接使用变量名+下划线index即可如:
${m_index};
2、这些东西估计大家都会现在说说插入目录问题
因为目录需要内容创建完成之后才能刷新目录所以在此处我寻求好多方法不知道怎么使用最后我折中想了个办法就是在xml转为word完成之后我另存一份word;在另存之前我先刷新目录这样就把目录问题解决了;废话少说例子上:
publicvoidxmlToWord(Stringdocfile,Stringhtmlfile){
ActiveXComponentapp=null;
try{
app=newActiveXComponent("Word.Application");//启动word
app.setProperty("Visible",newVariant(false));
Dispatchdocs=app.getProperty("Documents").toDispatch();
Dispatchdoc=Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
newObject[]{docfile,newVariant(false),
newVariant(true)},newint[1]).toDispatch();
Dispatchselection=app.getProperty("Selection").toDispatch();
Dispatchfind=Dispatch.call(selection,"Find").toDispatch();
/*设置要查找的内容*/
Dispatch.put(find,"Text","目录");
/*向前查找*/
Dispatch.put(find,"Forward","True");
/*设置格式*/
Dispatch.put(find,"Format","True");
/*大小写匹配*/
Dispatch.put(find,"MatchCase","True");
/*全字匹配*/
Dispatch.put(find,"MatchWholeWord","True");
/*查找并选中*/
Dispatch.call(find,"Execute").getBoolean();
/*取得ActiveDocument、TablesOfContents、range对象*/
DispatchActiveDocument=app.getProperty("ActiveDocument").toDispatch();
DispatchTablesOfContents=Dispatch.get(ActiveDocument,"TablesOfContents").toDispatch();
Dispatch.call(selection,"MoveRight");//移动光标到右边
Dispatch.call(selection,"TypeParagraph");//换行
Dispatchrange=Dispatch.get(selection,"Range").toDispatch();
/*增加目录*/
Dispatch.call(TablesOfContents,"Add",range,newVariant(true),newVariant
(1),newVariant(3),newVariant(true),newVariant(""),newVariant(true),newVariant(true));
Dispatch.invoke(doc,"SaveAs",Dispatch.Method,newObject[]{
htmlfile,newVariant(WORD_DOC)},newint[1]);
Variantf=newVariant(false);
Dispatch.call(doc,"Close",f);
}finally{
if(app!
=null)
app.invoke("Quit",newVariant[]{});
}
}
原理就是我在模板中有一页单独为目录留下来有目录两字,通过查找目录两字并且定位光标到目录之后并且换行;然后添加目录保存即可;
3、图片问题
相信大家都知道在word里面插入一张图片保存为xml的时候图片的位置会产生出很长的一串字符当你看到这个的时候那么替换图片你有希望;观察发现是二进制字符串所以你在准备数据的时候也要提前把需要保存的图片转成二进制字符串并存到变量中如picstr并在xml中替换${user.picstr}
pict>
shapetypeid="_x0000_t75"coordsize="21600,21600"o:
spt="75"o:
preferrelative="t"path="m@4@5l@4@11@9@11@9@5xe"filled="f"stroked="f">
strokejoinstyle="miter"/>
formulas>
feqn="iflineDrawnpixelLineWidth0"/>
feqn="sum@010"/>
feqn="sum00@1"/>
feqn="prod@212"/>
feqn="prod@321600pixelWidth"/>
feqn="prod@321600pixelHeight"/>
feqn="sum@001"/>
feqn="prod@612"/>
feqn="prod@721600pixelWidth"/>
feqn="sum@8216000"/>
feqn="prod@721600pixelHeight"/>
feqn="sum@10216000"/>
formulas>
patho:
extrusionok="f"gradientshapeok="t"o:
connecttype="rect"/>
lockv:
ext="edit"aspectratio="t"/>
shapetype>
binDataw:
name="wordml:
//06000001.png">${user.picstr?
if_exists}
binData>
shapeid="_x0000_i1025"type="#_x0000_t75"style="width:
297pt;height:
284.25pt">
imagedatasrc="wordml:
//06000001.png"o:
title="gisImage"/>
shape>
pict>
这样图的问题呢就解决了。
但是大多时候需求不是这样的是有多个图片需要同时出现虽然变量你已经替换但是你会发现图片都出来了但是都是显示的是同一张图片。
仔细研究发现图片在替换变量的时候不只是要替换内容的那块同时也要注意名称的地方也就是w:
name="wordml:
//06000001.png">06000001.png这个就是他在word中生存的名称因为图片的不确定性质所以你的名称就无法定下来所以灵机一动把名称也弄成随机变量(这个地方随机变量你可以在java端处理好这边用变量替换或者使用现成的变量那就是使用list循环)因为图片是多个势必你要使用<#listimageasi>这个时候${i_index}这个是变化的那么图片名称就改为
binDataw:
name="${"wordml:
//0600000"+i_index+1+".jpg"}">${i.picStr?
if_exists}
binData>
shapeid="_x0000_i1025"type="#_x0000_t75"style="width:
297pt;height:
284.25pt">
imagedatasrc="${"wordml:
//0600000"+i_index+1+".jpg"}"o:
title="gisImage2"/>
shape>
在这里啰嗦下一定要注意双引号图片问题完美解决;
4、页眉页脚问题好多人在制定模板的时候都忘记了设置页眉页脚页面设置等问题;一旦模板做好并且把变量替换完成之后那么再使用word就无法打开xml文件了当然你就无法再设置word的东西了所以只能通过代码来添加嘿嘿本人就遇到过;当然有人也说我可以重新做反正也不多我设置好了在替换变量对于这种想法的人我表示理解……;等你的模板什么时候有几十页的时候,或者循环套好几层的时候我看你肿么办。
言归正传:
页面页脚添加问题:
从新新建一个word把你想要在模板中实现好的所有东西都设置好然后copy这些代码到xml问题解决呵呵:
上代码:
stylew:
type="paragraph"w:
styleId="a6">
namew:
val="header"/>
uiNamewx:
val="页眉"/>
basedOnw:
val="a"/>
rsidw:
val="00302287"/>
pPr>
pStylew:
val="a6"/>
pBdr>
bottomw:
val="single"w:
sz="6"wx:
bdrwidth="15"w:
space="1"w:
color="auto"/>
pBdr>
tabs>
tabw:
val="center"w:
pos="4153"/>
tabw:
val="right"w:
pos="8306"/>
tabs>
snapToGridw:
val="off"/>
jcw:
val="center"/>
pPr>
rPr>
fontwx:
val="TimesNewRoman"/>
szw:
val="18"/>
sz-csw:
val="18"/>
rPr>
style>
stylew:
type="paragraph"w:
styleId="a7">
namew:
val="footer"/>
uiNamewx:
val="页脚"/>
basedOnw:
val="a"/>
rsidw:
val="00302287"/>
pPr>
pStylew:
val="a7"/>
tabs>
tabw:
val="center"w:
pos="4153"/>
tabw:
val="right"w:
pos="8306"/>
tabs>
snapToGridw:
val="off"/>
jcw:
val="left"/>
pPr>
rPr>
fontwx:
val="TimesNewRoman"/>
szw:
val="18"/>
sz-csw:
val="18"/>
rPr>
style>
其实最主要的是下面这段:
hdrw:
type="odd">
pBdrGroup>
borders>
bottomwx:
val="solid"wx:
bdrwidth="15"wx:
space="1"wx:
color="auto"/>
borders>
pwsp:
rsidR="00302287"wsp:
rsidRDefault="00302287">
pPr>
pStylew:
val="a6"/>
pPr>
r>
r>
rPr>
rFontsw:
hint="fareast"/>
fontwx:
val="宋体"/>
rPr>
t>测振报告
t>
r>
r>
rPr>
rFontsw:
hint="fareast"/>
rPr>
t>
t>
r>
r>
rPr>
rFontsw:
hint="fareast"/>
fontwx:
val="宋体"/>
rPr>
t>${vibrometerMissionVo.testReport.reportTitle?
if_exists}
t>
r>
pict>