概述
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,需手动添加
转载于:https://www.cnblogs.com/TheKiteRunner/p/9468093.html
最后
以上就是无辜洋葱为你收集整理的fremarker导出word list的全部内容,希望文章能够帮你解决fremarker导出word list所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复