JAVA 通过POI 模版,导出excel
2024-09-03 07:23:25
如有不足,欢迎指正,谢谢 !
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、导出成功后结果如图:
最新文章
- redis成长之路——(六)
- ThreaLocal内存泄露的问题
- IP地址及其子网划分
- versionCompare 版本号比较工具
- Java多线程编程核心技术---Java多线程技能
- SQL Server常用语句
- 生产订单修改删除组件BDC
- 安装完openfire之后打不开的解决方案
- userscript.user.js 文件头
- 将[{},{}]转为dict
- Android05-UI02布局,自定义控件,ListView
- Swift ->; Let &; Var 背后编程模式 探讨
- 详解C# Tuple VS ValueTuple(元组类 VS 值元组)
- PCI9054 突发模式数据传输 (burst mode data transfer )
- Discuz 5.x 6.x 7.x 前台SQL注入漏洞
- PHP输出缓存ob系列函数
- {python--GIL锁}一 介绍 二 GIL介绍 三 GIL与Lock 四 GIL与多线程 五 多线程性能测试
- Leetcode——198. 打家劫舍
- 企点微服务网关演进之路 IT大咖说 - 大咖干货,不再错过
- discuz的diy功能介绍
热门文章
- 使用Samba实现文件共享:Windows和Linux之间
- 传奇HERO引擎给装备加套装属性技巧
- 百炼OJ - 1005 - I Think I Need a Houseboat
- 执行ifconfig eth2 up命令报错eth2: unknown interface: No such device的解决思路
- The entity type XXX is not part of the model for the current context.
- jmeter的使用--添加自定义函数和导入自定义jar
- mysql修改密码的4种方式
- 用python实现网络文件共享
- opencv:图像轮廓计算
- AcWing 860. 染色法判定二分图