說明
導入和導出這里有四種方案,具體寫三種。
1、apache 中poi 的使用
2、easypoi 的使用
3、alibaba 的 easyexcel
4、hutool 工具中
apache - poi 介紹
官方主頁: http://poi.apache.org/index.html
文檔: http://poi.apache.org/apidocs/index.html
常用的api 類
HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能。
XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能。
HWPF - 提供讀寫Microsoft Word DOC97格式檔案的功能。
XWPF - 提供讀寫Microsoft Word DOC2003格式檔案的功能。
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。
HDGF - 提供讀Microsoft Visio格式檔案的功能。
HPBF - 提供讀Microsoft Publisher格式檔案的功能。
HSMF - 提供讀Microsoft Outlook格式檔案的功能。
文檔前 四個步驟就可以的

我只寫了一個demo 導入和導出功能。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
utils 方法
public class ExcelUtils {
/**
* 導出Excel
*
* @param sheetName sheet名稱
* @param title 標題
* @param values 內(nèi)容
* @param wb HSSFWorkbook對象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,創(chuàng)建一個HSSFWorkbook,對應一個Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數(shù)列數(shù)有限制
HSSFRow row = sheet.createRow(0);
// 第四步,創(chuàng)建單元格,并設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 創(chuàng)建一個居中格式
//聲明列對象
HSSFCell cell = null;
//創(chuàng)建標題
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//創(chuàng)建內(nèi)容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
//將內(nèi)容按順序賦給對應的列對象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
剩下就是命名和數(shù)據(jù)操作了。
讀取方法也和HSSFWorkbook 有關(guān)系。他既可以寫也可以讀
@RestController
public class TestController {
/**
* 導出報表
*
* @return
*/
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletResponse response) {
//獲取數(shù)據(jù)
List<PageData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
PageData pageData = new PageData();
pageData.setAge(i);
pageData.setName("姓名" + i);
pageData.setSex(i);
pageData.setClassName("班級" + i + "版本");
pageData.setSchool("學校" + i);
list.add(pageData);
}
//excel標題
String[] title = {"名稱", "性別", "年齡", "學校", "班級"};
//excel文件名
String fileName = "學生信息表" + System.currentTimeMillis() + ".xls";
//sheet名
String sheetName = "學生信息表";
String[][] content = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
PageData obj = list.get(i);
content[i][0] = obj.getName();
content[i][1] = "" + obj.getSex();
content[i][2] = "" + obj.getAge();
content[i][3] = obj.getSchool();
content[i][4] = obj.getClassName();
}
//創(chuàng)建HSSFWorkbook
HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);
//響應到客戶端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//發(fā)送響應流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("resource")
@RequestMapping("/import")
public Object importExcel(@RequestParam("file") MultipartFile file) throws Exception {
InputStream inputStream = file.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
List<PageData> list = new ArrayList<>();
for (int i = 1; i <= lastRowNum; i++) {
HSSFRow row = sheet.getRow(i);
String name = row.getCell(0).getStringCellValue();
int sex = Integer.parseInt(row.getCell(1).getStringCellValue());
int age = Integer.parseInt(row.getCell(2).getStringCellValue());
String school = row.getCell(3).getStringCellValue();
String className = row.getCell(4).getStringCellValue();
PageData pageData = new PageData();
pageData.setSchool(school);
pageData.setClassName(className);
pageData.setSex(sex);
pageData.setName(name);
pageData.setAge(age);
list.add(pageData);
System.out.println(name + "-" + sex + "-" + age + "-" + school + "-" + className);
}
return list;
}
}
多取 多行的時候,需要從第二行開始的

更多功能可以去挖掘。

easypoi 介紹

<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.2</version>
</dependency>
他demo 項目是獨立的
https://gitee.com/lemur/easypoi-test
里面還有報錯,用dev 就可以,master 去掉錯誤注釋也可以用。
好處就是導入和導出都有utils

