前几天做了jxl导出excel,现在用freemarker做一下

freemarker导出excel和导出word步骤和是实现方法是相同的。

1.制作excel模板

2.将后缀名改为ftl,放到对应的位置下

3.实现方法

 package org.lq.ssm.gp.controller;

 import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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 org.lq.ssm.entity.LandUser;
import org.springframework.web.bind.annotation.RequestMapping; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler; public class exportExcel { private Configuration configuration = null; public exportExcel(){
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
//@RequestMapping(params="print")
public void print() throws IOException { configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
// 要填入模本的数据文件
Map dataMap = new HashMap();
//getData(dataMap); List<Map<String, Object>>list=new ArrayList<Map<String,Object>>();
for(int i=0;i<10;i++){
Map<String, Object>map=new HashMap<String, Object>();
map.put("userName", "张三");
map.put("landName", "张三");
map.put("landScmj", "111111");
map.put("landBzdate", "20170626");
list.add(map);
}
dataMap.put("list", list); // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板
//configuration.setDirectoryForTemplateLoading(new File("G:\\")); configuration.setClassForTemplateLoading(this.getClass(), "/org/lq/ssm/gp/controller");
//设置异常处理器
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("land.ftl");
t.setEncoding("utf-8"); } catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("G:/TTT/land.xls");
Writer out = null; try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
} try {
t.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 注意dataMap里存放的数据Key值要与模板中的参数相对应
* @param dataMap
* @throws IOException
*
*/ public static void main(String[] args) throws IOException { exportExcel exc=new exportExcel();
exc.print();
} }

4.excel表格就好了。但是可能在打开文件的时候,出现错误。

这时候修改模板内容,找到

将值改大一点,就可以了。

java用freemarker实现导出excel的更多相关文章

  1. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  2. Java中导入、导出Excel

    原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...

  3. Java 用Freemarker完美导出word文档(带图片)

    Java  用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...

  4. 我是陌生人 Java中导入、导出Excel

    我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...

  5. [转]Java中导入、导出Excel

    原文地址:http://blog.csdn.net/jerehedu/article/details/45195359 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样 ...

  6. Java报表工具FineReport导出EXCEL的四种API

    在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...

  7. Java的导入与导出Excel

    使用Jakarta POI导入.导出Excel Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其中有用于操作Excel格式文件的HSSF和 ...

  8. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

  9. java使用poi生成导出Excel(新)

    导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...

随机推荐

  1. MySQL 5.7.10最新版本号源码安装具体过程

    ,重置密码 利用mysqladmin重置密码 [root@wgq_idc_mon_1_12 mysql5710]#./bin/mysqladmin -h localhost -uroot passwo ...

  2. STM32F030, 使用嘀嗒定时器Systick实现LED闪烁

    本文主要解决两个问题 1 STM32的IO口要反转,怎么实现? 2 嘀嗒定时器systick的配置 解答1: 单片机的口,反转非常easy.sbit led = P1 ^6;  led = ~led; ...

  3. HDU 1754(线段树区间最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. linux下使用convert命令修改图片分辨率【转】

    本文转载自:http://blog.csdn.net/mybelief321/article/details/9969949 Convert的resize子命令应该是在ImageMagick中使用较多 ...

  5. NumPy和Pandas常用库

    NumPy和Pandas常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数 ...

  6. Python·Jupyter Notebook各种使用方法记录

    标签(空格分隔): Python 一 Jupyter NoteBook的安装 1 新版本Anaconda自带Jupyter 2 老版本Anacodna需自己安装Jupyter 二 更改Jupyter ...

  7. USACO 2.2 Preface Numbering

    Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...

  8. 在阿里云的iis上安装php扩展

    具体参照http://jingyan.baidu.com/article/11c17a2c5ce349f447e39d6d.html

  9. javascript中的闭包以及闭包应用

    闭包简单理解就是能够读取其他函数内部变量的函数,而在javascript中只有内部函数可以读取函数的内部变量,所以我们学习javascript时可以这样理解,函数A中嵌套了一个函数B,然后我们用函数B ...

  10. 35.QT蝴蝶飞舞

    fly.h #ifndef FLY_H #define FLY_H #include <QObject> #include <QPainter> #include <QG ...