以下只不过是自己一个笔记而已,不喜勿喷!
1.maven中引入jar包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2.建一个excle文件,设置好格式
将其另存为xml格式的文件
3.使用xml格式化工具格式化文件
4.在项目resources目录下创建template文件夹用于存放模版文件,将格式化后的xml文件复制到template文件夹下并改为.ftl格式
修改ftl文件,指定输出的sheet名称
<#assign imap="学生表">
<Worksheet ss:Name="${imap}">
遍历数据
<#list list as item>
<Row>
<Cell ss:StyleID="s50">
<Data ss:Type="Number">${item.id}</Data>
</Cell>
<Cell ss:StyleID="s50">
<Data ss:Type="String">${item.name}</Data>
</Cell>
<Cell ss:StyleID="s50">
<Data ss:Type="Number">${item.age}</Data>
</Cell>
<Cell ss:StyleID="s50">
<Data ss:Type="String">${item.sex}</Data>
</Cell>
<Cell ss:StyleID="s50">
<Data ss:Type="String">${item.content}</Data>
</Cell>
</Row>
</#list>
5.创建导出工具类ExportExcel
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
/**
* @ClassName: ExportExcel
* @Description:freemarker导出execle工具类
* @author: Administrator
* @version V 1.0
* @date: 2017年4月22日 下午4:16:29
*/
public class ExportExcel {
private static Configuration configuration;
private static Template template;
/**
* @Title: export
* @Description: 导出excle
* @author: szc
* @param <E>
* @param templatePath 模版所在路径
* @param dataMap 导出数据
* @param templateName 模版名称
* @param fielName 导出文件的名称
* @date: 2017年4月22日 下午4:47:34
* @since V 1.0
*/
public static <E> boolean export(String templatePath, Map<String, List<E>> dataMap, String templateName, String fielName) {
try {
configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("UTF-8");
configuration.setDirectoryForTemplateLoading(new File(templatePath));
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
template = configuration.getTemplate(templateName);
//直接导出到固定目录
Writer writer=new FileWriter(fielName);
//如果是web项目中的导出,需要将其以流的方式写出,未测试
/*Writer writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fielName), Charset.forName("UTF-8")));*/
template.process(dataMap, writer);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
return false;
}
}
6.创建要导出的student实体类
package com.cn.mybatisDemo.util;
public class Student {
private int id;
private String name;
private int age;
private String sex;
private String content;
public Student() {
}
public Student(int id, String name, int age, String sex, String content) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
7.导出测试类
package com.cn.mybatisDemo.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestExecle {
// 模版所在路径
private static final String templatePath = "src/main/resources/template";
// 模版名称
private static final String templateName = "student.ftl";
// 导出文件放置路径
private static final String filePath = "C:\\Users\\Administrator\\Desktop\\";
public static void main(String[] args) {
TestExecle testExecle = new TestExecle();
List<Student> data = testExecle.getData();
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
map.put("list", data);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
// 导出的文件名称
StringBuffer fileName = new StringBuffer();
fileName.append(filePath).append(format.format(new Date())).append("学生表.xls");
ExportExcel.export(templatePath, map, templateName, fileName.toString());
}
public List<Student> getData() {
List<Student> list = new ArrayList<Student>();
for (int i = 1; i <= 50; i++) {
Student student;
if (i % 2 == 0) {
student = new Student(i, "张萨博" + i, i + 1, "男", "男士");
} else {
student = new Student(i, "王娜" + i, i + 2, "女", "女士");
}
list.add(student);
}
return list;
}
}