import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties; import javax.swing.filechooser.FileSystemView; import freemarker.template.Configuration;
import freemarker.template.Template;
仅带方法:
public void export() {
Map<String, List<Student>> dataMap = new HashMap<String, List<Student>>();
List<Student> list = new ArrayList<Student>();
for(int a = 0; a < 5; a++){
Student s = new Student(a, "stuNo" + a, "name"+a, "age"+a, "class"+a);
list.add(s);
}
dataMap.put("students", list); // 设置配置文件Configuration 此类的创建为单例模式
Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
configuration.setDefaultEncoding("UTF-8");
configuration.setClassForTemplateLoading(this.getClass(), "/com/haiyisoft/iecp/dw/dd/action"); // 获取系统属性名称、系统分割符
Properties properties = System.getProperties();
String osName = properties.getProperty("os.name");
String separator = properties.getProperty("file.separator");
System.out.println(osName + "文件目录分隔符:" + separator); // 获取文件导出路径(默认发布项目下download目录中)
//HttpServletRequest request = ServletActionContext.getRequest();
//String urlStr = request.getSession().getServletContext().getRealPath("/") + "download\\";
//urlStr = urlStr.replace("\\", "/");
// 桌面路径
String urlStr = FileSystemView.getFileSystemView() .getHomeDirectory().getAbsolutePath();
String fileName = "test.doc";
//设置输出文件 输出流 写入类 模板
File outFile = new File(urlStr + "/" + fileName);
FileOutputStream outputStream = null;
Writer writer = null;
Template template = null;
try {
template = configuration.getTemplate("temp.ftl");
outputStream = new FileOutputStream(outFile);
writer = new OutputStreamWriter(outputStream, "UTF-8");
template.process(dataMap, writer);
writer.close();
outputStream.close();
} catch (Exception e) {
System.out.println("出错啦!!!!");
e.printStackTrace();
}
}
// 此为实体类student

public class Student {
private int id;
private String stuNo;
private String name;
private String age;
private String classes;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public Student(int id, String stuNo, String name, String age, String classes) {
super();
this.id = id;
this.stuNo = stuNo;
this.name = name;
this.age = age;
this.classes = classes;
} }

这里只写方法。。
插入list类似于下列类型,将学生信息依次导入,其中${student.stNo!””}为表格中变量,student中变量需与上述实体类中变量对应



模板文件中没有自带list,需手动添加

fremarker导出word list的更多相关文章

  1. java 导出word 并下载

    记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...

  2. Asp.net通过模板(.dot/Html)导出Word,同时导出图片

    一.Office组件导出Word(服务器配置麻烦) 需要引用Office的DLL,在下文的附件中,不同的Offic版本用的不一样,虽然高级版本可以兼容低级的,不过,还是统一版本最好 贴上核心代码(转载 ...

  3. 导出Excel And 导出word

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...

  4. 使用NPOI2.1.3.1版本导出word附带表格和图片

    原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral 最近项目 ...

  5. Java使用velocity导出word

    效果展示: 使用word编辑好模板

  6. 【吉光片羽】MVC 导出Word的两种方式

    1.直接将Html转成Word.MVC自带FileResult很好用.Html中我们也可以嵌入自己的样式. html: <div id="target"> <st ...

  7. java导出word的6种方式(复制来的文章)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  8. 【MVC】 非常简单的页面导出 WORD, EXCEL方法

    [MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...

  9. 网页内容导出word/excel的js代码

    IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化  设为启用! 1.导出word //指定区域导出到Wo ...

随机推荐

  1. Maven Install报错:Perhaps you are running on a JRE rather than a JDK?

    我用的是idea,解决办法是:安装jdk,配置环境变量

  2. JSONP方法简单封装

    function myJsonp(url, data, callback) { //参数判断 if (Object.prototype.toString.call(data) == "[ob ...

  3. python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/

    公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...

  4. asp.net table/gridview 合并列

    public void MergeCell(TableRow trow) { TableCell currentCell = trow.Cells[0]; TableCell prevCell = n ...

  5. 错误笔记 对象为null时调用改对象的方法会报错

    对象为null时调用改对象的方法会报错

  6. Linux tar命令之--exclude参数 排除指定的文件或目录

    https://my.oschina.net/u/3285916/blog/1632552 参数: --exclude 打包时排除不需要处理的文件或目录 说明: tar -zcf a.tar.gz 打 ...

  7. SparkSQL

    Spark SQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了2个编程抽象:DataFrame和DataSet,并且作为分布式SQL查询引擎的作用. Hive SQL是转换成 ...

  8. sql查询单表之中大于2条的数据

    SELECT COUNT(字段)AS COUNT,字段FROM 表名 GROUP BY 字段 HAVING COUNT >=

  9. setOnTouchListener在小米手机中不走ACTION_UP而是走ACTION_CANCEL

    单点触控: MotionEvent.ACTION_DOWN:手指 初次接触到屏幕 时触发. MotionEvent.ACTION_MOVE:手指 在屏幕上滑动 时触发,会多次触发. MotionEve ...

  10. shell 命令 使用

    [root@ken ~]# cat ken.sh http://www.baidu.com http://www.baidu.com http://www.sina.com http://www.si ...