123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- package com.sinosoft.em.alert.duty.records.dao;
- import java.io.FileInputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- 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.persistence.DbConnection;
- import com.sinosoft.common.util.DateUtil;
- import com.sinosoft.em.alert.duty.records.vo.EcmDutyDetail;
- import com.sinosoft.em.alert.duty.records.vo.EcmEmDuty;
- import com.sinosoft.lz.system.department.Department;
- import com.sinosoft.lz.system.department.DepartmentDao;
- public class EcmDutyExcel2 {
- public Msg readExcel(String filePath,String oper_name, String oper_dept, String userId,String dept_names) {
- 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);
- EcmEmDuty duty = new EcmEmDuty();
-
- //************************检查是否匹配模版*****************************
- 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 checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
-
- childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
-
- childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
-
- childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
-
- childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
-
- childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
-
- childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
-
- childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyCar = childSheet.getRow(3).getCell(12).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(checkPerTel) || !"值班方式".equals(checkDutyWay)) {
-
- msg.setSucsess("false");
- msg.setInfo("导入Excel文件与模版不匹配!");
- workbook.close();
- return msg;
- }*/
-
- if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
- || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
- || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
- || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
-
- 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("2");
- duty.setRelease("0");
- duty.setOper_name_id(userId);
- duty.setDept_names(dept_names);
- List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
- 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)){//如果值为空,则跳过该行
- continue;
- }
- EcmDutyDetail detail = new EcmDutyDetail();
-
- //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);
- String duty_way = childSheet.getRow(r).getCell(4).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);
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
-
- childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
-
- childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
-
- childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
-
- childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
-
- childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
-
- childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
-
- datailList.add(detail);
-
- }
- workbook.close();
- EcmDutyDao dao = new EcmDutyDao();
- 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);
- EcmEmDuty duty = new EcmEmDuty();
-
- //************************检查是否匹配模版*****************************
- 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 checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
-
- childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
-
- childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
-
- childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
-
- childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
-
- childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
-
- childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
-
- childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
-
- // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
- // +checkDutyWay);
-
- if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
- || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
- || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
- || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
-
- 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_yyyyMMddHHmmss));
- duty.setDuty_type("2");
- duty.setRelease("0");
- duty.setDept_names(dept_names);
-
- List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
-
- for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
- EcmDutyDetail detail = new EcmDutyDetail();
-
- // 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);
-
- String duty_way = childSheet.getRow(r).getCell(4).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);
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
-
- childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
-
- childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
-
- childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
-
- childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
-
- childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
-
- childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
-
- datailList.add(detail);
- }
- workbook.close();
- EcmDutyDao dao = new EcmDutyDao();
- if(dao.saveDutyBatch(duty, datailList)){
- msg.setSucsess("true");
- }
- }
-
- //}
- } catch (Exception e) {
- throw new RuntimeException(e.getMessage());
- }
-
- return msg;
- }
-
- public Msg readExcel_bgd(String filePath,String oper_name, String oper_dept) {
- 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);
- EcmEmDuty duty = new EcmEmDuty();
-
- //************************检查是否匹配模版*****************************
- 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 checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
-
- childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
-
- childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
-
- childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
-
- childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
-
- childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
-
- childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
-
- childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyCar = childSheet.getRow(3).getCell(12).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(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_yyyyMMddHHmm));
- duty.setDuty_type("3");
-
- List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
- int rowCnt = childSheet.getLastRowNum();
- System.out.println("rowCnt"+ rowCnt);
- for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
- EcmDutyDetail detail = new EcmDutyDetail();
-
- //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);
- String duty_way = childSheet.getRow(r).getCell(4).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);
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
-
- childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
-
- childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
-
- childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
-
- childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
-
- childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
-
- childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
-
- datailList.add(detail);
-
- }
- workbook.close();
- EcmDutyDao dao = new EcmDutyDao();
- 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);
- EcmEmDuty duty = new EcmEmDuty();
-
- //************************检查是否匹配模版*****************************
- 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(6).setCellType(Cell.CELL_TYPE_STRING);
- String checkDutyWay = childSheet.getRow(3).getCell(6).getStringCellValue(); //值班方式
-
- childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
-
- childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
-
- childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
-
- childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
-
- childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
-
- childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
-
- childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
-
- childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
-
- if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
- || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
- || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
- || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
-
- 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("3");
-
- List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
-
- for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
- EcmDutyDetail detail = new EcmDutyDetail();
-
- // 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);
- String duty_way = childSheet.getRow(r).getCell(4).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);
-
- childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
-
- childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
- detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
-
- childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
-
- childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
- detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
-
- childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
-
- childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
-
- childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
-
- childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
- detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
-
- datailList.add(detail);
- }
- workbook.close();
- EcmDutyDao dao = new EcmDutyDao();
- 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;
- }
-
- }
|