maven依赖
<!--读取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.cyb.exceldemo.excel;
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 ExcelController {
@RequestMapping(value = "/excel2007Export")
public ResponseEntity<Resource> excel2007Export(HttpServletResponse response, HttpServletRequest request)
{
try
{
ClassPathResource cpr = new ClassPathResource("/static/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();
}
}
}
注意需要有Excel模板
![](https://img2020.cnblogs.com/blog/1504448/202007/1504448-20200723111636150-1412751516.png)