背景: 使用easypoi 导出数据库数据,并以excel存放。
easypoi文档:
http://easypoi.mydoc.io/#text_217704
1、导入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2、编写实体类:
注:必需有构造函数,如果懒得手动添加课添加注解@AllArgsConstructor //全参构造函数、@NoArgsConstructor //无参构造函数。
在实体类如果没有添加@TableName("")注解,程序会自动到数据库中找excel_Entity表。
package io.renren.modules.sys.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import java.util.Date;
//@TableName("")
public class excelEntity {
@Excel(name = "姓名", orderNum = "0")
private String name;
@Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
private String sex;
@Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2")
private Date birthday;
public excelEntity(String name, String sex, Date birthday) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
//this.companyLogo = companyLogo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3、编写service
import com.baomidou.mybatisplus.extension.service.IService;
import io.renren.modules.sys.entity.excelEntity;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Service
public interface ExcelImExportService extends IService<excelEntity> {
String exexport(HttpServletResponse response) throws IOException;
}
4、编写实现类impl
@Service
public class ExcelImExportServiceImpl extends ServiceImpl<excelDao, excelEntity> implements ExcelImExportService {
//导出
@Override
public String exexport(HttpServletResponse response) throws IOException {
//获取数据库信息
//若果你是用的是renren 框架,在这里存在疑惑可参考另一篇博客:
//https://editor.csdn.net/md/?articleId=106044901
List<excelEntity> list1 = this.list(new QueryWrapper<excelEntity>());
//构造数据
// List <excelEntity> list = new ArrayList<>();
// excelEntity ex1 = new excelEntity("路飞","1", new Date());
// excelEntity ex2 = new excelEntity("娜美","2", new Date());
// list.add(ex1);
// list.add(ex2);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"),
excelEntity.class, list1);
try {
// HttpServletResponse response1 = null;
File savefile = new File("E:/excel/");
if (!savefile.exists()) {
savefile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("E:/excel/教师课程学生导出测试1.xls");
workbook.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
4、编写测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class ExcelImExportTest {
@Autowired
private ExcelImExportService excelImExportService;
//简单导出
@Test
public void exexport() throws IOException {
HttpServletResponse response=null;
excelImExportService.exexport(response);
}
5、数据库信息与导出结果
5.1、数据库信息
5.2、导出结果