e5d9ffbe53f9401c1fef2b8413db3723ac0723d2.svn-base 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. package com.sinosoft.em.alert.duty.records.dao;
  2. import java.io.FileInputStream;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.xssf.usermodel.XSSFSheet;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import com.formaction.vo.Msg;
  21. import com.persistence.DbConnection;
  22. import com.sinosoft.common.util.DateUtil;
  23. import com.sinosoft.em.alert.duty.records.vo.EcmDutyDetail;
  24. import com.sinosoft.em.alert.duty.records.vo.EcmEmDuty;
  25. import com.sinosoft.lz.system.department.Department;
  26. import com.sinosoft.lz.system.department.DepartmentDao;
  27. public class EcmDutyExcel2 {
  28. public Msg readExcel(String filePath,String oper_name, String oper_dept, String userId,String dept_names) {
  29. Msg msg=new Msg();
  30. try {
  31. String excelType = filePath.substring(filePath.lastIndexOf(".") + 1);
  32. // Read the Excel 2003-2007
  33. if (excelType != null && !"".equalsIgnoreCase(excelType)
  34. && !"xlsx".equalsIgnoreCase(excelType)) {
  35. // 创建对Excel工作簿文件的引用
  36. HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
  37. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  38. // System.out.println("==========开始第 " + i
  39. // + " 个sheet============");
  40. //只读一页
  41. HSSFSheet childSheet = workbook.getSheetAt(0);
  42. EcmEmDuty duty = new EcmEmDuty();
  43. //************************检查是否匹配模版*****************************
  44. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  45. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  46. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  47. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  48. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  49. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  50. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  51. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  52. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  53. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  54. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  55. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  56. childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  57. String checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
  58. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  59. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  60. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  61. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  62. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  63. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  64. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  65. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  66. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  67. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  68. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  69. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  70. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  71. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  72. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  73. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  74. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  75. // +checkPer+" ,"+checkPerTel+" ,"+checkDutyWay);
  76. /* if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  77. || !"领导电话".equals(checkLeaderTel) || !"值班成员".equals(checkPer) || !"联系电话".equals(checkPerTel) || !"值班方式".equals(checkDutyWay)) {
  78. msg.setSucsess("false");
  79. msg.setInfo("导入Excel文件与模版不匹配!");
  80. workbook.close();
  81. return msg;
  82. }*/
  83. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  84. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
  85. || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
  86. || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
  87. msg.setSucsess("false");
  88. msg.setInfo("导入Excel文件与模版不匹配!");
  89. workbook.close();
  90. return msg;
  91. }
  92. //*********************************************************************
  93. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  94. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  95. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  96. String deptName = childSheet.getRow(2).getCell(4).getStringCellValue();
  97. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  98. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  99. //
  100. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  101. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  102. //
  103. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  104. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  105. duty.setName(name);
  106. String deptId = deptName;
  107. if(deptName!=null&&!"".equals(deptName)){
  108. DepartmentDao deptDao = new DepartmentDao();
  109. Department dept = deptDao.getDeptByName(deptName);
  110. if(dept!=null&&dept.getFd_objectid()!=null&&!"".equals(dept.getFd_objectid())){
  111. deptId = dept.getFd_objectid();
  112. }
  113. }
  114. duty.setDept_names(deptId);
  115. duty.setOper_name(oper_name);
  116. duty.setOper_dept(oper_dept);
  117. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmmss));
  118. duty.setDuty_type("2");
  119. duty.setRelease("0");
  120. duty.setOper_name_id(userId);
  121. duty.setDept_names(dept_names);
  122. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  123. int rowCnt = childSheet.getLastRowNum();
  124. System.out.println("rowCnt"+ rowCnt);
  125. for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
  126. HSSFCell cell = childSheet.getRow(r).getCell(0);
  127. String value = getValue(cell);
  128. if("".equals(value)){//如果值为空,则跳过该行
  129. continue;
  130. }
  131. EcmDutyDetail detail = new EcmDutyDetail();
  132. //childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  133. //detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  134. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  135. //System.out.println(childSheet.getRow(r).getCell(1).getStringCellValue());
  136. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  137. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  138. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  139. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  140. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  141. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  142. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  143. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  144. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  145. String wayValue = "";
  146. if(duty_way.contains("在岗值班")){
  147. wayValue = "0";
  148. }
  149. if(duty_way.contains("电话值班")){
  150. wayValue = "1";
  151. }
  152. if(duty_way.contains("应急指挥中心值班")){
  153. wayValue = "2";
  154. }
  155. detail.setDuty_way(wayValue);
  156. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  157. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  158. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  159. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  160. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  161. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  162. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  163. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  164. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  165. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  166. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  167. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  168. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  169. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  170. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  171. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  172. datailList.add(detail);
  173. }
  174. workbook.close();
  175. EcmDutyDao dao = new EcmDutyDao();
  176. if(dao.saveDutyBatch(duty, datailList)){
  177. msg.setSucsess("true");
  178. }
  179. // }
  180. } else { // Read the Excel 2010
  181. XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
  182. filePath));
  183. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  184. // System.out.println("==========开始第 " + i
  185. // + " 个sheet============");
  186. XSSFSheet childSheet = workbook.getSheetAt(0);
  187. EcmEmDuty duty = new EcmEmDuty();
  188. //************************检查是否匹配模版*****************************
  189. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  190. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  191. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  192. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  193. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  194. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  195. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  196. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  197. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  198. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  199. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  200. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  201. childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  202. String checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
  203. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  204. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  205. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  206. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  207. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  208. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  209. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  210. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  211. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  212. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  213. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  214. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  215. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  216. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  217. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  218. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  219. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  220. // +checkDutyWay);
  221. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  222. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
  223. || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
  224. || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
  225. msg.setSucsess("false");
  226. msg.setInfo("导入Excel文件与模版不匹配!");
  227. workbook.close();
  228. return msg;
  229. }
  230. //*********************************************************************
  231. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  232. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  233. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  234. String dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  235. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  236. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  237. //
  238. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  239. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  240. //
  241. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  242. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  243. duty.setName(name);
  244. duty.setDept_names(dept);
  245. duty.setOper_name(oper_name);
  246. duty.setOper_dept(oper_dept);
  247. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmmss));
  248. duty.setDuty_type("2");
  249. duty.setRelease("0");
  250. duty.setDept_names(dept_names);
  251. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  252. for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
  253. EcmDutyDetail detail = new EcmDutyDetail();
  254. // childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  255. // detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  256. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  257. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  258. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  259. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  260. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  261. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  262. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  263. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  264. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  265. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  266. String wayValue = "";
  267. if(duty_way.contains("在岗值班")){
  268. wayValue = "0";
  269. }
  270. if(duty_way.contains("电话值班")){
  271. wayValue = "1";
  272. }
  273. if(duty_way.contains("应急指挥中心值班")){
  274. wayValue = "2";
  275. }
  276. detail.setDuty_way(wayValue);
  277. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  278. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  279. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  280. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  281. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  282. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  283. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  284. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  285. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  286. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  287. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  288. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  289. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  290. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  291. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  292. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  293. datailList.add(detail);
  294. }
  295. workbook.close();
  296. EcmDutyDao dao = new EcmDutyDao();
  297. if(dao.saveDutyBatch(duty, datailList)){
  298. msg.setSucsess("true");
  299. }
  300. }
  301. //}
  302. } catch (Exception e) {
  303. throw new RuntimeException(e.getMessage());
  304. }
  305. return msg;
  306. }
  307. public Msg readExcel_bgd(String filePath,String oper_name, String oper_dept) {
  308. Msg msg=new Msg();
  309. try {
  310. String excelType = filePath.substring(filePath.lastIndexOf(".") + 1);
  311. // Read the Excel 2003-2007
  312. if (excelType != null && !"".equalsIgnoreCase(excelType)
  313. && !"xlsx".equalsIgnoreCase(excelType)) {
  314. // 创建对Excel工作簿文件的引用
  315. HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
  316. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  317. // System.out.println("==========开始第 " + i
  318. // + " 个sheet============");
  319. //只读一页
  320. HSSFSheet childSheet = workbook.getSheetAt(0);
  321. EcmEmDuty duty = new EcmEmDuty();
  322. //************************检查是否匹配模版*****************************
  323. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  324. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  325. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  326. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  327. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  328. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  329. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  330. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  331. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  332. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  333. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  334. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  335. childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  336. String checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
  337. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  338. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  339. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  340. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  341. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  342. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  343. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  344. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  345. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  346. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  347. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  348. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  349. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  350. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  351. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  352. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  353. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  354. // +checkPer+" ,"+checkPerTel+" ,"+checkDutyWay);
  355. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  356. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay)) {
  357. msg.setSucsess("false");
  358. msg.setInfo("导入Excel文件与模版不匹配!");
  359. workbook.close();
  360. return msg;
  361. }
  362. //*********************************************************************
  363. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  364. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  365. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  366. String deptName = childSheet.getRow(2).getCell(4).getStringCellValue();
  367. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  368. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  369. //
  370. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  371. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  372. //
  373. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  374. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  375. duty.setName(name);
  376. String deptId = deptName;
  377. if(deptName!=null&&!"".equals(deptName)){
  378. DepartmentDao deptDao = new DepartmentDao();
  379. Department dept = deptDao.getDeptByName(deptName);
  380. if(dept!=null&&dept.getFd_objectid()!=null&&!"".equals(dept.getFd_objectid())){
  381. deptId = dept.getFd_objectid();
  382. }
  383. }
  384. duty.setDept_names(deptId);
  385. duty.setOper_name(oper_name);
  386. duty.setOper_dept(oper_dept);
  387. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmm));
  388. duty.setDuty_type("3");
  389. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  390. int rowCnt = childSheet.getLastRowNum();
  391. System.out.println("rowCnt"+ rowCnt);
  392. for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
  393. EcmDutyDetail detail = new EcmDutyDetail();
  394. //childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  395. //detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  396. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  397. //System.out.println(childSheet.getRow(r).getCell(1).getStringCellValue());
  398. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  399. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  400. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  401. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  402. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  403. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  404. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  405. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  406. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  407. String wayValue = "";
  408. if(duty_way.contains("在岗值班")){
  409. wayValue = "0";
  410. }
  411. if(duty_way.contains("电话值班")){
  412. wayValue = "1";
  413. }
  414. if(duty_way.contains("应急指挥中心值班")){
  415. wayValue = "2";
  416. }
  417. detail.setDuty_way(wayValue);
  418. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  419. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  420. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  421. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  422. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  423. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  424. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  425. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  426. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  427. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  428. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  429. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  430. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  431. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  432. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  433. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  434. datailList.add(detail);
  435. }
  436. workbook.close();
  437. EcmDutyDao dao = new EcmDutyDao();
  438. if(dao.saveDutyBatch(duty, datailList)){
  439. msg.setSucsess("true");
  440. }
  441. // }
  442. } else { // Read the Excel 2010
  443. XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
  444. filePath));
  445. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  446. // System.out.println("==========开始第 " + i
  447. // + " 个sheet============");
  448. XSSFSheet childSheet = workbook.getSheetAt(0);
  449. EcmEmDuty duty = new EcmEmDuty();
  450. //************************检查是否匹配模版*****************************
  451. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  452. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  453. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  454. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  455. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  456. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  457. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  458. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  459. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  460. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  461. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  462. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  463. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  464. String checkDutyWay = childSheet.getRow(3).getCell(6).getStringCellValue(); //值班方式
  465. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  466. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  467. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  468. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  469. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  470. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  471. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  472. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  473. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  474. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  475. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  476. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  477. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  478. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  479. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  480. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  481. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  482. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
  483. || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
  484. || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
  485. msg.setSucsess("false");
  486. msg.setInfo("导入Excel文件与模版不匹配!");
  487. workbook.close();
  488. return msg;
  489. }
  490. //*********************************************************************
  491. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  492. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  493. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  494. String dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  495. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  496. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  497. //
  498. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  499. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  500. //
  501. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  502. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  503. duty.setName(name);
  504. duty.setDept_names(dept);
  505. duty.setOper_name(oper_name);
  506. duty.setOper_dept(oper_dept);
  507. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmm));
  508. duty.setDuty_type("3");
  509. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  510. for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
  511. EcmDutyDetail detail = new EcmDutyDetail();
  512. // childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  513. // detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  514. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  515. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  516. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  517. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  518. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  519. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  520. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  521. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  522. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  523. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  524. String wayValue = "";
  525. if(duty_way.contains("在岗值班")){
  526. wayValue = "0";
  527. }
  528. if(duty_way.contains("电话值班")){
  529. wayValue = "1";
  530. }
  531. if(duty_way.contains("应急指挥中心值班")){
  532. wayValue = "2";
  533. }
  534. detail.setDuty_way(wayValue);
  535. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  536. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  537. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  538. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  539. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  540. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  541. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  542. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  543. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  544. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  545. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  546. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  547. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  548. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  549. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  550. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  551. datailList.add(detail);
  552. }
  553. workbook.close();
  554. EcmDutyDao dao = new EcmDutyDao();
  555. if(dao.saveDutyBatch(duty, datailList)){
  556. msg.setSucsess("true");
  557. }
  558. }
  559. //}
  560. } catch (Exception e) {
  561. throw new RuntimeException(e.getMessage());
  562. }
  563. return msg;
  564. }
  565. /**
  566. * 获取Excel中某个单元格的值
  567. *
  568. * @param cell
  569. * @return
  570. * @throws Exception
  571. */
  572. @SuppressWarnings("deprecation")
  573. public String getValue(HSSFCell cell) throws Exception {
  574. String value = "";
  575. switch (cell.getCellType()) {
  576. case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
  577. System.out.println("数字类型");
  578. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  579. // 如果是date类型则 ,获取该cell的date值
  580. value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
  581. .toString();
  582. java.util.Date date1 = new Date(value);
  583. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  584. value = format.format(date1);
  585. } else {// 纯数字
  586. value = String.valueOf(cell.getNumericCellValue()).split("[.]")[0];
  587. }
  588. break;
  589. /* 此行表示单元格的内容为string类型 */
  590. case HSSFCell.CELL_TYPE_STRING: // 字符串型
  591. value = cell.getStringCellValue();
  592. break;
  593. case HSSFCell.CELL_TYPE_FORMULA:// 公式型
  594. // 读公式计算值
  595. value = String.valueOf(cell.getNumericCellValue());
  596. if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
  597. value = cell.getStringCellValue().toString();
  598. }
  599. cell.getCellFormula();
  600. break;
  601. case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
  602. value = " " + cell.getBooleanCellValue();
  603. break;
  604. /* 此行表示该单元格值为空 */
  605. case HSSFCell.CELL_TYPE_BLANK: // 空值
  606. value = "";
  607. break;
  608. case HSSFCell.CELL_TYPE_ERROR: // 故障
  609. value = "";
  610. break;
  611. default:
  612. value = cell.getStringCellValue().toString();
  613. }
  614. if(checkValueTrim(value)){
  615. return "";
  616. }
  617. return value;
  618. }
  619. private boolean checkValueTrim(String value) {
  620. boolean flagg = false;
  621. if("".equals(value.trim())){
  622. flagg = true;
  623. }
  624. return flagg;
  625. }
  626. }