1.使用word2013或者2007 将要用到的数据格式编写好
后台数据需要的是 Map集合 集合中的key 对应要在
word上显示的值 如 key=name 那么 word上${name}来接收
2.编写完成后,另存为2003.xml的格式
3.将2003.xml 改为2003.ftl 模板生成
工具类
public class WordExport {
后台数据需要的是 Map集合 集合中的key 对应要在
word上显示的值 如 key=name 那么 word上${name}来接收
2.编写完成后,另存为2003.xml的格式
3.将2003.xml 改为2003.ftl 模板生成
工具类
public class WordExport {
private static Configuration configuration = new Configuration();
public static void createOutputFile(String path,String moduleName,Map<String,Object> dataMap) {
configuration.setDefaultEncoding("utf-8");
File file = new File(path);
Template t=null;
try {
configuration.setDirectoryForTemplateLoading(file);
t = configuration.getTemplate("222.ftl");
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File(path,moduleName+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
} catch (Exception e1) {
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
控制层
控制层
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String path = request.getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
System.out.println(path+"........");
Map<String , Object> mapData = new HashMap<String, Object>();
System.out.println(request.getContextPath());
mapData.put("name", "天津海事局");
String name=new SimpleDateFormat("yyyyMMddhhmmsss").format(new Date());
WordExport word = new WordExport();
word.createOutputFile(path,name,mapData);
File fileInfo = new File(path+name+".doc");//service.exportDocument("")
response.setContentType("text/html;charset=utf-8");
response.setHeader("Content-disposition","attachment;filename="+"xxxx.doc");
InputStream in = new FileInputStream(fileInfo);
System.out.println(fileInfo.length());
OutputStream out = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=in.read(buffer))!=-1)
out.write(buffer, 0, len);
in.close();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
以下是通用word下载方法
@RequestMapping("dockrepair/reportword")
public void reportword(HttpServletRequest request,HttpServletResponse response,String id){
Map<String, Object> reportword = service.reportword(id);
WordExport we=new WordExport();
String fname=new SimpleDateFormat("yyyyMMddhhmmsss").format(new Date());
we.createOutputFile("坞修记录单.ftl", fname, reportword);
FileDowload f=new FileDowload();
String path=request.getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
f.dowload(response,path,fname,"坞修记录单.doc");
}
public class WordExport {
private static Configuration configuration = new Configuration();
/**
*
* @param path 文件路径
* @param ftlname 模板名称
* @param fname 文件名称
* @param dataMap 数据集
* @return File
*/
public static void createOutputFile(String ftlname,String fname,Map<String,Object> dataMap) {
configuration.setDefaultEncoding("utf-8");
//String path = Local.getRequest().getSession().getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"template"+File.separator;
File file = Local.getProjectSpace().getTemplateSpace();
Template t=null;
try {
configuration.setDirectoryForTemplateLoading(file);
t = configuration.getTemplate(ftlname);
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File(file.getPath(),fname+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
} catch (Exception e1) {
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File(file.getPath(),fname+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
} catch (Exception e1) {
e1.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class FileDowload {
public void dowload(HttpServletResponse response,String path,String name,String realname){
try {
response.setContentType("text/html;charset=utf-8");
response.setHeader("Content-disposition","attachment;filename="+new String(realname.getBytes("gbk"),"iso-8859-1"));
File file = new File(path+name+".doc");
InputStream in = new FileInputStream(file);
System.out.println(file.length());
OutputStream out = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=in.read(buffer))!=-1)
out.write(buffer, 0, len);
in.close();
out.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void dowload(HttpServletResponse response,String path,String name,String realname){
try {
response.setContentType("text/html;charset=utf-8");
response.setHeader("Content-disposition","attachment;filename="+new String(realname.getBytes("gbk"),"iso-8859-1"));
File file = new File(path+name+".doc");
InputStream in = new FileInputStream(file);
System.out.println(file.length());
OutputStream out = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=in.read(buffer))!=-1)
out.write(buffer, 0, len);
in.close();
out.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}