通过freemarker制作word比较简单
步骤:制作word模板。制作方式是:将模板word保存成为xml----在xml的word模板中添加相应的标记----将xml的word文件的后缀名改成ftl文件(要注意的是生成xml格式要是2003格式的xml,也就是说拿到的word模板得是2003格式的,否则用wps打开word将会出现问题)
详细步骤如下:
模板制作(将要动态显示的数据打上标记,这个标记是freemarker中的EL标记,要注意的是,要控制值为空的情况,下面${(site.wzmc)?default(“”)}标识当网站名称为空的时候显示空值,如果这里如果不做控制,在实际项目中会显示错误!)
另外要注意的是:
一、不要直接在word中替换掉文字的方式添加标记,这种会有问题。
二、不要使用Eclipse对xml文件进行格式化,这种生成word的时候会提示文档有问题。解决这个的问题是通过firstobject对word的xml进行格式化,对xml进行编辑。(使用firstobject打开带有中文文件名的xml文件的时候,会出现问题,建议使用英文word文档名称),或者使用XMLViewer
三、firstobject下载地址:http://www.firstobject.com/dn_editor.htm
其中,要想软件能够格式化xml代码,需要进行设置,设置方式是:打开firstobject----Tools-----Preferences------Format-----Tabs
点击Indent,结果xml变成了有格式化的,效果图如下:
常见标签:
行标记:
<w:p w:rsidR="00790C22"w:rsidRPr="00C07F75" w:rsidRDefault="00790C22"w:rsidP="004018B7">
标识是一个表格的标签
<w:tbl></w:tb1>
表格行:
<w:trw:rsidR="00790C22" w:rsidRPr="00C07F75"w:rsidTr="004018B7"></w:tr>
表格中的单元格:
<w:tc></w:tc>
循环输出数据的方式
<#list problemInfoInterview as problemInfo> <w:tbl> <w:tblPr> <w:tblW w:w="0" w:type="auto"/> <w:tblBorders> <w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/> <w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/> <w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/> <w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/> <w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/> <w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/> </w:tblBorders> <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/> </w:tblPr> <w:tblGrid> <w:gridCol w:w="2235"/> <w:gridCol w:w="6287"/> </w:tblGrid> <w:tr w:rsidR="00790C22" w:rsidRPr="00C07F75" w:rsidTr="004018B7"> <w:tc> <w:tcPr> <w:tcW w:w="2235" w:type="dxa"/> <w:shd w:val="clear" w:color="auto" w:fill="auto"/> <w:vAlign w:val="center"/> </w:tcPr> <w:p w:rsidR="00790C22" w:rsidRPr="00C07F75" w:rsidRDefault="00790C22" w:rsidP="004018B7"> <w:pPr> <w:jc w:val="center"/> <w:rPr> <w:rFonts w:ascii="Times New Roman" w:eastAsia="黑体" w:hAnsi="Times New Roman"/> <w:sz w:val="24"/> <w:szCs w:val="24"/> </w:rPr> </w:pPr> <w:r> <w:rPr> <w:rFonts w:ascii="Times New Roman" w:eastAsia="黑体" w:hAnsi="Times New Roman" w:hint="eastAsia"/> <w:sz w:val="24"/> <w:szCs w:val="24"/> </w:rPr> <w:t>问题名称</w:t> </w:r> </w:p> </w:tc> <w:tc> <w:tcPr> <w:tcW w:w="6287" w:type="dxa"/> <w:shd w:val="clear" w:color="auto" w:fill="auto"/> <w:vAlign w:val="center"/> </w:tcPr> <w:p w:rsidR="00790C22" w:rsidRPr="00C07F75" w:rsidRDefault="00790C22" w:rsidP="004018B7"> <w:pPr> <w:jc w:val="left"/> <w:rPr> <w:rFonts w:ascii="Times New Roman" w:eastAsia="宋体" w:hAnsi="Times New Roman"/> <w:sz w:val="24"/> <w:szCs w:val="24"/> </w:rPr> </w:pPr> <w:r> <w:rPr> <w:rFonts w:ascii="Times New Roman" w:eastAsia="宋体" w:hAnsi="Times New Roman" w:hint="eastAsia" /> <w:sz w:val="24" /> <w:szCs w:val="24" /> </w:rPr> <w:t>${(problemInfo.problemName)?default("")}</w:t> </w:r> </w:p> </w:tc> </w:tr>
这里面的行删除
</w:tbl> <w:p w:rsidR="007F35A8" w:rsidRPr="007F35A8" w:rsidRDefault="007F35A8" w:rsidP="007F35A8" /> </#list> |
另外,往文档中插入图片的时候,在做模板的时候要往模板中插入一个图片,然后打开文档,然后替换掉base64转码的图片部分。
将图片资源变成base64加密后的图片的代码
将图片转成base64串的方式:
publicstatic String getImageString(String fileName)throws IOException { InputStream in =null; byte[] data =null; try { in =new FileInputStream(fileName); data =newbyte[in.available()]; in.read(data); in.close(); }catch (Exception e) { e.printStackTrace(); }finally { if (in !=null){ in.close(); } } Base64Encoder encoder =new Base64Encoder(); return data !=null ? encoder.encode(data) :""; } |
下面是出word用的相关的类:
读取路径用的TemplateUtil工具类
package com.ucap.netcheck.utils;
import java.io.File; import java.io.FileInputStream; import java.net.URL; import java.util.Properties;
/** * @Title: TemplateUtil.java * @Package com.ucap.netcheck.utils * @Description: * @author * @date 2015-4-13下午9:07:58 * @version V1.0 */ public class TemplateUtil { //模板所在的位置 public static String reportTemplatePath; //模板的名称 public static String templateFileName;
static { URL resource = TemplateUtil.class.getClassLoader().getResource("template.properties"); System.out.println(resource.getPath());
File file = new File(resource.getPath()); //生成文件输入流 FileInputStream in = null; try { in = new FileInputStream(file); } catch (Exception e) { e.printStackTrace(); }
//生成properties对象 Properties prop = new Properties(); try { prop.load(in); } catch (Exception e) { e.printStackTrace(); }
reportTemplatePath = prop.getProperty("reportTemplatePath"); templateFileName = prop.getProperty("templateFileName"); }
/*public static void main(String[] args) { System.out.println(TemplateUtil.reportTemplatePath); }*/ } |
配置文件路径截图:
FreeMarkerUtil工具类
package com.ucap.netcheck.utils;
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import freemarker.template.Configuration; import freemarker.template.Template;
/** *@Title: FreeMarkerUtil.java *@Package com.ucap.netcheck.utils *@Description: FreeMarker工具类 *@authorZuoquan Tu *@date 2015-4-5下午6:02:11 *@version V1.0 */ publicclass FreeMarkerUtil { privatestatic Configurationconfiguration =null; privatestatic Map<String, Template>allTemplates =null;
static { configuration =new Configuration(); configuration.setDefaultEncoding("utf-8"); //configuration.setClassForTemplateLoading(FreeMarkerUtil.class, // "../template");
try { configuration.setDirectoryForTemplateLoading( new File(TemplateUtil.reportTemplatePath)); }catch (IOException e1) { e1.printStackTrace(); } allTemplates =new HashMap<String, Template>(); try { allTemplates.put("word",configuration.getTemplate(TemplateUtil.templateFileName)); }catch (Exception e) { e.printStackTrace(); thrownew RuntimeException(e); } }
public FreeMarkerUtil() {
}
publicstatic File createDoc(Map<?, ?> dataMap,String type){ String name ="temp" + (int) (Math.random() * 100000) +".doc"; File f =new File(name); Template t =allTemplates.get(type); try { //这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word //文档会因为有无法识别的编码而无法打开 Writer w =new OutputStreamWriter(new FileOutputStream(f),"utf-8"); t.process(dataMap, w); w.close(); }catch (Exception ex) { ex.printStackTrace(); thrownew RuntimeException(); } return f; }
publicstatic String getImageString(String fileName)throws IOException { InputStream in =null; byte[] data =null; try { in =new |