实体,每个实体一样,加注解就行。
/**
* @author lnxu
* @date 2021/12/2 19:13
* 就餐次数统计:
*/
@Data
@ExcelTarget("mealTimesByCount")
public class MealTimesByCount {
@Excel(name = "部门", width = 20,orderNum = "0")
private String dept ;
@Excel(name = "姓名", width = 20,orderNum = "1")
private String name ;
@Excel(name = "总数", width = 20,orderNum = "2")
private String count ;
}
pom.xml添加
<!--excel表格导入导出-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
调用方式
package com.tendyron.acs.adaptor.oa.controller.accessExcel;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesByCount;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesByDay;
import com.tendyron.acs.adaptor.oa.controller.accessExcel.resultDto.MealTimesDetail;
import com.tendyron.acs.adaptor.oa.dao.tdr.repository.AccessLogEntityRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author lnxu
* @date 2021/12/2 19:13
* 用餐统计
*/
@Slf4j
@RestController
@RequestMapping("/excel/")
public class AccessExcelController {
@Resource
AccessLogEntityRepository accessLogEntityRepository;
@Value("${excel.path}")
private String excelPath;
@Value("${server.port}")
private String port;
public String getDateTimeFormatter(LocalDateTime time){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return dateTimeFormatter.format(time);
}
public String getStartDateTime(){
LocalDateTime now = LocalDateTime.now().plusDays(-10L);
return getDateTimeFormatter(LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
}
public String getEndDateTime(){
LocalDateTime now = LocalDateTime.now().plusDays(-10L);
return getDateTimeFormatter(LocalDateTime.of(LocalDate.from(now.with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX));
}
public List<MealTimesByCount> getCount(List<Map<String,Object>> mealTimesByCountMap){
List<MealTimesByCount> list = new ArrayList<>();
mealTimesByCountMap.stream().forEach(m -> {
MealTimesByCount mealTimesByCount= new MealTimesByCount();
mealTimesByCount.setCount(m.get("count").toString());
mealTimesByCount.setDept(m.get("dept").toString());
mealTimesByCount.setName(m.get("name").toString());
list.add(mealTimesByCount);
});
return list;
}
public List<MealTimesByDay> getDay(List<Map<String,Object>> mealTimesByDayMap){
List<MealTimesByDay> mealTimesByDays = mealTimesByDayMap.stream().map(m -> {
MealTimesByDay mealTimesByDay = new MealTimesByDay();
mealTimesByDay.setDay(LocalDate.parse(m.getOrDefault("day", "").toString()));
mealTimesByDay.setCount(m.getOrDefault("count", "").toString());
return mealTimesByDay;
}).collect(Collectors.toList());
return mealTimesByDays;
}
public List<MealTimesDetail> getDetail(List<Map<String,Object>> mealTimesDetailMap){
List<MealTimesDetail> mealTimesDetailList = mealTimesDetailMap.stream().map(m -> {
MealTimesDetail mealTimesDetail = new MealTimesDetail();
mealTimesDetail.setDept(m.getOrDefault("dept", "").toString());
mealTimesDetail.setName(m.getOrDefault("name", "").toString());
mealTimesDetail.setOpenTime(m.getOrDefault("openTime", "").toString().substring(0,19));
mealTimesDetail.setStaffNo(m.getOrDefault("staffNo", "").toString());
return mealTimesDetail;
}).collect(Collectors.toList());
return mealTimesDetailList;
}
public String getLastMonth(String title){
LocalDate localDate = LocalDate.now().plusDays(-10L);
return localDate.toString().substring(0,7)+'月'+title;
}
public String createExcel(String fileName, Class<?> pojoClass, Collection<?> dataSet){
try {
ExportParams params = new ExportParams(getLastMonth(fileName), "sheetName1", ExcelType.XSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params,pojoClass,dataSet);
FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth(fileName)+".xls");
workbook.write(fos);
fos.close();
return "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port +"/excel/"+getLastMonth(fileName)+".xls";
} catch (Exception e) {
log.error(e.getMessage());
return getLastMonth(fileName)+"下载失败。";
}
}
@PostMapping(value = "/mealTimes")
public String createManySheetExcel(){
List<Map<String ,Object>> list = new ArrayList<>();
List<Map<String,Object>> lunchMealTimesByCount = accessLogEntityRepository.lunchMealTimesByCount(getStartDateTime(),getEndDateTime());
List<MealTimesByCount> mealTimesByCountList = getCount(lunchMealTimesByCount);
ExportParams lunchMealTimesByCountParams = new ExportParams("中午就餐次数统计", "中午就餐次数统计", ExcelType.XSSF);
Map<String,Object> lunchMealTimesByCountMap = new HashMap<>();
lunchMealTimesByCountMap.put("title",lunchMealTimesByCountParams);
lunchMealTimesByCountMap.put("entity",MealTimesByCount.class);
lunchMealTimesByCountMap.put("data",mealTimesByCountList);
list.add(lunchMealTimesByCountMap);
List<Map<String,Object>> lunchMealTimesByDay = accessLogEntityRepository.lunchMealTimesByDay(getStartDateTime(),getEndDateTime());
List<MealTimesByDay> mealTimesByDayList = getDay(lunchMealTimesByDay);
ExportParams lunchMealTimesByDayParams = new ExportParams("中午就餐次数按天统计", "中午就餐次数按天统计", ExcelType.XSSF);
Map<String,Object> lunchMealTimesByDayMap = new HashMap<>();
lunchMealTimesByDayMap.put("title",lunchMealTimesByDayParams);
lunchMealTimesByDayMap.put("entity",MealTimesByDay.class);
lunchMealTimesByDayMap.put("data",mealTimesByDayList);
list.add(lunchMealTimesByDayMap);
List<Map<String,Object>> lunchMealTimesDetail = accessLogEntityRepository.lunchMealTimesDetail(getStartDateTime(),getEndDateTime());
List<MealTimesDetail> mealTimesDetailList = getDetail(lunchMealTimesDetail);
ExportParams lunchMealTimesDetailParams = new ExportParams("中午就餐详情", "中午就餐详情", ExcelType.XSSF);
Map<String,Object> lunchMealTimesDetailMap = new HashMap<>();
lunchMealTimesDetailMap.put("title",lunchMealTimesDetailParams);
lunchMealTimesDetailMap.put("entity",MealTimesDetail.class);
lunchMealTimesDetailMap.put("data",mealTimesDetailList);
list.add(lunchMealTimesDetailMap);
List<Map<String,Object>> dinnerMealTimesByCount = accessLogEntityRepository.dinnerMealTimesByCount(getStartDateTime(),getEndDateTime());
List<MealTimesByCount> dinnerMMealTimesByCountList = getCount(dinnerMealTimesByCount);
ExportParams dinnerMealTimesByCountParams = new ExportParams("晚餐次数统计", "晚餐次数统计", ExcelType.XSSF);
Map<String,Object> dinnerMealTimesByCountMap = new HashMap<>();
dinnerMealTimesByCountMap.put("title",dinnerMealTimesByCountParams);
dinnerMealTimesByCountMap.put("entity",MealTimesByCount.class);
dinnerMealTimesByCountMap.put("data",dinnerMMealTimesByCountList);
list.add(dinnerMealTimesByCountMap);
List<Map<String,Object>> dinnerMealTimesByDay = accessLogEntityRepository.dinnerMealTimesByDay(getStartDateTime(),getEndDateTime());
List<MealTimesByDay> dinnerMealTimesByDayList = getDay(dinnerMealTimesByDay);
ExportParams dinnerMealTimesByDayParams = new ExportParams("晚餐次数按天统计", "晚餐次数按天统计", ExcelType.XSSF);
Map<String,Object> dinnerMealTimesByDayMap = new HashMap<>();
dinnerMealTimesByDayMap.put("title",dinnerMealTimesByDayParams);
dinnerMealTimesByDayMap.put("entity",MealTimesByDay.class);
dinnerMealTimesByDayMap.put("data",dinnerMealTimesByDayList);
list.add(dinnerMealTimesByDayMap);
List<Map<String,Object>> dinnerMealTimesDetail = accessLogEntityRepository.dinnerMealTimesDetail(getStartDateTime(),getEndDateTime());
List<MealTimesDetail> dinnerMealTimesDetailList = getDetail(dinnerMealTimesDetail);
ExportParams dinnerMealTimesDetailParams = new ExportParams("晚餐详情", "晚餐详情", ExcelType.XSSF);
Map<String,Object> dinnerMealTimesDetailMap = new HashMap<>();
dinnerMealTimesDetailMap.put("title",dinnerMealTimesDetailParams);
dinnerMealTimesDetailMap.put("entity",MealTimesDetail.class);
dinnerMealTimesDetailMap.put("data",dinnerMealTimesDetailList);
list.add(dinnerMealTimesDetailMap);
return createManySheetExcel(list);
}
public String createManySheetExcel(List<Map<String,Object>> list){
try {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth("就餐统计")+".xlsx");
workbook.write(fos);
fos.close();
return "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port +"/excel/"+getLastMonth("就餐统计")+".xlsx";
} catch (Exception e) {
log.error(e.getMessage());
return getLastMonth("就餐统计")+"下载失败。";
}
}
}
调用的方法
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
FileOutputStream fos = new FileOutputStream(excelPath+"/"+getLastMonth("就餐统计")+".xlsx");
workbook.write(fos);
fos.close();