package cn.com.goldenwater.dcproj.utils.impexcel;
import cn.com.goldenwater.dcproj.dao.AttAdXBaseDao;
import cn.com.goldenwater.dcproj.dao.AttRsBaseDao;
import cn.com.goldenwater.dcproj.dao.AttRsBaseFjDao;
import cn.com.goldenwater.dcproj.model.AttAdXBase;
import cn.com.goldenwater.dcproj.model.AttRsBase;
import cn.com.goldenwater.dcproj.model.AttRsBaseFj;
import cn.com.goldenwater.dcproj.param.AttAdXBaseParam;
import cn.com.goldenwater.dcproj.param.AttRsBaseParam;
import cn.com.goldenwater.dcproj.service.AttRsBaseService;
import cn.com.goldenwater.dcproj.socket.WebSocketServer;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static cn.com.goldenwater.dcproj.utils.export.FieldFormatDispatch.getFormatValue;
import static cn.com.goldenwater.dcproj.utils.impexcel.ImpUtil.XLS;
import static cn.com.goldenwater.dcproj.utils.impexcel.ImpUtil.checkIsDate;
/**
*
* 导入工具类
*
*
*
* @author liyz
* @date 2019/4/10 19:43
*/
public class ImpRsExcel {
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private static final String EMPTY = "";
@Autowired
private AttRsBaseDao attRsBaseDao;
@Autowired
private AttAdXBaseDao attAdXBaseDao;
@Autowired
private AttRsBaseService attRsBaseService;
@Autowired
private AttRsBaseFjDao attRsBaseFjDao;
public static void main(String[] args) {
String filePath = "D:\\最新福建省水库名录20200227.xls";
int startRow = 0;
int endRow = 0;
try {
//List bookList = (List) ImpRsExcel.importExcel(filePath, startRow, endRow, HashMap.class, "", "sk");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 正则表达式 用于匹配属性的第一个字母
*/
private static final String REGEX = "[a-zA-Z]";
/**
* 功能: Excel数据导入到数据库
* 参数: originUrl[Excel表的所在路径]
* 参数: startRow[从第几行开始]
* 参数: endRow[到第几行结束
* (0表示所有行;
* 正数表示到第几行结束;
* 负数表示到倒数第几行结束)]
* 参数: clazz[要返回的对象集合的类型]
*/
public void importExcel(String originUrl, int startRow, int endRow, Class> clazz, String sid, String type) throws Exception {
//是否打印提示信息
boolean showInfo = true;
//doImportExcel(originUrl, startRow, endRow, showInfo, clazz, sid, type);
}
/**
* 功能:判断文件是否存在
*/
public static File isExist(String originUrl) throws IOException {
// 判断文件是否存在
File file = new File(originUrl);
if (!file.exists()) {
throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");
}
return file;
}
/**
* 功能:真正实现导入
*/
public void doImportExcel(String originUrl, int startRow, int endRow, boolean showInfo, Class> clazz,
String sid, String type,AttAdXBaseDao attAdXBaseDao1,AttRsBaseFjDao attRsBaseFjDao1
,AttRsBaseService attRsBaseService1,AttRsBaseDao attRsBaseDao1) throws IOException, ParseException {
attAdXBaseDao = attAdXBaseDao1;
attRsBaseFjDao = attRsBaseFjDao1;
attRsBaseService = attRsBaseService1;
attRsBaseDao = attRsBaseDao1;
// 判断文件是否存在
File file = isExist(originUrl);
HSSFWorkbook wb = null;
FileInputStream fis = null;
List rowList = new ArrayList();
try {
fis = new FileInputStream(file);
// 去读Excel
wb = new HSSFWorkbook(fis);
Sheet sheet = wb.getSheetAt(0);
// 获取最后行号
int lastRowNum = sheet.getLastRowNum();
// 如果>0,表示有数据
if (lastRowNum > 0) {
out("\n开始读取并校验名为【" + sheet.getSheetName() + "】的内容:", showInfo, sid);
WebSocketServer.sendInfo(SIMPLE_DATE_FORMAT.format(new Date()) + "-②开始读取并校验名为【" + sheet.getSheetName() + "】的内容", sid);
}
List keys = new ArrayList<>();
Row keyRow = sheet.getRow(0);
for (int i = 0; i < keyRow.getPhysicalNumberOfCells(); i++) {
keys.add(keyRow.getCell(i).toString());
}
Row row = null;
// 循环读取
for (int i = startRow; i <= lastRowNum + endRow; i++) {
row = sheet.getRow(i);
HashMap map = new LinkedHashMap<>();
if (row != null) {
rowList.add(row);
// 获取每一单元格的值
for (int j = 0; j < row.getLastCellNum(); j++) {
String value = getCellValue(row.getCell(j));
if(j ads = getAdCds("福建省",map.get("所在市"),map.get("归属地"));
String provinceCode = "350000000000";
String cityCode = ads.get("shi");
String countryCode = ads.get("xian");
if(org.apache.commons.lang3.StringUtils.isNotBlank(countryCode)){
adCd = countryCode;
}else if(org.apache.commons.lang3.StringUtils.isNotBlank(cityCode)){
adCd = cityCode;
}else if(org.apache.commons.lang3.StringUtils.isNotBlank(provinceCode)){
adCd = provinceCode;
}
if(StringUtils.isEmpty(countryCode)){
System.out.println("行政区划未找到:"+map.get("所在市")+"--"+map.get("归属地")+"--"+map.get("水库名称"));
}
Map mapRs = checkRsBase(map.get("水库名称"), countryCode);
String rsCode = mapRs.get("rsCode");
AttRsBaseFj attRsBase = new AttRsBaseFj();
if(StringUtils.isNotBlank(rsCode)){
attRsBase = attRsBaseFjDao.get(rsCode);
}
attRsBase.setRsName(map.get("水库名称"));
attRsBase.setEngScal(encagl(map.get("工程规模")));
attRsBase.setAdmDiv(adCd);
attRsBase.setAdmDivName(map.get("归属地"));
attRsBase.setLocation("福建省-"+map.get("所在市")+"-"+map.get("归属地"));
attRsBase.setRsAdmName(map.get("管理单位名称"));
attRsBase.setRegCode(map.get("注册登记号"));
attRsBase.setRsClass(map.get("功能类别"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
if(StringUtils.isNotEmpty(map.get("下闸蓄水时间"))){
Date date = simpleDateFormat.parse(map.get("下闸蓄水时间"));
attRsBase.setImpdDt(date);
}
if(StringUtils.isNotEmpty(map.get("最近一次加固改造_蓄水运行时间"))){
Date date = simpleDateFormat.parse(map.get("最近一次加固改造_蓄水运行时间"));
attRsBase.setRnfcImpdDt(date);
}
if(StringUtils.isNotEmpty(map.get("总库容"))){
attRsBase.setTotCap(Double.parseDouble(map.get("总库容")));
}
attRsBase.setDamType(map.get("主坝坝型"));
attRsBase.setDamTypeMat(typeMat(map.get("主坝坝型")));
attRsBase.setDamTypeStr(typeStr(map.get("主坝坝型")));
if(StringUtils.isNotEmpty(map.get("主坝最大坝高"))){
attRsBase.setDamSizeHig(Double.parseDouble(map.get("主坝最大坝高")));
}
if(StringUtils.isNotEmpty(map.get("设计洪水位"))){
attRsBase.setDesFlStag(Double.parseDouble(map.get("设计洪水位")));
}
if(StringUtils.isNotEmpty(map.get("防洪高水位"))){
attRsBase.setFlConTopStag(Double.parseDouble(map.get("防洪高水位")));
}
attRsBase.setEdtrPesr("福建");
attRsBase.setWiunWaoLegPers(map.get("行政责任人"));
attRsBase.setWiunWaoLegPersUnit(map.get("行政责任人工作单位"));
attRsBase.setWiunWaoLegPersDuty(map.get("行政责任人职务"));
attRsBase.setWiunWaoLegPersTel(map.get("行政责任人联系方式"));
attRsBase.setTechPers(map.get("技术责任人"));
attRsBase.setTechPersUnit(map.get("技术责任人工作单位"));
attRsBase.setTechPersDuty(map.get("技术责任人职务"));
attRsBase.setTechPersTel(map.get("技术责任人联系方式"));
attRsBase.setPatrolPers(map.get("巡查责任人"));
attRsBase.setPatrolPersUnit(map.get("巡查责任人工作单位"));
attRsBase.setPatrolPersDuty(map.get("巡查责任人职务"));
attRsBase.setPatrolPersTel(map.get("巡查责任人联系方式"));
attRsBase.setUptm(new Date());
attRsBase.setRegNo(i+1+"");
if(StringUtils.isEmpty(rsCode)){
String str = attRsBaseFjDao.findMax(countryCode.substring(0,6));
if(StringUtils.isEmpty(str)){
str = countryCode;
}
long a = Long.parseLong(str)+1;
rsCode = a+"";
attRsBase.setRsCode(rsCode);
out("第" + (i + 1) + "行:", showInfo, false, sid);
System.out.println(attRsBase.toString());
attRsBaseFjDao.insert(attRsBase);
}else {
attRsBase.setRsCode(rsCode);
attRsBaseFjDao.insert(attRsBase);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
wb.close();
}
//return returnObjectList(rowList, type, sid);
}
/**
* 功能:获取单元格的值
*/
private static String getCellValue(Cell cell) {
Object result = "";
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
result = cell.getStringCellValue();
break;
case NUMERIC:
result = cell.getNumericCellValue();
break;
case BOOLEAN:
result = cell.getBooleanCellValue();
break;
case FORMULA:
result = cell.getCellFormula();
break;
case ERROR:
result = cell.getErrorCellValue();
break;
case BLANK:
break;
default:
break;
}
}
return result.toString();
}
/**
* 功能:返回指定的对象集合
*/
private static List