- 1 import java.io.File;
- 2 import java.io.FileInputStream;
- 3 import java.io.FileOutputStream;
- 4 import java.io.IOException;
- 5 import java.io.InputStream;
- 6 import java.io.OutputStreamWriter;
- 7 import java.io.Writer;
- 8 import java.net.URLEncoder;
- 9 import java.util.Date;
- 10 import java.util.Map;
- 11
- 12 import javax.servlet.ServletOutputStream;
- 13 import javax.servlet.http.HttpServletRequest;
- 14 import javax.servlet.http.HttpServletResponse;
- 15
- 16 import freemarker.template.Configuration;
- 17 import freemarker.template.Template;
- 18
- 19 public class WordUtils {
- 20 //配置信息,代码本身写的还是很可读的,就不过多注解了
- 21 private static
- Configuration configuration =
- null;
- 22 //这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
- 23 // private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "web-INF/templetes/";
- 24 private static final
- String templateFolder = "H:/我的项目/lm/lm/web/src/main/webapp/WEB-INF/templates"
- ;
- 25 static {
- 26
- configuration =
- new Configuration();
- 27
- configuration.setDefaultEncoding("utf-8"
- );
- 28 try {
- 29
- configuration.setDirectoryForTemplateLoading(
- new File(templateFolder));
- 30
- }
- catch (IOException e) {
- 31 e.printStackTrace();
- 32 }
- 33 }
- 34
- 35 private WordUtils() {
- 36 throw new AssertionError();
- 37 }
- 38
- 39 public static void
- exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map,String title,String ftlFile)
- throws IOException {
- 40
- Template freemarkerTemplate =
- configuration.getTemplate(ftlFile);
- 41
- File file =
- null;
- 42
- InputStream fin =
- null;
- 43
- ServletOutputStream out =
- null;
- 44 try {
- 45 // 调用工具类的createDoc方法生成Word文档
- 46
- file =
- createDoc(map,freemarkerTemplate);
- 47
- fin =
- new FileInputStream(file);
- 48
- 49
- response.setCharacterEncoding("utf-8"
- );
- 50
- response.setContentType("application/msword"
- );
- 51 // 设置浏览器以下载的方式处理该文件名
- 52
- String fileName = title+DateUtil.formatDateDetailTime(
- new
- Date()) + ".doc"
- ;
- 53
- response.setHeader("Content-Disposition", "attachment;filename="
- 54
- .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"
- ))));
- 55
- 56
- out =
- response.getOutputStream();
- 57 byte
- [] buffer =
- new byte
- [512];
- // 缓冲区
- 58 int
- bytesToRead = -1
- ;
- 59 // 通过循环将读入的Word文件的内容输出到浏览器中
- 60 while
- ((bytesToRead = fin.read(buffer)) != -1
- ) {
- 61
- out.write(buffer, 0
- , bytesToRead);
- 62 }
- 63
- }
- finally {
- 64 if
- (fin !=
- null) fin.close();
- 65 if
- (out !=
- null) out.close();
- 66 if
- (file !=
- null
- ) file.delete();
- // 删除临时文件
- 67 }
- 68 }
- 69
- 70 private static
- File createDoc(Map dataMap, Template template) {
- 71
- String name = "sellPlan.doc"
- ;
- 72
- File f =
- new File(name);
- 73
- Template t =
- template;
- 74 try {
- 75 // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
- 76
- Writer w =
- new
- OutputStreamWriter(
- new
- FileOutputStream(f), "utf-8"
- );
- 77 t.process(dataMap, w);
- 78 w.close();
- 79
- }
- catch (Exception ex) {
- 80 ex.printStackTrace();
- 81 throw new RuntimeException(ex);
- 82 }
- 83 return f;
- 84 }
- 85
- }
来源: http://www.cnblogs.com/zwqh/p/7182953.html