EasyExcelUtils含调用

package com.springboot.ssm.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
* Excel工具类
*/
@Slf4j
public class ExcelUtils {

/**
* 导出Excel(07版.xlsx)到指定路径下
*
* @param path 路径
* @param excelName Excel名称
* @param sheetName sheet页名称
* @param clazz Excel要转换的类型
* @param data 要导出的数据
*/
public static void export2File(String path, String excelName, String sheetName, Class clazz, List data) {
String fileName = path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(fileName, clazz).sheet(sheetName).doWrite(data);
}

/**
* 导出Excel(07版.xlsx)到web
*
* @param response 响应
* @param excelName Excel名称
* @param sheetName sheet页名称
* @param clazz Excel要转换的类型
* @param data 要导出的数据
* @throws Exception
*/
public static void export2Web(HttpServletResponse response, String excelName, String sheetName, Class clazz, List data) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
excelName = URLEncoder.encode(excelName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
}

/**
* 将指定位置指定名称的Excel导出到web
*
* @param response 响应
* @param path 文件路径
* @param excelName 文件名称
* @return
* @throws UnsupportedEncodingException
*/
public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
File file = new File(path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue()));
if (!file.exists()) {
return "文件不存在!";
}

response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
excelName = URLEncoder.encode(excelName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());

try (
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
) {
IOUtils.copy(in, out);
return "导出成功!";
} catch (Exception e) {
log.error("导出文件异常:", e);
}

return "导出失败!";
}

}


// easyexcel导出Excel到指定位置
@GetMapping("/export2File")
@ResponseBody
public String export2File() {
ExcelUtils.export2File(path, "用户表", "用户信息", User.class, userService.getAll());
return "导出成功";
}

// easyexcel导出Excel到web
@GetMapping("/export2Web")
public void export2Web(HttpServletResponse response) {
try {
ExcelUtils.export2Web(response, "用户表", "用户信息", User.class, userService.getAll());
} catch (Exception e) {
log.error("报表导出异常:", e);
}
}

// 将指定位置指定名称的Excel导出到web
@GetMapping("/export2Web4File/{excelName}")
@ResponseBody
public String export2Web4File(HttpServletResponse response, @PathVariable String excelName) {
try {
return ExcelUtils.export2Web4File(response, path, excelName);
} catch (Exception e) {
log.error("文件导出异常:", e);
}

return "文件导出失败";
}

// easyexcel读取文件
@GetMapping("/read4File")
@ResponseBody
public String read4File() {
String fileName = path + "用户表导入.xlsx";
EasyExcel.read(fileName, User.class, new UserDataListener(userService)).sheet().doRead();
return "读取成功";
}

// easyexcel上传文件
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), User.class, new UserDataListener(userService)).sheet().doRead();
return "上传成功";
}

非本人著作
原文地址:https://www.cnblogs.com/austinspark-jessylu/p/15207121.html