Spring Boot--POI导出excel文件下载
2017-12-05 22:03
686 查看
1. 依赖
2. 代码
2.1 写入excel
2.2 控制层调用
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.16</version> </dependency>
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
2. 代码
2.1 写入excel
/** * 导出excel * @param list 数据集合 * @param column 列名 * @param templatePath 模板路径 * @param os 输出流 */ public static <T 4000 > void exportExcel(List<T> list, String[] column, String templatePath, OutputStream os) { // 获取列名map Map<String, String> map = XmlParser.getColumnName(templatePath); // 声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); // 声明一个单子并命名 HSSFSheet sheet = wb.createSheet("1"); // 给单子名称一个长度 sheet.setDefaultColumnWidth((short) 15); // 生成一个样式 HSSFCellStyle style = wb.createCellStyle(); // 创建第一行(也可以称为表头) HSSFRow row = sheet.createRow(0); // 样式字体居中 style.setAlignment(HorizontalAlignment.CENTER); // 给表头第一行一次创建单元格 if (column == null || column.length == 0) return ; for (int index = 0; index < column.length; index++) { HSSFCell cell = row.createCell((short) index); // 名称.. cell.setCellValue(map.get(column[index])); cell.setCellStyle(style); } // 将集合转成list // 有可能报错,json序列化死循环 JSONArray jsonarray = JSONArray.fromObject(list); // 向单元格里填充数据 for (short i = 0; i < jsonarray.size(); i++) { row = sheet.createRow(i + 1); JSONObject jsonObject = jsonarray.getJSONObject(i); for (int index = 0; index < column.length; index++) { row.createCell(index).setCellValue(jsonObject.get(column[index]) + ""); } } try { wb.write(os); System.out.println("导出成功"); wb.close(); } catch (Exception e) { e.printStackTrace(); } }
2.2 控制层调用
/** * 下载 * * @throws IOException */ @RequestMapping("/export") public void export(String templatePath, HttpServletResponse response) throws IOException { // 查询所有的数据 List<Student> findAll = studentService.findAll(); String[] str = { "id", "name"};//列名 // 写入文件,得到文件 Date date = new Date(System.currentTimeMillis()); // 转换提日期输出格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String name = dateFormat.format(date) + ".xls"; response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("UTF-8"), "ISO8859-1")); response.setContentType("application/octet-stream"); OutputStream toClient = null; toClient = new BufferedOutputStream(response.getOutputStream()); ExcelUtil.exportExcel(findAll, str, templatePath, response.getOutputStream()); toClient.flush(); response.getOutputStream().close(); }
相关文章推荐
- SpringMVC+POI下载文件模板和导出Excel
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- SpringBoot中使用POI,快速实现Excel导入导出
- Spring Boot 菜鸟教程 12 EasyPoi导出Excel下载
- SpringBoot中使用POI,快速实现Excel导入导出
- POI导出Excel文件,浏览器点击可下载
- Springboot 之 POI导出Word文件
- Spring Boot利用poi导出Excel
- springboot poi 导出excel
- SpringMVC/SpringBoot使用easypoi实现Excel文件导入导出功能实现
- 文件下载利用poi导出excel
- Spring实现的动态文件下载(以Excel导出为例)
- Spring Boot学习笔记----POI(Excel导入导出)
- Spring实现的动态文件下载(以Excel导出为例)
- 【poi excel导出】不同浏览器的编码方式,解决下载文件名称乱码
- 【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器
- POI文件导出至EXCEL,并弹出下载框
- java 利用 poi 生成 Excel文件与spring使用文件流形式下载文件
- springboot+poi导出指定格式Excel模板详解+Demo
- springboot easypoi导出excel