fremarker导出word list
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的更多相关文章
- java 导出word 并下载
记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...
- Asp.net通过模板(.dot/Html)导出Word,同时导出图片
一.Office组件导出Word(服务器配置麻烦) 需要引用Office的DLL,在下文的附件中,不同的Offic版本用的不一样,虽然高级版本可以兼容低级的,不过,还是统一版本最好 贴上核心代码(转载 ...
- 导出Excel And 导出word
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...
- 使用NPOI2.1.3.1版本导出word附带表格和图片
原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral 最近项目 ...
- Java使用velocity导出word
效果展示: 使用word编辑好模板
- 【吉光片羽】MVC 导出Word的两种方式
1.直接将Html转成Word.MVC自带FileResult很好用.Html中我们也可以嵌入自己的样式. html: <div id="target"> <st ...
- java导出word的6种方式(复制来的文章)
来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...
- 【MVC】 非常简单的页面导出 WORD, EXCEL方法
[MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...
- 网页内容导出word/excel的js代码
IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化 设为启用! 1.导出word //指定区域导出到Wo ...
随机推荐
- mysql 经纬度求距离
SELECT id,lng,lat,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((lat1*PI()/180-lat*PI()/180)/2),2)+COS(lat1*PI( ...
- Tensorflow笔记二
MNIST手写体识别 (Mixed National Institute of Standards and Technology database)的28*28字符识别共0-9类. 在ipython命 ...
- webstorm Terminal 位置错乱解决方案
win+R 打开终端,右击属性->勾选使用旧版控制台->确定 ,再重启webstrom 即可.
- Lesson 1-1
1.1常见难记的几种运算符 1.1.1 除运算 ‘/’ 除运算的结果为小数,即浮点数. >>> 10/3 3.3333333333333335 >>> 10/2 5 ...
- Zabbix中获取各用户告警媒介分钟级统计
任务内容: 获取Zabbix各用户告警媒介分钟级统计,形成趋势图,便于观察各用户在每分钟收到的告警数量,在后续处理中,可以根据用户在某时间段内(例如3分钟内)收到的邮件总数,来判断是否有告警洪水的现象 ...
- loadrunner出现中文乱码
录制的脚本出现乱码 录制的时候出现乱码,有的时候可以正常回放,我们可以不用管它,但如果影响回放,我们可以使用以下方法解决: 1)更改录制选项 选择菜单栏 Tools --> Recording ...
- springmvc 控制器 读取properties文件
配置文件app.properties如下: yt.api.url=http://localhost:9000 springmvc.xml 增加配置: <context:property-plac ...
- Java 多线程系列 CountDownLatch
CountDownLatch 一个或多个线程等待其他线程完成操作后在在执行 CountDownLatch通过一个计数器来实现,await方法阻塞直到 countDown() 调用计数器归零之后释放所有 ...
- python 的os.getenv("PATH")和os.environ.get("PATH")的区别
os.environ(x [,x]) raises an exception if the environmental variable does not exist. os.getenv(x) do ...
- sql--迁移条件数据和补全数值的一个流程
目的:我要把老顾客的部分数据迁移到另一个表里面 -- 步骤一:筛选查询-- 打开表,只显示想要看到的数据列-- 做条件筛选,筛选出想要的数据 -- 步骤二:sql查询 SELECT ID,Name,G ...
