生成统计excel表格
This commit is contained in:
parent
ad02b97f66
commit
d61ba6bd55
@ -9,6 +9,8 @@ import cn.iocoder.yudao.framework.common.util.string.StrUtils;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
|
||||
import cn.iocoder.yudao.module.inspect.controller.admin.inspectapplylog.vo.InspectApplylogSaveReqVO;
|
||||
import cn.iocoder.yudao.module.inspect.controller.admin.inspectdoctor.vo.InspectDoctorPageReqVO;
|
||||
import cn.iocoder.yudao.module.inspect.controller.admin.inspectdoctor.vo.InspectDoctorRespVO;
|
||||
import cn.iocoder.yudao.module.inspect.controller.admin.inspectpacsdata.vo.InspectPacsDataSaveReqVO;
|
||||
import cn.iocoder.yudao.module.inspect.controller.admin.inspectpatientitems.vo.InspectPatientitemsSaveReqVO;
|
||||
import cn.iocoder.yudao.module.inspect.dal.dataobject.inspectdoctor.InspectDoctorDO;
|
||||
@ -1942,4 +1944,14 @@ public class InspectPatientController {
|
||||
// 将Map转换为List并返回
|
||||
return success(new ArrayList<>(statisticsMap.values()));
|
||||
}
|
||||
|
||||
@GetMapping("/export-statistics-excel")
|
||||
@Operation(summary = "导出统计 Excel")
|
||||
@ApiAccessLog(operateType = EXPORT)
|
||||
public void exportExcel(@Valid @RequestParam("dates") @DateTimeFormat(pattern = "yyyy-MM-dd") List<LocalDate> dates,HttpServletResponse response) throws IOException {
|
||||
patientService.exportStatistics(dates,response);
|
||||
// 导出 Excel
|
||||
/*ExcelUtils.write(response, "统计.xls", "数据", InspectDoctorRespVO.class,
|
||||
BeanUtils.toBean(list, InspectDoctorRespVO.class));*/
|
||||
}
|
||||
}
|
||||
@ -1,32 +1,46 @@
|
||||
package cn.iocoder.yudao.module.inspect.controller.admin.inspectpatient.vo;
|
||||
|
||||
import cn.hutool.core.annotation.Alias;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WorkloadStatisticsVO {
|
||||
|
||||
@Schema(description = "卫生院名称")
|
||||
@Alias("卫生院")
|
||||
private String orgname;
|
||||
|
||||
@Schema(description = "行政村名称")
|
||||
@Alias("行政村")
|
||||
private String districtname;
|
||||
|
||||
@Schema(description = "老年人数量")
|
||||
@Alias("老年人")
|
||||
private Integer oldmanflag;
|
||||
|
||||
@Schema(description = "高血压数量")
|
||||
@Alias("高血压")
|
||||
private Integer htnflag;
|
||||
|
||||
@Schema(description = "糖尿病数量")
|
||||
@Alias("糖尿病")
|
||||
private Integer diaflag;
|
||||
|
||||
@Schema(description = "精神病数量")
|
||||
@Alias("精神病")
|
||||
private Integer smiflag;
|
||||
|
||||
@Schema(description = "肺结核数量")
|
||||
@Alias("肺结核")
|
||||
private Integer pulflag;
|
||||
|
||||
@Schema(description = "总数")
|
||||
@Alias("总数")
|
||||
private Integer sum;
|
||||
}
|
||||
@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.inspect.dal.dataobject.inspectpatient.InspectPati
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
@ -150,5 +151,5 @@ public interface InspectPatientService {
|
||||
*/
|
||||
List<PatientSupplementVO> getPatientSupplementsByDates(List<LocalDate> dates);
|
||||
|
||||
|
||||
void exportStatistics(List<LocalDate> dates, HttpServletResponse response);
|
||||
}
|
||||
@ -1,5 +1,8 @@
|
||||
package cn.iocoder.yudao.module.inspect.service.inspectpatient;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.hutool.poi.excel.ExcelWriter;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
|
||||
import cn.iocoder.yudao.module.inspect.dal.dataobject.inspectpacsdata.InspectPacsDataDO;
|
||||
@ -19,6 +22,7 @@ import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.jodconverter.DocumentConverter;
|
||||
import org.jodconverter.LocalConverter;
|
||||
@ -34,6 +38,8 @@ import cn.iocoder.yudao.module.inspect.dal.mysql.inspectpatient.InspectPatientMa
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
@ -800,4 +806,140 @@ public class InspectPatientServiceImpl implements InspectPatientService {
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全加法
|
||||
* @param a 加数
|
||||
* @param b 加数
|
||||
* @return
|
||||
*/
|
||||
private Integer addIntegers(Integer a, Integer b) {
|
||||
return (a == null ? 0 : a) + (b == null ? 0 : b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算数组中相同属性的累加值,返回一个新的对象
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
private WorkloadStatisticsVO sumWorkloadList(List<WorkloadStatisticsVO> list) {
|
||||
return list.stream()
|
||||
.reduce(new WorkloadStatisticsVO(), (sum, current) -> {
|
||||
sum.setOldmanflag(addIntegers(sum.getOldmanflag(), current.getOldmanflag()));
|
||||
sum.setHtnflag(addIntegers(sum.getHtnflag(), current.getHtnflag()));
|
||||
sum.setDiaflag(addIntegers(sum.getDiaflag(), current.getDiaflag()));
|
||||
sum.setSmiflag(addIntegers(sum.getSmiflag(), current.getSmiflag()));
|
||||
sum.setPulflag(addIntegers(sum.getPulflag(), current.getPulflag()));
|
||||
sum.setSum(addIntegers(sum.getSum(), current.getSum()));
|
||||
return sum;
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void exportStatistics(List<LocalDate> dates, HttpServletResponse response) {
|
||||
try {
|
||||
// 获取指定日期范围内的所有患者补充信息
|
||||
List<PatientSupplementVO> supplements = getPatientSupplementsByDates(dates);
|
||||
|
||||
// 使用Map来存储统计结果,key为卫生院名称+行政村名称
|
||||
Map<String, WorkloadStatisticsVO> statisticsMap = new HashMap<>();
|
||||
|
||||
// 遍历所有补充信息进行统计
|
||||
for (PatientSupplementVO supplement : supplements) {
|
||||
String key = supplement.getOrgname() + "_" + supplement.getDistrictname();
|
||||
WorkloadStatisticsVO statistics = statisticsMap.computeIfAbsent(key, k -> {
|
||||
WorkloadStatisticsVO vo = new WorkloadStatisticsVO();
|
||||
vo.setOrgname(supplement.getOrgname());
|
||||
vo.setDistrictname(supplement.getDistrictname());
|
||||
vo.setOldmanflag(0);
|
||||
vo.setHtnflag(0);
|
||||
vo.setDiaflag(0);
|
||||
vo.setSmiflag(0);
|
||||
vo.setPulflag(0);
|
||||
vo.setSum(0);
|
||||
return vo;
|
||||
});
|
||||
|
||||
// 统计各项数量
|
||||
if ("1".equals(supplement.getOldmanflag())) {
|
||||
statistics.setOldmanflag(statistics.getOldmanflag() + 1);
|
||||
}
|
||||
if ("1".equals(supplement.getHtnflag())) {
|
||||
statistics.setHtnflag(statistics.getHtnflag() + 1);
|
||||
}
|
||||
if ("1".equals(supplement.getDiaflag())) {
|
||||
statistics.setDiaflag(statistics.getDiaflag() + 1);
|
||||
}
|
||||
if ("1".equals(supplement.getSmiflag())) {
|
||||
statistics.setSmiflag(statistics.getSmiflag() + 1);
|
||||
}
|
||||
if ("1".equals(supplement.getPulflag())) {
|
||||
statistics.setPulflag(statistics.getPulflag() + 1);
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
statistics.setSum(statistics.getOldmanflag() + statistics.getHtnflag() +
|
||||
statistics.getDiaflag() + statistics.getSmiflag() +
|
||||
statistics.getPulflag());
|
||||
}
|
||||
|
||||
// excel数据
|
||||
List<WorkloadStatisticsVO> statisticsVOS = new ArrayList<>(statisticsMap.values());
|
||||
statisticsVOS.sort(Comparator.comparing(WorkloadStatisticsVO::getOrgname,Comparator.nullsLast(Comparator.naturalOrder())));
|
||||
// 通过工具类创建writer,默认创建xls格式
|
||||
ExcelWriter writer = ExcelUtil.getWriter();
|
||||
// 设计表头
|
||||
writer.merge(0,1,0,0,"卫生院",true);
|
||||
writer.merge(0,1,1,1,"行政村",true);
|
||||
writer.merge(0,0,2,7,"体检数",true);
|
||||
writer.addHeaderAlias("oldmanflag","老年人");
|
||||
writer.addHeaderAlias("htnflag","高血压");
|
||||
writer.addHeaderAlias("diaflag","糖尿病");
|
||||
writer.addHeaderAlias("smiflag","精神病");
|
||||
writer.addHeaderAlias("pulflag","肺结核");
|
||||
writer.addHeaderAlias("sum","总数");
|
||||
|
||||
writer.passCurrentRow();
|
||||
writer.write(statisticsVOS,true);
|
||||
|
||||
// 按第一列内容分组,记录行范围
|
||||
Map<Object, List<Integer>> groupMap = new LinkedHashMap<>();
|
||||
List<Object> orangnameList = new ArrayList<>();
|
||||
for (int i = 0; i < statisticsVOS.size(); i++) {
|
||||
Object key = statisticsVOS.get(i).getOrgname();
|
||||
orangnameList.add(key);
|
||||
groupMap.computeIfAbsent(key, k -> new ArrayList<>()).add(i + 3); // +1跳过标题行
|
||||
}
|
||||
|
||||
// 动态合并单元格
|
||||
int firstCol = 0; // 第一列索引为0
|
||||
groupMap.forEach((key, rows) -> {
|
||||
if (rows.size() > 1) {
|
||||
int startRow = orangnameList.indexOf(key)+2;
|
||||
int endRow =orangnameList.lastIndexOf(key)+2;
|
||||
// 合并单元格(起始行、结束行、起始列、结束列)
|
||||
writer.merge(startRow, endRow, firstCol, firstCol, key.toString(), false);
|
||||
}
|
||||
});
|
||||
WorkloadStatisticsVO sumVO = sumWorkloadList(statisticsVOS);
|
||||
sumVO.setOrgname("合计");
|
||||
writer.write(Arrays.asList(sumVO));
|
||||
|
||||
writer.autoSizeColumn(0);
|
||||
writer.autoSizeColumn(1);
|
||||
//response为HttpServletResponse对象
|
||||
response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
||||
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
||||
response.setHeader("Content-Disposition","attachment;filename=test.xls");
|
||||
|
||||
ServletOutputStream out=response.getOutputStream();
|
||||
|
||||
writer.flush(out, true);
|
||||
// 关闭writer,释放内存
|
||||
writer.close();
|
||||
//此处记得关闭输出Servlet流
|
||||
IoUtil.close(out);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user