项目用的是springboot框架,非常的简单,只需导入项目即可运行,先看看效果:

资源下载地址:

http://download.csdn.net/download/tjcyjd/9998721

具体步骤如下:

1、引入依赖jar包。

在pom.xml中引入两个依赖的包即可:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version>
</dependency>

2、把需要导出的Excel模板放在webapp下的excel文件夹中。

如下目录,建立两个Excel一个2003的,一个2007的,user_model.xls、user_model.xlsx。

其中Excel需要导出的内容格式如下:

3、导出代码实现。

废话少说,直接上代码,拿来就用。

导出Excel2003:

 @RequestMapping(value = "/export2003.do", method = RequestMethod.GET)public void export2003(HttpServletRequest request, HttpServletResponse response) {List<User> list = new ArrayList<User>();User user1 = new User();user1.setId(100000000L);user1.setUsername("张三");user1.setHead("http://qzapp.qlogo.cn/qzapp/101357640/3C94155CAB4E28517D8435BF404B52F1/100");user1.setSex(0);user1.setPhone("18800000000");User user2 = new User();user2.setId(100000001L);user2.setUsername("李四");user2.setHead("http://q.qlogo.cn/qqapp/1105676675/9DA6D356F4FE1DF0E63BD07334680BF2/100");user2.setSex(0);user2.setPhone("18800000001");list.add(user1);list.add(user2);HSSFWorkbook wb = null;try {// excel模板路径String basePath = request.getSession().getServletContext().getRealPath("/");String excel = basePath + "/excel/user_model.xls";File fi = new File(excel);POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));// 读取excel模板wb = new HSSFWorkbook(fs);// 读取了模板内所有sheet内容HSSFSheet sheet = wb.getSheetAt(0);// 在相应的单元格进行赋值int rowIndex = 1;for (User user : list) {HSSFRow row = sheet.getRow(rowIndex);if (null == row) {row = sheet.createRow(rowIndex);}HSSFCell cell0 = row.getCell(0);if (null == cell0) {cell0 = row.createCell(0);}cell0.setCellValue(user.getId());// 标识HSSFCell cell1 = row.getCell(1);if (null == cell1) {cell1 = row.createCell(1);}cell1.setCellValue(user.getUsername());// 用户名HSSFCell cell2 = row.getCell(2);if (null == cell2) {cell2 = row.createCell(2);}cell2.setCellValue(user.getHead());// 头像HSSFCell cell3 = row.getCell(3);if (null == cell3) {cell3 = row.createCell(3);}cell3.setCellValue(user.getSex() == 0 ? "女" : "男");// 性别HSSFCell cell4 = row.getCell(4);if (null == cell4) {cell4 = row.createCell(4);}cell4.setCellValue(user.getPhone());// 手机rowIndex++;}String fileName = "用户信息";ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);byte[] content = os.toByteArray();InputStream is = new ByteArrayInputStream(content);// 设置response参数,可以打开下载页面response.reset();response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));ServletOutputStream sout = response.getOutputStream();BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(is);bos = new BufferedOutputStream(sout);byte[] buff = new byte[2048];int bytesRead;// Simple read/write loop.while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {bos.write(buff, 0, bytesRead);}} catch (Exception e) {logger.error("导出excel出现异常:", e);} finally {if (bis != null)bis.close();if (bos != null)bos.close();}} catch (Exception e) {logger.error("导出excel出现异常:", e);}}

导出Excel2007:

 @RequestMapping(value = "/export2007.do", method = RequestMethod.GET)public void export2007(HttpServletRequest request, HttpServletResponse response) {List<User> list = new ArrayList<User>();User user1 = new User();user1.setId(100000000L);user1.setUsername("张三");user1.setHead("http://qzapp.qlogo.cn/qzapp/101357640/3C94155CAB4E28517D8435BF404B52F1/100");user1.setSex(0);user1.setPhone("18800000000");User user2 = new User();user2.setId(100000001L);user2.setUsername("李四");user2.setHead("http://q.qlogo.cn/qqapp/1105676675/9DA6D356F4FE1DF0E63BD07334680BF2/100");user2.setSex(0);user2.setPhone("18800000001");list.add(user1);list.add(user2);XSSFWorkbook wb = null;try {// excel模板路径String basePath = request.getSession().getServletContext().getRealPath("/");String excel = basePath + "/excel/user_model.xlsx";File fi = new File(excel);// 读取excel模板wb = new XSSFWorkbook(new FileInputStream(fi));// 读取了模板内所有sheet内容XSSFSheet sheet = wb.getSheetAt(0);// 在相应的单元格进行赋值int rowIndex = 1;int j = 1;for (User user : list) {XSSFRow row = sheet.getRow(rowIndex);if (null == row) {row = sheet.createRow(rowIndex);}XSSFCell cell0 = row.getCell(0);if (null == cell0) {cell0 = row.createCell(0);}cell0.setCellValue(user.getId());// 标识XSSFCell cell1 = row.getCell(1);if (null == cell1) {cell1 = row.createCell(1);}cell1.setCellValue(user.getUsername());// 用户名XSSFCell cell2 = row.getCell(2);if (null == cell2) {cell2 = row.createCell(2);}cell2.setCellValue(user.getHead());// 头像XSSFCell cell3 = row.getCell(3);if (null == cell3) {cell3 = row.createCell(3);}cell3.setCellValue(user.getSex() == 0 ? "女" : "男");// 性别XSSFCell cell4 = row.getCell(4);if (null == cell4) {cell4 = row.createCell(4);}cell4.setCellValue(user.getPhone());// 手机rowIndex++;}String fileName = "用户信息";ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);byte[] content = os.toByteArray();InputStream is = new ByteArrayInputStream(content);// 设置response参数,可以打开下载页面response.reset();response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));ServletOutputStream sout = response.getOutputStream();BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(is);bos = new BufferedOutputStream(sout);byte[] buff = new byte[2048];int bytesRead;// Simple read/write loop.while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {bos.write(buff, 0, bytesRead);}} catch (Exception e) {logger.error("导出excel出现异常:", e);} finally {if (bis != null)bis.close();if (bos != null)bos.close();}} catch (Exception e) {logger.error("导出excel出现异常:", e);}}

导出的结果如下:

导出成功!

资源下载地址:

http://download.csdn.net/download/tjcyjd/9998721

springboot+poi导出指定格式Excel模板详解+Demo相关推荐

  1. java 将网页表格导出_Java导出网页表格Excel过程详解

    将网页中的table数据,导出到excel表格,可以使用java POI实现. java poi是java中操作excel的工具,支持excel的导入与导出,一般有三种形式: 1.HSSFWorkbo ...

  2. 教育行业疫情分析研判报告撰写格式与模板详解

    要写好一份每日疫情研判报告的前提是需要做好大量的数据梳理分析.所以小编就来为各位分享一些疫情舆情数据快速整理分析的方法以及提供了一份教育疫情舆情分析报告格式模板,供各位参考. 疫情舆情数据快速整理分析 ...

  3. Easypoi使用模板导出文档或excel表格详解

    Easypoi使用模板导出docx文档或excel表格详解 **doc或docx文档的模板导出** **Excel的模板导出** 话不多说先上依赖 <dependency><grou ...

  4. Java使用poi导出数据到excel(包括xls和xlsx两种格式)并通过浏览器下载

    情景:将数据导出到excel是java开发常用的功能,数据量不大的时候,xls和xlsx两种格式的文件都行,但是数据量太大的时候就有区别了,xls格式的文件一个sheet页最多只能存六万多条数据,而x ...

  5. SpringBoot Poi导出word,浏览器下载

    文章目录 SpringBoot Poi导出word,浏览器下载 1.引依赖: 2.写代码(生成本地word): 3.返回给浏览器下载 1.如何返回给浏览器让它下载 4.解决方案 5.为什么没使用eas ...

  6. java使用POI导出图片到Excel

    个人学习记录 目录 个人学习记录 1. 使用POI导出图片到Excel中,Excel格式为xls 2. 使用POI导出图片到Excel中,Excel格式为xlsx,图片设置边距 3. 获取图片,生成B ...

  7. springboot+poi导出百万级数据避免OOM内存溢出

    springboot+poi导出百万级数据避免OOM内存溢出 文章目录 springboot+poi导出百万级数据避免OOM内存溢出 前言 一.具体实现 二.代码实现 1.引入poi包 2.功能代码 ...

  8. php导出csv_原生PHP实现导出csv格式Excel文件的方法示例【附源码下载】

    本文实例讲述了原生PHP实现导出csv格式Excel文件的方法.分享给大家供大家参考,具体如下: 效果图 源码分析 index.php require_once "./Export.php& ...

  9. vb.net 导出固定格式EXCEL

    客户要求,导出固定格式EXCEL,牵涉到office.excel类的相关操作,记录下: 1)引用Microsoft.Office.Interop.Excel和Microsoft.Office.Inte ...

最新文章

  1. 回归分析评估指标均方对数误差(MSLE)详解及其意义:Mean Squared Log Error
  2. C# 笔记 获取程序当前目录
  3. Zerodium以100万美元求Tor浏览器0day漏洞以转售给政府
  4. Java顶尖程序员需要看的书
  5. HTML表格属性跨列,HTML表格的使用 与 跨行跨列
  6. 计算机二级access上机题,2017历年全国计算机二级access上机试题及答案
  7. OpenCV-图像处理(07、绘制形状与文字)
  8. Windows下jmeter安装
  9. docker上安装多个mysql_docker:安装mysql多个
  10. 电荷泵负电源芯片SGM3207
  11. CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)...
  12. 网站维护怎么做?网站维护内容有哪些
  13. QT Creator 7 如何同时运行多个客户端窗口(Client)
  14. ElementUI表格中显示图片,悬浮显示放大图片
  15. 最大斑块指数怎么算_教你一个看血管硬化指数的精准公式!
  16. canvas制作钟表
  17. Cognos问题解决
  18. vue 微信公众号 使用weixin-java-mp、JSSDK自定义分享
  19. linux执行lsof命令_Linux lsof命令使用详解
  20. JavaScript DOM高级程序设计

热门文章

  1. tp3.2中微信支付
  2. CPU架构有多少种?X86与ARM有哪些不同之处?看完这篇你就懂了
  3. FISCO BCOS中交易池及其优化策略
  4. RewriteCond和13个mod_rewrite应用举例Apache伪静态之htaccess编写
  5. 技术名词:Q-in-Q
  6. 这里是离科学最近的地方——白春礼在国科大首届开学典礼上的致辞
  7. merkle-hellman背包算法 java_浅析几种公钥密码体制-RSA;Merkle-Hellman、背包加密体制、ECC优缺点...
  8. MongDB基础学习(一)
  9. matlab halcon,Halcon-Matlab 图像匹配
  10. Android自定义表情功能的实现