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 ...
随机推荐
- SQL CE 和 SQLite数据库对比测试
于项目需要,在客户端需要做数据存储功能,考虑到部署方便同时满足功能需要的情况下选择了SQLCE 和SQLite两种数据库进行客户端数据存储.当然还有很多其他的方式做本地数据存储,比如本地文件存储.微软 ...
- 移动端右侧导航 显示隐藏js
transform与fixed影响 html按钮 <span class="nav-btn"></span> <span class="cl ...
- MySQL时区错误导致server time zone value 'Öйú±ê׼ʱ¼ä' 错误
时区错误 由于中国是东八区,跟mysql配置不同,需要修改: 管理员登录MySQL OK成功
- java - day003 - 循环嵌套, 循环命名, while, 数组
1.循环嵌套 break 中断循环或switch(跳出循环).中断后继续往下执行 continue (跳到循环的下一轮继续执行) return (结束方法) 2.循环命名 内层循环控制外层循环.需要给 ...
- kali linux 网络配置
/etc/init.d/networking restart service newworking restart ifdown eth0 ifup eth0 ifconfig down eth0 i ...
- HTTP协议详解及http1.0与http1.1http2.0的区别
HTTP协议介绍 http(超文本传输协议)是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.特点: (1)支持客户/服务器模式. HTTP是一个客户端和服务器端请 ...
- angular2报错
1.运行ng serve的时候卡在95% emitting LicenseWebpackPlugin 放弃使用cnpm,使用yarn 删除node_modules (不需要删除project目录重新n ...
- Visual Studio Code 搭建Python开发环境
1.下载Python https://www.python.org/downloads/windows/ 选择一个版本,目前2.0的源码比较多,我下载的2.7.12 2.配置环境变量 3.Visual ...
- 大数据学习之Linux进阶02
大数据学习之Linux进阶 1-> 配置IP 1)修改配置文件 vi /sysconfig/network-scripts/ifcfg-eno16777736 2)注释掉dhcp #BOOTPR ...
- RESTful-2一分钟理解什么是REST和RESTful
从事web开发工作有一小段时间,REST风格的接口,这样的词汇总是出现在耳边,然后又没有完全的理解,您是不是有和我相同的疑问呢?那我们一起来一探究竟吧! 就是用URL定位资源,用HTTP描述操作. 知 ...
