JAVA 通过POI 模版,导出excel
如有不足,欢迎指正,谢谢 !
1、Maven引入 POI jar包、模版和结果文件.rar下载
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.4</version>
</dependency>
2、工具方法如下:
package com.***.***.***;
import net.sf.jxls.transformer.XLSTransformer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
CLASS 省略。。。 /**
*
* @param request
* @param response
* @param tempPath 模版的相对路径
* @param resultMap 结果集
* @throws Exception
*/
public static void excelTempExport(HttpServletRequest request,HttpServletResponse response,String tempPath, Map resultMap) {
String tempFileName = null;
String newFileName = null;
File newFile = null;
XLSTransformer transformer = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//获得模版
tempFileName = request.getSession().getServletContext().getRealPath(tempPath);
//新的文件名称
newFileName= new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//文件名称统一编码格式
newFileName = URLEncoder.encode(newFileName, "utf-8");
//生成的导出文件
newFile = File.createTempFile(newFileName, ".xls");
//transformer转到Excel
transformer = new XLSTransformer();
//将数据添加到模版中生成新的文件
transformer.transformXLS(tempFileName, resultMap, newFile.getAbsolutePath());
//将文件输入
InputStream inputStream = new FileInputStream(newFile);
// 设置response参数,可以打开下载页面
response.reset();
//设置响应文本格式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + String.valueOf((newFileName + ".xls").getBytes(), "iso-8859-1"));
//将文件输出到页面
ServletOutputStream out = response.getOutputStream();
bis = new BufferedInputStream(inputStream);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// 根据读取并写入
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//使用完成后关闭流
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {}
}
} //测试类
@RequestMapping(value = "/exportExcelTest", method = RequestMethod.GET)
public void exportExcelTest(HttpServletRequest request, HttpServletResponse response) {
Map map = new HashMap(4);
Map itemMap = null;
//商户
List<Map> merchantList = new ArrayList<Map>();
itemMap = new HashMap();
itemMap.put("id", 3);
itemMap.put("merchant_name", "张3");
itemMap.put("market_id", 1);
itemMap.put("market_name", "市场1");
merchantList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 4);
itemMap.put("merchant_name", "张5");
itemMap.put("market_id", 2);
itemMap.put("market_name", "市场2");
merchantList.add(itemMap);
map.put("merchantList", merchantList); //品类
List<Map> bccList = new ArrayList<Map>();
itemMap = new HashMap();
itemMap.put("id", 1);
itemMap.put("category", "苹果");
itemMap.put("sub_category", "红富士苹果");
bccList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 2);
itemMap.put("category", "苹果");
itemMap.put("sub_category", "国光苹果");
bccList.add(itemMap);
map.put("bccList", bccList); //供货商
List<Map> merchantSupplierList = new ArrayList<Map>();
itemMap = new HashMap();
itemMap.put("id", 1);
itemMap.put("supplierName", "李四");
itemMap.put("supplierShopName", "李四水果店");
merchantSupplierList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 2);
itemMap.put("supplierName", "王五");
itemMap.put("supplierShopName", "王五蔬菜店");
merchantSupplierList.add(itemMap);
map.put("merchantSupplierList", merchantSupplierList); //省市区
List<Map> areaList = new ArrayList<Map>();
itemMap = new HashMap();
itemMap.put("sheng_id", 610000);
itemMap.put("sheng_name", "陕西省");
itemMap.put("shi_id", 610100);
itemMap.put("shi_name", "西安市");
itemMap.put("qu_id", 610104);
itemMap.put("qu_name", "莲湖区");
areaList.add(itemMap);
itemMap = new HashMap();
itemMap.put("sheng_id", 610000);
itemMap.put("sheng_name", "陕西省");
itemMap.put("shi_id", 610100);
itemMap.put("shi_name", "西安市");
itemMap.put("qu_id", 610113);
itemMap.put("qu_name", "雁塔区");
areaList.add(itemMap);
map.put("areaList", areaList);
ExcelTemplate.exportExcelByTemplate(request, response, map, "excelTemplate/template.xls");
}
3、导出成功后结果如图:

JAVA 通过POI 模版,导出excel的更多相关文章
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- java使用poi生成导出Excel(新)
导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...
- Java中导入、导出Excel
原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...
- 如何用poi生成导出excel
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Sheet; import java. ...
- 我是陌生人 Java中导入、导出Excel
我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...
- jxls使用模版导出Excel
/** * 使用模版导出Excel */ @SuppressWarnings({ "unchecked", "deprecation" } ...
- POI导入导出excel(附工具类)
关于POI导出excel的功能我在前面的文章已经写过了,POI导出excel的三种方式 , 导出表格数据到excel并下载(HSSFWorkbook版) ,本篇文章主要是将导入导出功能进一步地封装,在 ...
- java的poi技术写Excel的Sheet
在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- POI实现导出Excel和模板导出Excel
一.导出过程 1.用户请求导出 2.先访问数据库,查询需要导出的结果集 3.创建导出的Excel工作簿 4.遍历结果集,写入工作簿 5.将Excel已文件下载的形式回复给请求客户端 二.具体实现(截取 ...
随机推荐
- 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)
Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...
- java基础(十一)之抽象类和抽象函数
1.抽象函数的语法特征2.抽象类的语法特征3.抽象类的作用 抽象函数 只有函数的定义,没有函数体的函数被称为抽象函数: abstract void func(); 抽象类 使用abstract定义的类 ...
- [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅
有一段时间没有更新技术博文了,因为这段时间埋下头来看Vue源码了.本文我们一起通过学习双向绑定原理来分析Vue源码.预计接下来会围绕Vue源码来整理一些文章,如下. 一起来学Vue双向绑定原理-数据劫 ...
- 解决wps for linux缺失windows字体
操作步骤 1.下载缺失字体 链接: https://pan.baidu.com/s/1ZUbtQ96b8RVbH0LrXb_GlQ 密码: nsr4 2.解压字体 unzip wps-font-sy ...
- Codeforces Round #608 (Div. 2)D(贪心)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],b[],c[]; int u,v; ...
- 微信支付之H5支付
HoJe男孩子你要加油阿 前言准备材料H5支付请求的参数返回结果统一下单回调接口用到的工具类886 . 前言 大家好,分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于 ...
- Springmvc-crud-03(静态资源错误)
错误描述:静态资源加载失败 原因:spring会拦截静态资源 解决办法: <!-- 配置spring支持静态资源请求 --> <mvc:default-servlet-handler ...
- git查漏补缺
1. commit提交注释规范 2. commit 注释没写完或写错了,在不用删除这条commit的情况下,如何更正注释信息 git commit -m '1' git commit --amend ...
- aria2连接网站出现handshake failure问题的分析与解决方法
aria2是一款轻量级的,支持多协议,跨平台的命令行下载工具,是笔者目前在使用的下载工具,结合uget使用基本上能媲美window下的迅雷工具.在笔者使用过程中,遇到了aria2连接部分网站时出现ha ...
- tensorflow 学习记录
函数变动 tf.train.SummaryWriter 变为 tf.summary.Filewritter 函数功能相同,仅仅是简单的重命名 ``` writer = tf.summary.FileW ...