1

【java】根据模板生成word下载

 2 years ago
source link: https://holydogs.github.io/2021/05/30/%E3%80%90java%E3%80%91%E6%A0%B9%E6%8D%AE%E6%A8%A1%E6%9D%BF%E7%94%9F%E6%88%90word%E6%96%87%E4%BB%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【java】根据模板生成word下载 | XFF's blog

首先准备word模板,编辑好后在需要插入变量的地方以${xxx}设置变量,然后另存为2003版本的xml文件(好像老版本word才有)。然后再修改后缀为ftl模板文件。

多张图片的插入

<#if listim??>  
<#list listim as listimlist>  
<w:p wsp:rsidR="00682D71" wsp:rsidRDefault="00492E77" wsp:rsidP="00AD0653">  
<w:r>  
<w:rPr>  
<w:rFonts w:hint="fareast" />  
<wx:font wx:val="宋体" />  
</w:rPr>  
<w:t>${listimlist.imgName}</w:t>  
</w:r>  
</w:p>  
<#if listimlist.image??>  
<#list listimlist.image as im>  
<w:p wsp:rsidR="00492E77" wsp:rsidRPr="002D72B0" wsp:rsidRDefault="00372090" wsp:rsidP="00AD0653">  
<w:pPr>  
<w:rPr>  
<w:rFonts w:hint="fareast" /></w:rPr>  
</w:pPr>  
<w:r wsp:rsidRPr="004E4E9E">  
<w:rPr>  
<w:noProof/></w:rPr>  
<w:pict>  
<v:shapetype id="_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">  
<v:stroke joinstyle="miter" />  
<v:formulas>  
<v:f eqn="if lineDrawn pixelLineWidth 0" />  
<v:f eqn="sum @0 1 0" />  
<v:f eqn="sum 0 0 @1" />  
<v:f eqn="prod @2 1 2" />  
<v:f eqn="prod @3 21600 pixelWidth" />  
<v:f eqn="prod @3 21600 pixelHeight" />  
<v:f eqn="sum @0 0 1" />  
<v:f eqn="prod @6 1 2" />  
<v:f eqn="prod @7 21600 pixelWidth" />  
<v:f eqn="sum @8 21600 0" />  
<v:f eqn="prod @7 21600 pixelHeight" />  
<v:f eqn="sum @10 21600 0" /><![](file:///C:\Users\fenghere\AppData\Roaming\Tencent\QQTempSys\[LC3U)F{0XCAB)[email protected]):formulas>  
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />  
<o:lock v:ext="edit" aspectratio="t" />  
<![](file:///C:\Users\fenghere\AppData\Roaming\Tencent\QQTempSys\[LC3U)F{0XCAB)[email protected]):shapetype>  
<w:binData w:name="${"wordml://0"+listimlist_index+3+"00000"+im_index+1+".png"}" xml:space="preserve">${im}</w:binData>  
<v:shape id="图片 1" o:spid="_x0000_i1025" type="#_x0000_t75" style="width:283.5pt;height:217.5pt;visibility:visible">  
<v:imagedata src="${"wordml://0"+listimlist_index+3+"00000"+im_index+1+".png"}" o:title="" /><![](file:///C:\Users\fenghere\AppData\Roaming\Tencent\QQTempSys\[LC3U)F{0XCAB)[email protected]):shape>  
</w:pict>  
</w:r>  
</w:p>  
</#list>  
</#if>  
</#list>  
</#if>

图片转化为base64的编码

File file = new File(imgFile);  
if(!file.exists())return "";  
byte[] data = null;  
InputStream in = null;  
Encoder encoder = Base64.getEncoder();  
try {  
    in = new FileInputStream(file);  
    data = new byte[in.available()];  
    in.read(data);  
    in.close();  
} catch (IOException e) {  
    throw e;  
}finally {  
    if(in != null) in.close();  
}  
return data != null ? encoder.encodeToString(data) : "";

数据模板结合

使用freemarker的template类,将模板和数据合并成文件(调用process(dataMap, out)方法,)然后输出为word。

dataMap封装的是对应模板里$符号的变量,out是模板文件。



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK