78fafb5d261c138dc6f4d3d95fd4512656811f95.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package com.sinosoft.am.duty.records.dao;
  2. import java.io.FileInputStream;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import org.apache.poi.hssf.usermodel.HSSFCell;
  8. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.ss.usermodel.Cell;
  12. import org.apache.poi.xssf.usermodel.XSSFSheet;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14. import com.formaction.vo.Msg;
  15. import com.sinosoft.am.duty.records.vo.EcmAmDuty;
  16. import com.sinosoft.am.duty.records.vo.EcmDutyDetail1;
  17. import com.sinosoft.common.util.DateUtil;
  18. import com.sinosoft.lz.system.department.Department;
  19. import com.sinosoft.lz.system.department.DepartmentDao;
  20. public class AlertDutyExcel1 {
  21. public Msg readExcel(String filePath,String oper_name, String oper_dept, String userId) {
  22. Msg msg=new Msg();
  23. try {
  24. String excelType = filePath.substring(filePath.lastIndexOf(".") + 1);
  25. // Read the Excel 2003-2007
  26. if (excelType != null && !"".equalsIgnoreCase(excelType)
  27. && !"xlsx".equalsIgnoreCase(excelType)) {
  28. // 创建对Excel工作簿文件的引用
  29. HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
  30. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  31. // System.out.println("==========开始第 " + i
  32. // + " 个sheet============");
  33. //只读一页
  34. HSSFSheet childSheet = workbook.getSheetAt(0);
  35. EcmAmDuty duty = new EcmAmDuty();
  36. //************************检查是否匹配模版*****************************
  37. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  38. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  39. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  40. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  41. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  42. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  43. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  44. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  45. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  46. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  47. System.out.println("");
  48. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  49. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  50. childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  51. String checkPer = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班成员,联系电话
  52. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  53. String checkDutyWay = childSheet.getRow(3).getCell(5).getStringCellValue(); //值班方式
  54. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  55. // +checkPer+" ,"+checkPerTel+" ,"+checkDutyWay);
  56. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  57. || !"领导电话".equals(checkLeaderTel) || !"值班成员,联系电话".equals(checkPer) || !"值班方式".equals(checkDutyWay)) {
  58. msg.setSucsess("false");
  59. msg.setInfo("导入Excel文件与模版不匹配!");
  60. workbook.close();
  61. return msg;
  62. }
  63. //*********************************************************************
  64. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  65. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  66. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  67. String deptName = childSheet.getRow(2).getCell(4).getStringCellValue();
  68. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  69. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  70. //
  71. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  72. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  73. //
  74. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  75. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  76. duty.setName(name);
  77. String deptId = deptName;
  78. if(deptName!=null&&!"".equals(deptName)){
  79. DepartmentDao deptDao = new DepartmentDao();
  80. Department dept = deptDao.getDeptByName(deptName);
  81. if(dept!=null&&dept.getFd_objectid()!=null&&!"".equals(dept.getFd_objectid())){
  82. deptId = dept.getFd_objectid();
  83. }
  84. }
  85. duty.setDept_names(deptId);
  86. duty.setOper_name(oper_name);
  87. duty.setOper_dept(oper_dept);
  88. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmmss));
  89. duty.setDuty_type("1");
  90. duty.setOper_name_id(userId);
  91. List<EcmDutyDetail1> datailList = new ArrayList<EcmDutyDetail1>();
  92. int rowCnt = childSheet.getLastRowNum();
  93. System.out.println("rowCnt"+ rowCnt);
  94. for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
  95. HSSFCell cell = childSheet.getRow(r).getCell(0);
  96. String value = getValue(cell);
  97. if("".equals(value.trim())){//如果值为空,则跳过该行
  98. continue;
  99. }
  100. EcmDutyDetail1 detail = new EcmDutyDetail1();
  101. //childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  102. //detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  103. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  104. //System.out.println(childSheet.getRow(r).getCell(1).getStringCellValue());
  105. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  106. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  107. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  108. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  109. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  110. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  111. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  112. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  113. detail.setName(childSheet.getRow(r).getCell(4).getStringCellValue());
  114. //childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  115. //detail.setTelephone(childSheet.getRow(r).getCell(5).getStringCellValue());
  116. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  117. String duty_way = childSheet.getRow(r).getCell(5).getStringCellValue();
  118. String wayValue = "";
  119. if(duty_way.contains("在岗值班")){
  120. wayValue = "0";
  121. }
  122. if(duty_way.contains("电话值班")){
  123. wayValue = "1";
  124. }
  125. if(duty_way.contains("应急指挥中心值班")){
  126. wayValue = "2";
  127. }
  128. detail.setDuty_way(wayValue);
  129. datailList.add(detail);
  130. }
  131. workbook.close();
  132. EcmDutyDao1 dao = new EcmDutyDao1();
  133. if(dao.saveDutyBatch(duty, datailList)){
  134. msg.setSucsess("true");
  135. }
  136. // }
  137. } else { // Read the Excel 2010
  138. XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
  139. filePath));
  140. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  141. // System.out.println("==========开始第 " + i
  142. // + " 个sheet============");
  143. XSSFSheet childSheet = workbook.getSheetAt(0);
  144. EcmAmDuty duty = new EcmAmDuty();
  145. //************************检查是否匹配模版*****************************
  146. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  147. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  148. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  149. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  150. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  151. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  152. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  153. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  154. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  155. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  156. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  157. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  158. childSheet.getRow(3).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  159. String checkPer = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班成员
  160. //childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  161. //String checkPerTel = childSheet.getRow(3).getCell(5).getStringCellValue(); //成员电话
  162. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  163. String checkDutyWay = childSheet.getRow(3).getCell(5).getStringCellValue(); //值班方式
  164. System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  165. +checkPer+" ,"+checkDutyWay);
  166. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  167. || !"领导电话".equals(checkLeaderTel) || !"值班成员,联系电话".equals(checkPer) || !"值班方式".equals(checkDutyWay)) {
  168. msg.setSucsess("false");
  169. msg.setInfo("导入Excel文件与模版不匹配!");
  170. workbook.close();
  171. return msg;
  172. }
  173. //*********************************************************************
  174. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  175. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  176. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  177. String dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  178. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  179. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  180. //
  181. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  182. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  183. //
  184. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  185. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  186. duty.setName(name);
  187. duty.setDept_names(dept);
  188. duty.setOper_name(oper_name);
  189. duty.setOper_dept(oper_dept);
  190. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmm));
  191. duty.setDuty_type("1");
  192. List<EcmDutyDetail1> datailList = new ArrayList<EcmDutyDetail1>();
  193. for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
  194. EcmDutyDetail1 detail = new EcmDutyDetail1();
  195. // childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  196. // detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  197. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  198. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  199. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  200. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  201. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  202. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  203. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  204. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  205. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  206. detail.setName(childSheet.getRow(r).getCell(4).getStringCellValue());
  207. //childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  208. //detail.setTelephone(childSheet.getRow(r).getCell(5).getStringCellValue());
  209. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  210. String duty_way = childSheet.getRow(r).getCell(5).getStringCellValue();
  211. String wayValue = "";
  212. if(duty_way.contains("在岗值班")){
  213. wayValue = "0";
  214. }
  215. if(duty_way.contains("电话值班")){
  216. wayValue = "1";
  217. }
  218. if(duty_way.contains("应急指挥中心值班")){
  219. wayValue = "2";
  220. }
  221. detail.setDuty_way(wayValue);
  222. datailList.add(detail);
  223. }
  224. workbook.close();
  225. EcmDutyDao1 dao = new EcmDutyDao1();
  226. if(dao.saveDutyBatch(duty, datailList)){
  227. msg.setSucsess("true");
  228. }
  229. }
  230. //}
  231. } catch (Exception e) {
  232. throw new RuntimeException(e.getMessage());
  233. }
  234. return msg;
  235. }
  236. /**
  237. * 获取Excel中某个单元格的值
  238. *
  239. * @param cell
  240. * @return
  241. * @throws Exception
  242. */
  243. @SuppressWarnings("deprecation")
  244. public String getValue(HSSFCell cell) throws Exception {
  245. String value = "";
  246. switch (cell.getCellType()) {
  247. case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
  248. System.out.println("数字类型");
  249. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  250. // 如果是date类型则 ,获取该cell的date值
  251. value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
  252. .toString();
  253. java.util.Date date1 = new Date(value);
  254. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  255. value = format.format(date1);
  256. } else {// 纯数字
  257. value = String.valueOf(cell.getNumericCellValue()).split("[.]")[0];
  258. }
  259. break;
  260. /* 此行表示单元格的内容为string类型 */
  261. case HSSFCell.CELL_TYPE_STRING: // 字符串型
  262. value = cell.getStringCellValue();
  263. break;
  264. case HSSFCell.CELL_TYPE_FORMULA:// 公式型
  265. // 读公式计算值
  266. value = String.valueOf(cell.getNumericCellValue());
  267. if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
  268. value = cell.getStringCellValue().toString();
  269. }
  270. cell.getCellFormula();
  271. break;
  272. case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
  273. value = " " + cell.getBooleanCellValue();
  274. break;
  275. /* 此行表示该单元格值为空 */
  276. case HSSFCell.CELL_TYPE_BLANK: // 空值
  277. value = "";
  278. break;
  279. case HSSFCell.CELL_TYPE_ERROR: // 故障
  280. value = "";
  281. break;
  282. default:
  283. value = cell.getStringCellValue().toString();
  284. }
  285. if(checkValueTrim(value)){
  286. return "";
  287. }
  288. return value;
  289. }
  290. private boolean checkValueTrim(String value) {
  291. boolean flagg = false;
  292. if("".equals(value.trim())){
  293. flagg = true;
  294. }
  295. return flagg;
  296. }
  297. }