使用FreeMarker生成word文档
来源:互联网 发布:手机音乐剪切软件 编辑:程序博客网 时间:2024/04/29 05:20
最近再做一个项目需要生成word文档,通过在网上收集了一些资料后决定采用Freemarker 来生成word文档。其基本过程如下所示:
1.编辑文档模板
该文档的XML格式(格式控制部分省略)如下所示,其中加粗部分是添加的FreeMarker标记。
<w:t>测试文档</w:t>
…………………………
<w:t>作者</w:t></w:r><w:r wsp:rsidR="00E05F5B"><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>${author}</w:t></w:r></w:p>
…………………………
<w:t>日期</w:t></w:r><w:r wsp:rsidR="00E05F5B"><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/></w:rPr><w:t>${date}</w:t></w:r></w:p>
…………………………
<w:t>版本</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="fareast"/>
</w:rPr><w:t>:${version}</w:t></w:r></w:p>
…………………………
<w:t>控件信息表</w:t></w:r></w:p>
…………………………
<w:t>编号</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>名称</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>位置</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>长度</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>宽度</w:t></w:r></w:p></w:tc><w:tc><w:tcPr
…………………………
<w:t>说明</w:t></w:r></w:p></w:tc></w:tr>
<#list tabel as list>
…………………………
<w:t>${list.id}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>${list.text}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>${list.location}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>${list.length}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>${list.width}</w:t></w:r></w:p></w:tc><w:tc><w:tcPr>
…………………………
<w:t>${list.decration}</w:t></w:r></w:p></w:tc></w:tr>
</#list></w:tbl>
…………………………
<w:t>界面截图:</w:t></w:r></w:p>
…………………………
<w:binDataw:name="wordml://02000001.jpg"xml:space="preserve">${image}</w:binData>
…………………………
</w:sectPr></wx:sect></w:body>
2)将加入FreeMarker标记的文件保存为FTL格式,作为文档模板。编写程序向模板中写入数据;
/**
*通过使用该Javabean,可以向表单中循环输入数据;
*Javabean中主要对表单的属性信息进行控制。
**/
public class tabel {
private int id;
private String text;
private String location;
private int length;
private int width;
private String decration;
public String getDecration() {
return decration;
}
public void setDecration(String decration) {
this.decration = decration;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getLocation() {
return location.toString();
}
public void setLocation(String str) {
this.location =str;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
/**
*向模板中诸如数据,生成说明文档;
***/
public class WordTest {
private Configuration config = null;
public WordTest() {
config = new Configuration();
config.setDefaultEncoding("UTF-8");
}
public void createWord() {
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
//此处获取模板信息
config.setClassForTemplateLoading(this.getClass(), "/org/cs/getword/template");
Template t = null;
try {
t = config.getTemplate("test.ftl");
} catch (IOException e) {
e.printStackTrace();
}
//生成的说明文档存放路径
File output = new File("D:/temp/output.doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getData(Map<String, Object> dataMap) {
dataMap.put("author", "陈松");
dataMap.put("date", "2015.3.2");
dataMap.put("version", "version1.0");
List<tabel> _table=new ArrayList<tabel>();
for(int i=0;i<10;i++){
tabel t=new tabel();
t.setId(1+i);
t.setText("提交");
t.setLocation("(100,10)");
t.setLength(10+i);
t.setWidth(5+i);
t.setDecration("单击此按钮提交信息!");
_table.add(t);
}
dataMap.put("tabel", _table);
String imageFile="D:/temp/test.jpg";
InputStream in=null;
byte[] data=null;
try{
in=new FileInputStream(imageFile);
data=new byte[in.available()];
in.read(data);
in.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
//对图像数据进行Base64编码。
BASE64Encoder encoder=new BASE64Encoder();
dataMap.put("image",encoder.encode(data));
}
}
所得结果为:3.所生成的文档。
- 使用FreeMarker生成word文档
- 使用FreeMarker生成Word文档
- SpringMVC中使用FreeMarker生成Word文档
- SpringMVC中使用FreeMarker生成Word文档
- 使用FreeMarker生成Word文档中的表格
- FreeMarker生成word文档
- freeMarker生成word文档
- Freemarker生成word文档
- freemarker生成Word文档
- 用 Freemarker 生成 word 文档
- 用 Freemarker 生成 word 文档
- 用 Freemarker 生成 word 文档
- spring+freemarker生成word 文档
- 用FreeMarker生成Word文档
- 用freemarker生成word文档
- 使用Freemarker生成Word
- 使用freemarker生成word
- 使用freemarker生成word
- 【Jason's_ACM_解题报告】Tour
- vim查找/替换字符串
- 外观模式
- multipart/form-data和application/x-www-form-urlencoded的区别
- X86汇编指令小结
- 使用FreeMarker生成word文档
- Android总结篇系列:Android Service
- 土豪就是任性,赠送2015年北风网200元学习卡很多张,有需要的自己拿去
- Hadoop 2.0+YARN启动脚本分析与实战经验
- 太空互联网计划:全球覆盖WiFi热点
- Download模块 (十六)
- POJ 3258 - River Hopscotch(二分搜索)
- 史上最全的iOS面试题及答案
- [Codeforces #295(Div 1)]简要题解