java poi导出excel模板_JAVA 通过POI 模版,导出excel

该博客介绍了一个JAVA方法,使用POI库和模板来导出Excel文件。通过`excelTempExport`函数,从指定的模板路径加载模板,然后结合结果集`resultMap`动态填充数据,生成新的Excel文件并进行下载。博客还提供了测试用例,展示了如何填充商户、品类、供货商和省市区等数据。
摘要由CSDN通过智能技术生成

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 省略。。。

/***

*@paramrequest

*@paramresponse

*@paramtempPath 模版的相对路径

*@paramresultMap 结果集

*@throwsException*/

public static voidexcelTempExport(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(newDate());//文件名称统一编码格式

newFileName = URLEncoder.encode(newFileName, "utf-8");//生成的导出文件

newFile = File.createTempFile(newFileName, ".xls");//transformer转到Excel

transformer = newXLSTransformer();//将数据添加到模版中生成新的文件

transformer.transformXLS(tempFileName, resultMap, newFile.getAbsolutePath());//将文件输入

InputStream inputStream = newFileInputStream(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= newBufferedInputStream(inputStream);

bos= newBufferedOutputStream(out);byte[] buff = new byte[2048];intbytesRead;//根据读取并写入

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 merchantList = new ArrayList();

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 bccList = new ArrayList();

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 merchantSupplierList = new ArrayList();

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 areaList = new ArrayList();

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值