123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- package com.sinosoft.am.duty.records.dao;
- import java.io.FileInputStream;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import com.formaction.vo.Msg;
- import com.sinosoft.am.duty.records.vo.EcmAmDuty;
- import com.sinosoft.am.duty.records.vo.EcmDutyDetail1;
- import com.sinosoft.common.util.DateUtil;
- import com.sinosoft.lz.system.department.Department;
- import com.sinosoft.lz.system.department.DepartmentDao;
- public class AlertDutyExcel1 {
-
- public Msg readExcel(String filePath,String oper_name, String oper_dept, String userId) {
- Msg msg=new Msg();
- try {
-
- String excelType = filePath.substring(filePath.lastIndexOf(".") + 1);
- // Read the Excel 2003-2007
- if (excelType != null && !"".equalsIgnoreCase(excelType)
- && !"xlsx".equalsIgnoreCase(excelType)) {
- // 创建对Excel工作簿文件的引用
- HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
- // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
- // System.out.println("==========开始第 " + i
- // + " 个sheet============");
- //只读一页
- HSSFSheet childSheet = workbook.getSheetAt(0);
- EcmAmDuty duty = new EcmAmDuty();
-
- //************************检查是否匹配模版*****************************
- childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
-
- childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
-
- childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
-
- childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
-
- childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
- String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
-
- System.out.println("");
-
- childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
-
- childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- String checkPer = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班成员,联系电话
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkDutyWay = childSheet.getRow(3).getCell(5).getStringCellValue(); //值班方式
-
- // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
- // +checkPer+" ,"+checkPerTel+" ,"+checkDutyWay);
-
- if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
- || !"领导电话".equals(checkLeaderTel) || !"值班成员,联系电话".equals(checkPer) || !"值班方式".equals(checkDutyWay)) {
-
- msg.setSucsess("false");
- msg.setInfo("导入Excel文件与模版不匹配!");
- workbook.close();
- return msg;
- }
-
- //*********************************************************************
-
-
- childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
- String name = childSheet.getRow(2).getCell(1).getStringCellValue();
-
- childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
- String deptName = childSheet.getRow(2).getCell(4).getStringCellValue();
-
- // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
- //
- // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
- //
- // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
- duty.setName(name);
-
- String deptId = deptName;
- if(deptName!=null&&!"".equals(deptName)){
- DepartmentDao deptDao = new DepartmentDao();
- Department dept = deptDao.getDeptByName(deptName);
- if(dept!=null&&dept.getFd_objectid()!=null&&!"".equals(dept.getFd_objectid())){
- deptId = dept.getFd_objectid();
- }
- }
- duty.setDept_names(deptId);
-
- duty.setOper_name(oper_name);
- duty.setOper_dept(oper_dept);
- duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmmss));
- duty.setDuty_type("1");
- duty.setOper_name_id(userId);
-
- List<EcmDutyDetail1> datailList = new ArrayList<EcmDutyDetail1>();
- int rowCnt = childSheet.getLastRowNum();
- System.out.println("rowCnt"+ rowCnt);
-
- for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
- HSSFCell cell = childSheet.getRow(r).getCell(0);
- String value = getValue(cell);
- if("".equals(value.trim())){//如果值为空,则跳过该行
- continue;
- }
- EcmDutyDetail1 detail = new EcmDutyDetail1();
-
- //childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- //detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
-
- childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- //System.out.println(childSheet.getRow(r).getCell(1).getStringCellValue());
- detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
-
- childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
-
- childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
- detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
-
- childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
-
- childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- detail.setName(childSheet.getRow(r).getCell(4).getStringCellValue());
-
- //childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- //detail.setTelephone(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String duty_way = childSheet.getRow(r).getCell(5).getStringCellValue();
- String wayValue = "";
- if(duty_way.contains("在岗值班")){
- wayValue = "0";
- }
- if(duty_way.contains("电话值班")){
- wayValue = "1";
- }
- if(duty_way.contains("应急指挥中心值班")){
- wayValue = "2";
- }
- detail.setDuty_way(wayValue);
- datailList.add(detail);
-
- }
- workbook.close();
- EcmDutyDao1 dao = new EcmDutyDao1();
- if(dao.saveDutyBatch(duty, datailList)){
- msg.setSucsess("true");
- }
- // }
-
- } else { // Read the Excel 2010
- XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
- filePath));
- // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
- // System.out.println("==========开始第 " + i
- // + " 个sheet============");
- XSSFSheet childSheet = workbook.getSheetAt(0);
- EcmAmDuty duty = new EcmAmDuty();
-
- //************************检查是否匹配模版*****************************
- childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
-
- childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
-
- childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
-
- childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
-
- childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
- String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
-
- childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
-
- childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- String checkPer = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班成员
-
- //childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- //String checkPerTel = childSheet.getRow(3).getCell(5).getStringCellValue(); //成员电话
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkDutyWay = childSheet.getRow(3).getCell(5).getStringCellValue(); //值班方式
-
- System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
- +checkPer+" ,"+checkDutyWay);
-
- if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
- || !"领导电话".equals(checkLeaderTel) || !"值班成员,联系电话".equals(checkPer) || !"值班方式".equals(checkDutyWay)) {
-
- msg.setSucsess("false");
- msg.setInfo("导入Excel文件与模版不匹配!");
- workbook.close();
- return msg;
- }
-
- //*********************************************************************
-
- childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
- String name = childSheet.getRow(2).getCell(1).getStringCellValue();
-
- childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
- String dept = childSheet.getRow(2).getCell(4).getStringCellValue();
-
- // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
- //
- // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
- //
- // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
- duty.setName(name);
- duty.setDept_names(dept);
- duty.setOper_name(oper_name);
- duty.setOper_dept(oper_dept);
- duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmm));
- duty.setDuty_type("1");
-
- List<EcmDutyDetail1> datailList = new ArrayList<EcmDutyDetail1>();
-
- for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
- EcmDutyDetail1 detail = new EcmDutyDetail1();
-
- // childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- // detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
-
- childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
- detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
-
- childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
-
- childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
- detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
-
- childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
- detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
-
- childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
- detail.setName(childSheet.getRow(r).getCell(4).getStringCellValue());
-
- //childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- //detail.setTelephone(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
-
- String duty_way = childSheet.getRow(r).getCell(5).getStringCellValue();
-
- String wayValue = "";
- if(duty_way.contains("在岗值班")){
- wayValue = "0";
- }
- if(duty_way.contains("电话值班")){
- wayValue = "1";
- }
- if(duty_way.contains("应急指挥中心值班")){
- wayValue = "2";
- }
- detail.setDuty_way(wayValue);
-
-
- datailList.add(detail);
- }
- workbook.close();
- EcmDutyDao1 dao = new EcmDutyDao1();
- if(dao.saveDutyBatch(duty, datailList)){
- msg.setSucsess("true");
- }
- }
-
- //}
- } catch (Exception e) {
- throw new RuntimeException(e.getMessage());
- }
-
- return msg;
- }
- /**
- * 获取Excel中某个单元格的值
- *
- * @param cell
- * @return
- * @throws Exception
- */
- @SuppressWarnings("deprecation")
- public String getValue(HSSFCell cell) throws Exception {
- String value = "";
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
- System.out.println("数字类型");
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- // 如果是date类型则 ,获取该cell的date值
- value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
- .toString();
- java.util.Date date1 = new Date(value);
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- value = format.format(date1);
- } else {// 纯数字
- value = String.valueOf(cell.getNumericCellValue()).split("[.]")[0];
- }
- break;
- /* 此行表示单元格的内容为string类型 */
- case HSSFCell.CELL_TYPE_STRING: // 字符串型
- value = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_FORMULA:// 公式型
- // 读公式计算值
- value = String.valueOf(cell.getNumericCellValue());
- if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
- value = cell.getStringCellValue().toString();
- }
- cell.getCellFormula();
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
- value = " " + cell.getBooleanCellValue();
- break;
- /* 此行表示该单元格值为空 */
- case HSSFCell.CELL_TYPE_BLANK: // 空值
- value = "";
- break;
- case HSSFCell.CELL_TYPE_ERROR: // 故障
- value = "";
- break;
- default:
- value = cell.getStringCellValue().toString();
- }
- if(checkValueTrim(value)){
- return "";
- }
- return value;
- }
-
-
- private boolean checkValueTrim(String value) {
- boolean flagg = false;
- if("".equals(value.trim())){
- flagg = true;
- }
- return flagg;
- }
-
- }
|