第二步:查询数据,匹配对应字典表数据,然后导出
1)字典表图片
2)数据处理完,返回处理完的数据List list;
private List<String> dealData(Connection conn, FollowMmcVisitPO followMmcVisitPO, List<String> excelList) throws Exception {
List<String> list = new ArrayList<>();
// 获取这个人的完成随访次数
Long followTimes = this.followDao.getCountMMCvisit(conn,followMmcVisitPO.getMemberId(),followMmcVisitPO.getModifyDt());
followTimes = followTimes + 1;
for (int i = 0; i < excelList.size(); i++) {
String tempValue = "";
// 处理首次特殊项
if (excelList.get(i).equals("随访名称")){
if (followMmcVisitPO.getFollowType().equals("15")){
tempValue = "糖尿病病人问卷调查";
}else if (followMmcVisitPO.getFollowType().equals("16")){
tempValue = "三个月随访";
}else tempValue = "半年随访";
}
tempValue = this.dealFollowData(conn, excelList, followMmcVisitPO, tempValue, i);
list.add(tempValue);
}
return list;
}
注:excellist 是第一步(另一篇文章)中java读取Excel存储的字段,followMmcVisitPO单用户数据,i 对应行的下标值;
* 处理随访数据
* */
private String dealFollowData(Connection conn,List<String> excelList,FollowMmcVisitPO followMmcVisitPO,String tempValue,int i){
try {
Map<String, String> memberInfoMap = JsonSerializer.jsonToStringMap(followMmcVisitPO.getMemberInfo());
Map<String, String> followInfoMap = JsonSerializer.jsonToStringMap(followMmcVisitPO.getFollowInfo());
String followType = followMmcVisitPO.getFollowType();
List<FollowQuestionPO> followQuestionPOS = this.followDao.listfollowQuestionPO(conn, followMmcVisitPO.getFollowType()); // 获取字典表的数据
for (int j = 0; j < followQuestionPOS.size(); j++) {
if (excelList.get(i).equals(followQuestionPOS.get(j).getSubQuestionContent().trim())) { // 先匹配模板对应行的key和字典表的key是否一致
for (String key :memberInfoMap.keySet()){
if (key.equals(followQuestionPOS.get(j).getSubQuestionCode())) {
tempValue = memberInfoMap.get(key);
//System.out.println("key:" + key +"值:"+ memberInfoMap.get(key));
}
}
for (String key : followInfoMap.keySet()) {
if (followQuestionPOS.get(j).getOptionType().equals("3")){
if (key.equals(followQuestionPOS.get(j).getSubQuestionCode())) {
tempValue = followInfoMap.get(key);
}
}else {
String[] arr = followInfoMap.get(key).split(","); // 为避免选项是多选的,做的处理
for (String optionCode : arr) {
// 当map中的key等于字典表的subcode,并且可以对应的value等于optioncode时,那就进行赋值。
if (key.equals(followQuestionPOS.get(j).getSubQuestionCode()) && optionCode.equals(followQuestionPOS.get(j).getOptionCode())) {
if (tempValue.equals("")) {
tempValue = followQuestionPOS.get(j).getOptionContent();
} else {
tempValue = tempValue + "、" + followQuestionPOS.get(j).getOptionContent();
}
}
}
}
}
}
}
return tempValue;
}
3)进行数据导出,填充
//获取Excel的值,匹配项
List<String> excellist = this.getExcelValue();
//获取需要处理随访列表(followMmcVisitPOList)
List<FollowMmcVisitPO> followMmcVisitPOList = this.followDao.listfollowMMCvisit(conn,dcnyStart,dcnyEnd);
logger.info("startTime:"+startTime+" @需要导出的随访数:"+followMmcVisitPOList.size());
for (int j=0;j<followMmcVisitPOList.size();j++) {
//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
Thread.sleep(200);
int beginCell = j + columnNumber; //起始列
FileOutputStream fo = null;
try {
//处理数据//拼好的数据
List<String> fillList = this.dealData(conn,followMmcVisitPOList.get(j),excellist);
if (fillList.size()==0 || fillList==null || fillList.isEmpty()){
continue;
}
for (int i=0;i<fillList.size();i++) {
String path = ConfigUtils.getValueByKey("Excel_follow_address")+year+File.separator;
String savePath = ""; //存储的位置
// 输出的文件流保存到本地文件
File tempFile = new File(path);
if (!tempFile.exists()) { //文件夹不存在 读取模板
tempFile.mkdirs();
savePath = path+startTime.substring(0,7)+".xls";
path = System.getProperty("user.dir")+"/conf/followTemplate.xls";
} else { //如果文件夹存在,文件不存在,创建文件
path = path+startTime.substring(0,7)+".xls";
savePath = path;
File excelFile = new File(path);
if(!excelFile.exists()){
excelFile.createNewFile();
path = System.getProperty("user.dir")+"/conf/followTemplate.xls";
}
}
FileInputStream templateFile_Input = new FileInputStream(path);
POIFSFileSystem fs = new POIFSFileSystem(templateFile_Input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
//sheet.setDefaultColumnWidth();
HSSFRow row = sheet.getRow(i);
if( row == null ){
//如果不做空判断,你必须让你的模板文件画好边框,beginRow和beginCell必须在边框最大值以内
//否则会出现空指针异常
row = sheet.createRow(i);
}
HSSFCell cell = row.getCell(beginCell);
if(null == cell){
cell = row.createCell(beginCell);
}
// HSSFCellStyle cellStyle = this.createCellStyle(wb);
// cell.setCellStyle(cellStyle);
//设置存入内容为字符串
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//向单元格中放入值
cell.setCellValue(fillList.get(i));
fo = new FileOutputStream(savePath); // 输出到文件
wb.write(fo);
wb.close();
}
}
注:由于代码量比较多,特殊处理和其他的数据表没有展示,只截取部分代码(说明:这只是一个简单的java定时程序,用于定时跑取数据)。
4)导出展示
好处是:通过模板进行导出,后期模板变化,我们只需维护字典表。
不好的地方:很多特殊处理是没有办法服用的。