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已文件下载的形式回复给请求客户端 二.具体实现(截取 ...
随机推荐
- 5G手机来了,但这些问题让其短期内难以成为“香饽饽”
在5G手机喊了太长时间后,其终于在近日不断亮相. 此前据中国质量认证中心官网显示,共有8款5G手机获得3C认证--华为4款,一加.中兴.OPPO和vivo各有一款.随着首批5G手机通过3C认证,意味着 ...
- 下载图片(vue 下载图片)
downloadImg(){ const url = this.imgUrl // window.open(_this.detail.imgUrl) let xmlhttp=new XMLHttpRe ...
- layer.open 回调函数
官方资料:http://www.layui.com/doc/modules/layer.html 在一个弹出框中新增个按钮,点击按钮后执行自己的语句(返回上一页并刷新). layer.open({ti ...
- 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH
参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...
- leetcode 25. K 个一组翻转链表
# coding:utf-8 __author__ = "sn" """ 25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返 ...
- kotori和bangdream
#include<cstdio> #include<iostream> using namespace std; int n,x,a,b; int main() { cin ...
- ALSA driver--HW Buffer
当app在调用snd_pcm_writei时,alsa core将app传来的数据搬到HW buffer(即DMA buffer)中,alsa driver从HW buffer中读取数据传输到硬件播放 ...
- Unity 鼠标旋转物体360展示
PC端 using UnityEngine; using System.Collections; public class DragRound : MonoBehaviour { public Tra ...
- C语言创建共享库(动态库)步骤
C语言创建共享库(动态库)步骤: 创建sth.c,共享库源文件: //sth.c库源程序 unsigned long long sth(unsigned int x, unsigned int y) ...
- django admin后台(数据库简单管理后台)
只需要简单的几行胆码就可以生成一个完整的管理后台 这个就是django魅力之一 创建超级用户 python manage.py createsuperuser ---- 之后会提示输入用慕名 ...