6 changed files with 168 additions and 30 deletions
@ -0,0 +1,71 @@ |
|||||
|
package org.dromara.platform.controller; |
||||
|
|
||||
|
import com.alibaba.excel.EasyExcel; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import jakarta.servlet.http.HttpServletResponse; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.common.log.annotation.Log; |
||||
|
import org.dromara.common.log.enums.BusinessType; |
||||
|
import org.dromara.platform.domain.PerformanceManagement; |
||||
|
import org.dromara.platform.domain.PointInfo; |
||||
|
import org.dromara.platform.domain.bo.PerformanceManagementBo; |
||||
|
import org.dromara.platform.domain.bo.PointInfoBo; |
||||
|
import org.dromara.platform.domain.vo.PerformanceManagementVo; |
||||
|
import org.dromara.platform.domain.vo.PointInfoVo; |
||||
|
import org.dromara.platform.listener.PerformanceManagementListener; |
||||
|
import org.dromara.platform.listener.PointInfoListener; |
||||
|
import org.dromara.platform.service.IPointInfoService; |
||||
|
import org.dromara.platform.service.IProjectInfoService; |
||||
|
import org.dromara.platform.service.ProjectManagerService; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.time.LocalDate; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Validated |
||||
|
@RequiredArgsConstructor |
||||
|
@RestController |
||||
|
@RequestMapping("/platform/excel") |
||||
|
public class ExcelController { |
||||
|
@Resource |
||||
|
private IPointInfoService pointInfoService ; |
||||
|
@PostMapping("/uploadPointInfo") |
||||
|
public void uploadPointInfo(MultipartFile file, HttpServletResponse response) throws IOException { |
||||
|
long t1 = System.currentTimeMillis(); |
||||
|
// 业务层
|
||||
|
EasyExcel.read(file.getInputStream(), PointInfoVo.class, new PointInfoListener(pointInfoService)).sheet().doRead(); |
||||
|
response.setContentType("text/html;charset=utf8"); |
||||
|
long t2 = System.currentTimeMillis(); |
||||
|
response.getWriter().println("导入数据成功!,共用时:"+(t2-t1)+"ms"); |
||||
|
log.info("批量导入点位信息项目成功! 共用时:{}ms",(t2-t1)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 导出点位信息 |
||||
|
*/ |
||||
|
@PostMapping("/ExportPointInfo") |
||||
|
public void ExportPointInfo(PointInfoBo bo, HttpServletResponse response) throws IOException { |
||||
|
List<PointInfoVo> list = pointInfoService.queryList(bo); |
||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
|
response.setCharacterEncoding("utf-8"); |
||||
|
String flag = "点位信息"; |
||||
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
||||
|
LocalDate currentDate = LocalDate.now(); |
||||
|
System.out.println("当前日期: " + currentDate); |
||||
|
String fileName = URLEncoder.encode( flag+currentDate, "UTF-8") |
||||
|
.replaceAll("\\+", "%20"); |
||||
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
||||
|
EasyExcel.write(response.getOutputStream(), PointInfoVo.class) |
||||
|
.sheet("点位信息"+currentDate) |
||||
|
.doWrite(list); |
||||
|
} |
||||
|
} |
@ -0,0 +1,69 @@ |
|||||
|
package org.dromara.platform.listener; |
||||
|
|
||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.platform.domain.AgreementInfo; |
||||
|
import org.dromara.platform.domain.PointInfo; |
||||
|
import org.dromara.platform.domain.vo.PointInfoVo; |
||||
|
import org.dromara.platform.service.IAgreementInfoService; |
||||
|
import org.dromara.platform.service.IPointInfoService; |
||||
|
import org.springframework.beans.BeanUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 自定义监听器读数据 |
||||
|
* @author gjh |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class PointInfoListener implements ReadListener<PointInfoVo> { |
||||
|
@Resource |
||||
|
private IPointInfoService pointInfoService ; |
||||
|
private List<PointInfo> list = new ArrayList<>(); |
||||
|
/** |
||||
|
* 自己定义一个缓冲量 |
||||
|
*/ |
||||
|
private static final int BATCH_COUNT = 20; |
||||
|
|
||||
|
|
||||
|
|
||||
|
public PointInfoListener( IPointInfoService pointInfoService) { |
||||
|
this.pointInfoService = pointInfoService; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 每读一行数据都会调用这个方法 |
||||
|
* |
||||
|
* @param pointInfoVo |
||||
|
* @param analysisContext |
||||
|
*/ |
||||
|
@Override |
||||
|
public void invoke(PointInfoVo pointInfoVo, AnalysisContext analysisContext) { |
||||
|
PointInfo pointInfo = new PointInfo(); |
||||
|
BeanUtils.copyProperties(pointInfoVo,pointInfo); |
||||
|
// 读取一行数据就添加到集合
|
||||
|
list.add(pointInfo); |
||||
|
// 判断是否到达缓存量了
|
||||
|
if (list.size() >= BATCH_COUNT){ |
||||
|
// 操作数据库
|
||||
|
pointInfoService.addData(list); |
||||
|
list = new ArrayList<>(BATCH_COUNT); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 读完整个excel之后再调用这个方法 |
||||
|
* |
||||
|
* @param analysisContext |
||||
|
*/ |
||||
|
@Override |
||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
|
if (list.size()>0){ |
||||
|
pointInfoService.addData(list); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue