在网上查找了很多导出word的方法,poi就不说了简单的内容导出word可以,但是遇到复杂的内容过多就不是好的选择了。下面根据上网参考的资料自己做了模板调整之后做的。记录下来大家分享下:
首先做了一个word模板,如下:
将其另存为.xml文件,在利用notepad打开。将“1”替换为${one},2替换为${two},下面的表格做为集合的形式,找打编号1的位置的w:tr,在上面加<#list list as test>同时在/w:tr结尾处加</#list>。将编号1改为${test.bianhao}工作内容1改为${test.gongzuo}。以此类推其他的也做处理,之后将.xml文件改为.ftl保存。
最后就是如何导出word:
package HttpWeb;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class WordTest {
private Configuration configuration = null;
public WordTest() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public static void main(String[] args) {
WordTest test = new WordTest();
test.createWord();
}
public void createWord(){
Map<String,Object> dataMap=new HashMap<String,Object>(); //dataMap 要填入模本的数据文件
getData(dataMap);
//这里我们的模板是放在HttpWeb包下面
//第一种
configuration.setClassForTemplateLoading(this.getClass(), "/HttpWeb"); //FTL文件所存在的位置(基于类路径)
//第二种
/*try {
configuration.setDirectoryForTemplateLoading(new File("/template"));//(基于文件系统)
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}*/
//第三种就是 /WebRoot/template目录。
// HttpServletRequest request = ServletActionContext.getRequest();
// configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template");//基于Servlet Context(web project)第二个参数是基于WebRoot下的
Template t=null;
try {
t = configuration.getTemplate("framemaker.ftl"); //framemaker.ftl为要装载的模板
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("D:/outFilessa"+Math.random()*10000+".doc"); //输出文档路径及名称
if(!outFile.exists()){
try {
outFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Writer out = null;
FileOutputStream fos=null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = null;
try {
oWriter = new OutputStreamWriter(fos,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。
out = new BufferedWriter(oWriter);
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.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("one", "10");
dataMap.put("two", "25");
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < 10; i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("bianhao", i);
map.put("gongzuo", "内容"+i);
map.put("cundangren", "存档人"+i);
map.put("kaishidate", "开始时间"+i);
map.put("jieshudate", "结束时间"+i);
list.add(map);
}
dataMap.put("list", list);
}
}