cn.afterturn
easypoi-base
3.0.3
cn.afterturn
easypoi-web
3.0.3
cn.afterturn
easypoi-annotation
3.0.3
commons-fileupload
commons-fileupload
1.3.1
package com.zzf.finals.utiles;
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 org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
public class ExcelUtiles {
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass,
String fileName, boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass,String fileName,
HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List
package com.zzf.finals.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "seckill")
public class DemoExcel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Excel(name = "id" ,orderNum = "0")
private Long seckillId;
@Column(name = "name")
@Excel(name = "姓名" ,orderNum = "1")
private String name;
@Column(name = "number")
@Excel(name = "数量" ,orderNum = "2")
private int number;
@Column(name = "start_time")
@Excel(name = "开始日期" ,orderNum = "3",importFormat = "yyyy-MM-dd HH:mm:ss")//exportFormat = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@Column(name = "end_time")
@Excel(name = "结束日期" ,orderNum = "4",importFormat = "yyyy-MM-dd HH:mm:ss")//exportFormat = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
@Column(name = "create_time")
@Excel(name = "创建日期" ,orderNum = "5",importFormat = "yyyy-MM-dd HH:mm:ss")//exportFormat = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
public Long getSeckillId() {
return seckillId;
}
public void setSeckillId(Long seckillId) {
this.seckillId = seckillId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "DemoExcel{" +
"seckillId=" + seckillId +
", name='" + name + '\'' +
", number=" + number +
", startTime=" + startTime +
", endTime=" + endTime +
", createTime=" + createTime +
'}';
}
}
package com.zzf.finals.controller;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.zzf.finals.entity.DemoExcel;
import com.zzf.finals.repository.DemoExcelRepository;
import com.zzf.finals.service.DemoService;
import com.zzf.finals.utiles.ExcelUtiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.List;
import java.util.Map;
@RestController
@RequestMapping("/Excel")
public class ExcelController {
@Autowired
private DemoExcelRepository demoExcelRepository;
@Autowired
private DemoService demoService;
@GetMapping("/export")
public void export(HttpServletResponse response) {
System.out.println(1);
// 模拟从数据库获取需要导出的数据
List personList = demoExcelRepository.findAll();
// 导出操作
ExcelUtiles.exportExcel(personList, "测试名", "什么名字", DemoExcel.class, "测试.xls", response);
}
@PostMapping("/importExcel2")
public void importExcel2(@RequestParam("file") MultipartFile file) {
ImportParams importParams = new ImportParams();
// 数据处理
importParams.setHeadRows(1);
importParams.setTitleRows(1);
// 需要验证
importParams.setNeedVerfiy(true);
try {
ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), DemoExcel.class,
importParams);
List successList = result.getList();
for (DemoExcel demoExcel : successList) {
System.out.println(demoExcel);
}
} catch (IOException e) {
} catch (Exception e) {
}
}
}
输入连接后如图所示。
导入后如:
最最最简单的导入导出就这么完成了。
参考博客:https://blog.csdn.net/qq_37598011/article/details/80918565?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param