java 导出word

java

从页面导出Word文件

  (2017-04-25 09:51:52)

转载▼

标签: 

it

 

教育

 

1.首先新建Word,





另存为xml,再次打开编辑文件,如图所示,

mark改为${mark}然后把文件的扩展名改为ftl,就可以得到模板了。

2.(1)下面是后台代码UrlWordController.java

 

package com.itech.ups.app.activity.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itech.ups.base.web.action.AbstractActionParent;

@Controller
public class UrlWordController  extends AbstractActionParent {
 
 
    @RequestMapping(value = "expWord")
    @ResponseBody
    public void exportMillCertificate(HttpServletRequest request,
                      HttpServletResponse response) throws Exception {
        Map map = new HashMap();
        map.put("mark","哈哈哈");
        new UrlWord().exportMillCertificateWord(request,response,map);
    } 
 }

 

(2)导出Word工具类DaoChuWord.java

package com.itech.ups.app.activity.action;

 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.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class UrlWord {

//配置信息,代码本身写的还是很可读的,就不过多注解了

private static Configuration configuration = null;

//这里注意的是利用UrlWord的类加载器动态获得模板文件的位置

public UrlWord(){

configuration = new Configuration();

configuration.setDefaultEncoding("UTF-8");

}


 

public  void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response,Map map) throws IOException {

File file = null;

InputStream fin = null;

ServletOutputStream out = null;

try {

configuration.setClassForTemplateLoading(this.getClass(), "/");   //FTL文件所存在的位置


 

Template freemarkerTemplate = configuration.getTemplate("test.ftl");

// 调用工具类的createDoc方法生成Word文档

file = createDoc(map,freemarkerTemplate);

fin = new FileInputStream(file);


 

response.setCharacterEncoding("utf-8");

response.setContentType("application/msword");

// 设置浏览器以下载的方式处理该文件名也可以从浏览器传入

String fileName = "test"+new Date().getTime() + ".doc";

response.setHeader("Content-Disposition", "attachment;filename="

.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));


 

out = response.getOutputStream();

byte[] buffer = new byte[512];  // 缓冲区

int bytesToRead = -1;

// 通过循环将读入的Word文件的内容输出到浏览器中

while((bytesToRead = fin.read(buffer)) != -1) {

out.write(buffer, 0, bytesToRead);

}

}catch(Exception o){

o.printStackTrace();

} finally {

if(fin != null) fin.close();

if(out != null) out.close();

if(file != null) file.delete(); // 删除临时文件

}

}


 

 private static File createDoc(Map dataMap, Template template) {
         String name =  "test.doc";//临时文件名
         File f = new File(name);
         Template t = template;
         try {
             // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
             Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
             t.process(dataMap, w);
             w.close();
         } catch (Exception ex) {
             ex.printStackTrace();
             throw new RuntimeException(ex);
         }
         return f;
     }
 }

以上是 java 导出word 的全部内容, 来源链接: utcz.com/z/391436.html

回到顶部