freemarker导出excle
来源:互联网 发布:高性能php架构 编辑:程序博客网 时间:2024/06/07 06:10
以下只不过是自己一个笔记而已,不喜勿喷!
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; }}
0 0
- freemarker导出excle
- Excle导出
- 导出EXCLE
- excle导出
- 导出excle
- excle导出
- excle导出
- 数据导出Excle
- 数据导出Excle !
- SQL导出Excle 心得
- CSVWriter----excle导出示例
- C#导出Excle文件
- 类对象导出excle
- 导出excle,乱码解决
- 简单方法导出EXCLE
- POI导出Excle HSSF
- 导出到excle
- js table导出excle
- Android studio 在分享项目到github上时,会出现项目已存在说的错误,
- java.io.Serializable
- python多线程
- 队列与堆栈
- String问题
- freemarker导出excle
- ACM ArabellaCPC 2015F题 并查集
- 清华镜像下载Android源码
- 笔试数理题总结
- java线程学习
- 给kali的Metasploit下添加一个新的exploit
- Nginx 源码阅读笔记6 master 主循环
- React Native之Modal组件实现遮罩层效果
- DataFrame筛选数据与loc用法