我的demo
package com.spring.easypoi.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.io.FileUtil;
import com.spring.easypoi.domain.UserDTO;
import com.spring.easypoi.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
public class PoiController {
/**
* 導出表格 - 最基礎(chǔ)的
*
* @param response
*/
@GetMapping("/export")
public void export(HttpServletResponse response) {
List<UserDTO> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
UserDTO client = new UserDTO();
client.setUsername("小明" + i);
client.setBirthday("18797" + i);
client.setAge("1" + i);
list.add(client);
}
ExportParams params = new ExportParams();
params.setSheetName("測試");
params.setType(ExcelType.XSSF);
// params.setFreezeCol(2);// 表頭是否固定
Workbook workbook = ExcelExportUtil.exportExcel(params, UserDTO.class, list);
String now = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
String fileName = String.format("%s.xlsx", now);
ExcelUtils.downLoadExcel(fileName, response, workbook);
}
@PostMapping("/import")
public Object importXlsx(@RequestParam MultipartFile file) {
/*
* 服務器地址
*/
String property = System.getProperty("user.dir");
String OriginalFilename = file.getOriginalFilename();//獲取原文件名
String suffixName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));//獲取文件后綴名
//重新隨機生成名字
String filename = UUID.randomUUID().toString().replace("-", "") + suffixName;
File localFile = new File(property + "\\" + filename);
try {
/*
* 上傳方式1
* 把上傳的文件保存至本地
*/
// file.transferTo(localFile);
/*
* 上傳方式2
* 把上傳的文件保存至本地
*/
FileUtil.writeBytes(file.getBytes(), localFile.getAbsoluteFile());
ImportParams params = new ImportParams();
List<Map<String, Object>> list = ExcelImportUtil.importExcel(localFile, Map.class, params);
for (Map map : list) {
System.out.println(map.get("姓名"));
System.out.println(map.get("年齡"));
System.out.println(map.get("生日"));
}
/*
* 刪除本地緩存數(shù)據(jù)
*/
localFile.delete();
return list;
} catch (IOException e) {
e.printStackTrace();
System.out.println("上傳失敗");
return "" + e.getMessage();
}
}
}
多寫了一個
/**
* 下載表格
*
* @param fileName
* @param response
* @param workbook
*/
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.getMessage();
}
}
他的注解也不少

Map導入,自由發(fā)揮
全部內(nèi)容
http://easypoi.mydoc.io/#text_202983
http://doc.wupaas.com/docs/easypoi/easypoi-1c0u4mo8p4ro8
easyexcel 介紹
EasyExcel是一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel。 github地址:https://github.com/alibaba/easyexcel
最簡單的寫
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
/**
* 最簡單的寫
* <p>1. 創(chuàng)建excel對應的實體對象 參照{(diào)@link com.alibaba.easyexcel.test.demo.write.DemoData}
* <p>2. 直接寫即可
*/
@Test
public void simpleWrite() {
String fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx";
// 這里 需要指定寫用哪個class去讀,然后寫到第一個sheet,名字為模板 然后文件流會自動關(guān)閉
// 如果這里想使用03 則 傳入excelType參數(shù)即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
}
最簡單的讀
/**
* 指定列的下標或者列名
*
* <p>1. 創(chuàng)建excel對應的實體對象,并使用{@link ExcelProperty}注解. 參照{(diào)@link IndexOrNameData}
* <p>2. 由于默認一行行的讀取excel,所以需要創(chuàng)建excel一行一行的回調(diào)監(jiān)聽器,參照{(diào)@link IndexOrNameDataListener}
* <p>3. 直接讀即可
*/
@Test
public void indexOrNameRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 這里默認讀取第一個sheet
EasyExcel.read(fileName, IndexOrNameData.class, new IndexOrNameDataListener()).sheet().doRead();
}
這里IndexOrNameDataListerner()這個是自定義,里面的方法如下
public class IndexOrNameDataListener implements ReadListener<UserInfo> {
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
/*讀取異常*/
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
// 獲取excel的第一行head數(shù)據(jù)
}
@Override
public void invoke(UserInfo userInfo, AnalysisContext analysisContext) {
// 讀取正文數(shù)據(jù),一次只讀取一行
}
@Override
public void extra(CellExtra extra, AnalysisContext context) {
// 額外單元格返回的數(shù)據(jù),這個方法還沒詳細了解過,一直沒用到過
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 所有數(shù)據(jù)讀取完了
}
@Override
public boolean hasNext(AnalysisContext context) {
// 是否讀取下一行
return true;
}
}
我的demo
@RestController
public class TestController {
@GetMapping("/export")
public Object index(HttpServletResponse response) {
List<UserInfo> list = new ArrayList<>();
for (int i = 1; i < 10; i++) {
UserInfo userInfo = new UserInfo();
userInfo.setId(i);
userInfo.setUsername("姓名" + i);
userInfo.setPassword("123456");
userInfo.setAddress("地址" + i);
userInfo.setPhone("電話" + i);
list.add(userInfo);
}
try (ServletOutputStream out = response.getOutputStream()) {
String disposition = "attachment; fileName=" +
new String("用戶信息".getBytes("UTF-8"), "ISO-8859-1") + ".xls";
response.setCharacterEncoding("utf8");
response.setContentType("text/xls");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("用戶信息表格" + ".xls", "UTF-8"));
response.setHeader("Content-Disposition", disposition);
EasyExcel.write(out, UserInfo.class).sheet("用戶信息").doWrite(list);
out.flush();
} catch (IOException e) {
throw new RuntimeException();
}
return "導出成功";
}
/**
* 2、EasyExcel excel導入(百萬級數(shù)據(jù)測試)
* @return
*/
@PostMapping("/submitExcel")
public void submitExcel(MultipartFile file){
//創(chuàng)建reader
ExcelReader excelReader = null;
try {
excelReader = EasyExcel.read(file.getInputStream(), UserInfo.class, new ExcelUpload()).build();
// 構(gòu)建sheet,可以指定是第幾個sheet
ReadSheet readSheet = EasyExcel.readSheet(0).build();
// 讀取sheet
excelReader.read(readSheet);
}catch (Exception e){
e.printStackTrace();
}finally {
if (excelReader != null) {
//這里不能省略
excelReader.finish();
}
}
}
}
還有注解 說明

