您可以下载https://github.com/limiaogithub/yt_mybatis_example 直接运行示例代码。
1.基于apache poi
2.注解的方式进行配置
3.支持多sheet
4.使用简单
1.idea开发工具
2.大约10分钟
1.引入maven依赖
<dependency> <groupId>com.github.limiaogithub</groupId> <artifactId>yt_excel</artifactId> <version>1.0.0</version> </dependency>
2.编写你的Pojo类,这里@ImportExcel指导入的字段,@ExportExcel指导出的字段,属性一看就懂了。
public class TestBean {
private int id;
@ExportExcel(title = "姓名", order = 1)
@ImportExcel(order = 1)
private String name;
@ExportExcel(title = "密码", order = 2)
@ImportExcel(order = 2)
private int password;
@ExportExcel(title = "日期", order = 3)
@ImportExcel(order = 3)
private Date date;
@ExportExcel(title = "BigDecimal", order = 4)
@ImportExcel(order = 4)
private BigDecimal big;
public TestBean() {
}
public TestBean(String name, int password, Date date, BigDecimal big) {
this.name = name;
this.password = password;
this.date = date;
this.big = big;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public BigDecimal getBig() {
return big;
}
public void setBig(BigDecimal big) {
this.big = big;
}
@Override
public String toString() {
return "TestBean{" +
"id=" + id +
", name='" + name + '\'' +
", password=" + password +
", date=" + date +
", big=" + big +
'}';
}
}
3.导出
@ApiOperation(value = "export")
@RequestMapping(value = "/export", method = RequestMethod.GET)
public void export() throws Exception {
ServletOutputStream out = response.getOutputStream();
String fileName = EncodeUtils.toUtf8String(request.getHeader("User-Agent"), "导出文件.xls");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentType("application/octet-stream; charset=utf-8");
ExcelUtils.createExcel(out, TestBean.class, getTestList());
out.flush();
}
private static List getTestList() {
List list = new ArrayList<>();
TestBean testBean1 = new TestBean("name1", 123, new Date(), new BigDecimal(1111));
list.add(testBean1);
TestBean testBean2 = new TestBean("name2", 456, new Date(), new BigDecimal(2222));
list.add(testBean2);
return list;
}
4.导入
@ApiOperation(value = "import")
@RequestMapping(value = "/import", method = RequestMethod.GET)
public void import1(MultipartFile file) throws Exception {
File file1=new File("D:\\导出文件.xls");
List list = ExcelUtils.readExcel(file1, TestBean.class, new ExcelConfig().setStartRow(2));
System.out.println(list.size());
for (TestBean testBean : list) {
System.out.println(testBean.toString());
}
}