JAVA 通过POI 模版,导出excel

Posted helloearth

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 通过Apache poi导出excel代码demo实例

Poi导出Excel模版设置单元格为下拉框格式

Poi导出Excel模版设置单元格为下拉框格式

求问 java poi 导出多个sheet页

java导出excel后打开出现错误

谁能给我一个详细的Java通过Apache POI导出Excel方法,最好能给完整代码