這個沒有測試

Hutool-poi 介紹
Hutool-poi是針對Apache POI的封裝,因此需要用戶自行引入POI庫,Hutool默認不引入。到目前為止,Hutool-poi支持:
Excel文件(xls, xlsx)的讀?。‥xcelReader)
Excel文件(xls,xlsx)的寫出(ExcelWriter)
主要是對poi 進行封裝,也是一個不錯的框架
我的demo
@GetMapping("/export")
public void export(HttpServletResponse response) {
//默認創(chuàng)建xls格式的(getWriter(true)則創(chuàng)建xlsx格式)
ExcelWriter writer = ExcelUtil.getWriter(true);
writer.addHeaderAlias("username", "姓名");
writer.addHeaderAlias("age", "年齡");
writer.addHeaderAlias("birthday", "生日");
List<UserDTO> rows = getDataList();
// 一次性寫出內(nèi)容,使用默認樣式
writer.write(rows, true);
//response為HttpServletResponse對象
response.setContentType("application/vnd.ms-excel");
ServletOutputStream out = null;
//文件名格式:文件+當前時間,如文件20220617094900
String rawFileName = "文件" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
try {
//彈出下載對話框的文件名不能為中文,中文請自行編碼
//P.S 這里可以中文命名,但是只能通過URL請求,通過swagger或postman請求還是會亂碼,若要測試可以通過Get請求
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(rawFileName, "UTF-8") + ".xlsx");
out = response.getOutputStream();
writer.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關(guān)閉writer,釋放內(nèi)存
writer.close();
//此處記得關(guān)閉輸出Servlet流
IoUtil.close(out);
}
}
public List getDataList() {
List<UserDTO> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
UserDTO client = new UserDTO();
client.setUsername("小明" + i);
client.setBirthday("18797" + i);
client.setAge("1" + i);
list.add(client);
}
return list;
}
詳細可以看 https://www.hutool.cn/docs/#/poi/%E6%A6%82%E8%BF%B0
總結(jié)
總上,都是api 的使用,看文檔寫demo 基本需求都能寫出來的。整理和收集就到這里了。感覺
easyexcel 好一點。不過其他都很優(yōu)秀的。