import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class Test_export_Excel {
/** 设置表格标题 */
private static List<String> getTitles(){
List<String> titles = new ArrayList<>();
titles.add("序号");
titles.add("姓名");
titles.add("年级");
titles.add("班级");
titles.add("语文");
titles.add("数学");
titles.add("英语");
return titles;
}
/** 设置表格内容 */
private static List<Student> getStudents(){
List<Student> students = new ArrayList<>();
for(int i=0; i<10; i++){
Student student = new Student(100 + i, "zong_"+i, 1+i, 2+i, 90+i, 80+i, 100-i);
students.add(student);
}
return students;
}
public static void main(String[] args) throws Exception{
//加载freemarker 模板,获取模板对象
Configuration cfg = new Configuration();
cfg.setDefaultEncoding("UTF-8");
cfg.setDirectoryForTemplateLoading(new File("templates"));
Template studentTemplate = cfg.getTemplate("123.ftl");
//设置表格标题和内容
List<String> titles = getTitles();
List<Student> students = getStudents();
Map root = new HashMap();
root.put("titles", titles);
root.put("students", students);
root.put("loanName","test test");//文字換行
File file = new File("D:/excel_students.xls");
PrintWriter fw = new PrintWriter(file,"UTF-8");
//按照ftl 模板规定的样式,将root 对象里的内容写入新的文件
studentTemplate.process(root, fw);
fw.flush();
fw.close();
}
}