java 根据excel模板导出文件
Posted 漫步CODE人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 根据excel模板导出文件相关的知识,希望对你有一定的参考价值。
<!--读取excel文件,配置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>
package com.example.demo; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.core.io.ClassPathResource; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/index") public class IndexController { @RequestMapping(value = "/excel2007Export") public ResponseEntity<Resource> excel2007Export(HttpServletResponse response, HttpServletRequest request) { try { ClassPathResource cpr = new ClassPathResource("/student.xlsx"); InputStream is = cpr.getInputStream(); Workbook workbook = new XSSFWorkbook(is); org.apache.poi.ss.usermodel.Sheet sheet0 = workbook.getSheetAt(0); // 这里作为演示,造几个演示数据,模拟数据库里查数据 List<String> list = new ArrayList<String>(); list.add("张三"); list.add("张四"); list.add("王五"); for (int i = 0; i < list.size(); i++) { Row row = sheet0.createRow(i + 5); row.createCell(0).setCellValue(i); row.createCell(1).setCellValue(list.get(i)); } String fileName = "eeeee.xlsx"; downLoadExcel(fileName, response, workbook); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return new ResponseEntity<Resource>(HttpStatus.OK); } public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename="" + URLEncoder.encode(fileName, "UTF-8") + """); workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping(value = "/exportExcel", method = { RequestMethod.GET, RequestMethod.POST }) public void exportExcel(HttpServletResponse response) throws UnsupportedEncodingException { String fileName = "导出excel.xls"; response.setContentType("application/excel"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";filename*=utf-8‘‘" + URLEncoder.encode(fileName, "UTF-8")); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); int rowNum = 0; // 添加标题 String[] headers = { "标题", "版块", "作者", "创建时间", "内容" }; HSSFRow row = sheet.createRow(rowNum); List<String> list = new ArrayList<String>(); list.add("111"); list.add("222"); list.add("333"); for (String text : list) { HSSFRow row1 = sheet.createRow(rowNum); row1.createCell(0).setCellValue(rowNum); row1.createCell(1).setCellValue(text); rowNum++; } try { workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
以上是关于java 根据excel模板导出文件的主要内容,如果未能解决你的问题,请参考以下文章
如何让java利用POI导出excel表,并在Excel表中根据表格的数据生成柱形图。要求柱形图是动态的。
急!!!java用poi导出excel文件,打开导出的文件时报错“文件错误,数据可能丢失”