0aadc16cf8b7b20bf112361ed86ef1a8a588989f.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package com.sinosoft.em.alert.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.common.util.DateUtil;
  16. import com.sinosoft.em.alert.duty.records.vo.EcmDutyDetail;
  17. import com.sinosoft.em.alert.duty.records.vo.EcmEmDuty;
  18. import com.sinosoft.lz.system.department.Department;
  19. import com.sinosoft.lz.system.department.DepartmentDao;
  20. public class AlertDutyExcel2 {
  21. public Msg readExcel(String filePath,String oper_name, String oper_dept, String userId, String dept_names) {
  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. EcmEmDuty duty = new EcmEmDuty();
  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(4).setCellType(Cell.CELL_TYPE_STRING);
  53. String checkDutyWay = childSheet.getRow(3).getCell(4).getStringCellValue(); //值班方式
  54. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  55. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  56. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  57. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  58. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  59. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  60. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  61. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  62. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  63. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  64. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  65. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  66. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  67. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  68. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  69. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  70. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  71. // +checkPer+" ,"+checkPerTel+" ,"+checkDutyWay);
  72. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  73. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
  74. || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
  75. || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
  76. msg.setSucsess("false");
  77. msg.setInfo("导入Excel文件与模版不匹配!");
  78. workbook.close();
  79. return msg;
  80. }
  81. //*********************************************************************
  82. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  83. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  84. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  85. String deptName = childSheet.getRow(2).getCell(4).getStringCellValue();
  86. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  87. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  88. //
  89. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  90. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  91. //
  92. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  93. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  94. duty.setName(name);
  95. String deptId = deptName;
  96. if(deptName!=null&&!"".equals(deptName)){
  97. DepartmentDao deptDao = new DepartmentDao();
  98. Department dept = deptDao.getDeptByName(deptName);
  99. if(dept!=null&&dept.getFd_objectid()!=null&&!"".equals(dept.getFd_objectid())){
  100. deptId = dept.getFd_objectid();
  101. }
  102. }
  103. duty.setDept_names(deptId);
  104. duty.setOper_name(oper_name);
  105. duty.setOper_dept(oper_dept);
  106. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmmss));
  107. duty.setDuty_type("1");
  108. duty.setRelease("0");
  109. duty.setOper_name_id(userId);
  110. duty.setDept_names(dept_names);
  111. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  112. int rowCnt = childSheet.getLastRowNum();
  113. System.out.println("rowCnt"+ rowCnt);
  114. for (int r = 4; r <= rowCnt; r++) {// 循环该 子sheet row
  115. HSSFCell cell = childSheet.getRow(r).getCell(0);
  116. String value = getValue(cell);
  117. if("".equals(value.trim())){//如果值为空,则跳过该行
  118. continue;
  119. }
  120. EcmDutyDetail detail = new EcmDutyDetail();
  121. //childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  122. //detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  123. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  124. //System.out.println(childSheet.getRow(r).getCell(1).getStringCellValue());
  125. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  126. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  127. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  128. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  129. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  130. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  131. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  132. //childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  133. //detail.setTelephone(childSheet.getRow(r).getCell(5).getStringCellValue());
  134. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  135. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  136. String wayValue = "";
  137. if(duty_way.contains("在岗值班")){
  138. wayValue = "0";
  139. }
  140. if(duty_way.contains("电话值班")){
  141. wayValue = "1";
  142. }
  143. if(duty_way.contains("应急指挥中心值班")){
  144. wayValue = "2";
  145. }
  146. detail.setDuty_way(wayValue);
  147. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  148. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  149. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  150. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  151. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  152. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  153. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  154. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  155. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  156. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  157. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  158. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  159. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  160. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  161. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  162. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  163. datailList.add(detail);
  164. }
  165. workbook.close();
  166. EcmDutyDao dao = new EcmDutyDao();
  167. if(dao.saveDutyBatch(duty, datailList)){
  168. msg.setSucsess("true");
  169. }
  170. // }
  171. } else { // Read the Excel 2010
  172. XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
  173. filePath));
  174. // for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet
  175. // System.out.println("==========开始第 " + i
  176. // + " 个sheet============");
  177. XSSFSheet childSheet = workbook.getSheetAt(0);
  178. EcmEmDuty duty = new EcmEmDuty();
  179. //************************检查是否匹配模版*****************************
  180. childSheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  181. String checkName=childSheet.getRow(2).getCell(0).getStringCellValue(); // 名称
  182. childSheet.getRow(2).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  183. String checkDeptName = childSheet.getRow(2).getCell(3).getStringCellValue(); //部门
  184. childSheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  185. String checkStartTime = childSheet.getRow(3).getCell(0).getStringCellValue(); //开始时间
  186. childSheet.getRow(3).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  187. String checkEndTime = childSheet.getRow(3).getCell(1).getStringCellValue(); //结束时间
  188. childSheet.getRow(3).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  189. String checkLeader = childSheet.getRow(3).getCell(2).getStringCellValue(); //值班领导
  190. childSheet.getRow(3).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  191. String checkLeaderTel = childSheet.getRow(3).getCell(3).getStringCellValue(); //领导电话
  192. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  193. String checkDutyWay = childSheet.getRow(3).getCell(5).getStringCellValue(); //值班方式
  194. childSheet.getRow(3).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  195. String checkAdministrationDuty = childSheet.getRow(3).getCell(5).getStringCellValue(); //行政值班
  196. childSheet.getRow(3).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  197. String checkAdministrationDutyTel = childSheet.getRow(3).getCell(6).getStringCellValue(); //行政值班电话
  198. childSheet.getRow(3).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  199. String checkEveryDayDuty = childSheet.getRow(3).getCell(7).getStringCellValue(); //生产值班
  200. childSheet.getRow(3).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  201. String checkEveryDayDutyTel = childSheet.getRow(3).getCell(8).getStringCellValue(); //生产值班电话
  202. childSheet.getRow(3).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  203. String checkSpotDuty = childSheet.getRow(3).getCell(9).getStringCellValue(); //现场值班
  204. childSheet.getRow(3).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  205. String checkSpotDutyTel = childSheet.getRow(3).getCell(10).getStringCellValue(); //现场值班电话
  206. childSheet.getRow(3).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  207. String checkSpotDutyAddress = childSheet.getRow(3).getCell(11).getStringCellValue(); //现场值班地点
  208. childSheet.getRow(3).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  209. String checkSpotDutyCar = childSheet.getRow(3).getCell(12).getStringCellValue(); //现场值班车辆
  210. // System.out.println(checkName +" ,"+ checkDeptName+" ,"+checkStartTime+" ,"+checkEndTime+" ,"+checkLeader+" ,"+checkLeaderTel+" ,"
  211. // +checkDutyWay);
  212. if(!"名称".equals(checkName) || !"部门".equals(checkDeptName) || !"开始时间".equals(checkStartTime) || !"结束时间".equals(checkEndTime) || !"值班领导".equals(checkLeader)
  213. || !"领导电话".equals(checkLeaderTel) || !"值班方式".equals(checkDutyWay) || !"行政值班".equals(checkAdministrationDuty) || !"行政值班电话".equals(checkAdministrationDutyTel)
  214. || !"生产值班".equals(checkEveryDayDuty) || !"生产值班电话".equals(checkEveryDayDutyTel) || !"现场值班".equals(checkSpotDuty) || !"现场值班电话".equals(checkSpotDutyTel)
  215. || !"现场值班地点".equals(checkSpotDutyAddress) || !"现场值班车辆".equals(checkSpotDutyCar)) {
  216. msg.setSucsess("false");
  217. msg.setInfo("导入Excel文件与模版不匹配!");
  218. workbook.close();
  219. return msg;
  220. }
  221. //*********************************************************************
  222. childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING); //按照String类型读取
  223. String name = childSheet.getRow(2).getCell(1).getStringCellValue();
  224. childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING); //
  225. String dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  226. // childSheet.getRow(2).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  227. // String oper_name = childSheet.getRow(2).getCell(1).getStringCellValue();
  228. //
  229. // childSheet.getRow(2).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  230. // String oper_dept = childSheet.getRow(2).getCell(4).getStringCellValue();
  231. //
  232. // childSheet.getRow(2).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  233. // String oper_time = childSheet.getRow(2).getCell(7).getStringCellValue();
  234. duty.setName(name);
  235. duty.setDept_names(dept);
  236. duty.setOper_name(oper_name);
  237. duty.setOper_dept(oper_dept);
  238. duty.setOper_time(DateUtil.format(new Date(), DateUtil.formatStr_yyyyMMddHHmm));
  239. duty.setDuty_type("1");
  240. duty.setRelease("0");
  241. duty.setDept_names(dept_names);
  242. List<EcmDutyDetail> datailList = new ArrayList<EcmDutyDetail>();
  243. for (int r = 4; r < childSheet.getPhysicalNumberOfRows(); r++) {// 循环该 子sheet row
  244. EcmDutyDetail detail = new EcmDutyDetail();
  245. // childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  246. // detail.setDuty_date(childSheet.getRow(r).getCell(0).getStringCellValue());
  247. childSheet.getRow(r).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
  248. detail.setStart_time(childSheet.getRow(r).getCell(0).getStringCellValue());
  249. childSheet.getRow(r).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
  250. detail.setEnd_time(childSheet.getRow(r).getCell(1).getStringCellValue());
  251. childSheet.getRow(r).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
  252. detail.setLeader(childSheet.getRow(r).getCell(2).getStringCellValue());
  253. childSheet.getRow(r).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
  254. detail.setLeader_tel(childSheet.getRow(r).getCell(3).getStringCellValue());
  255. childSheet.getRow(r).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
  256. String duty_way = childSheet.getRow(r).getCell(4).getStringCellValue();
  257. String wayValue = "";
  258. if(duty_way.contains("在岗值班")){
  259. wayValue = "0";
  260. }
  261. if(duty_way.contains("电话值班")){
  262. wayValue = "1";
  263. }
  264. if(duty_way.contains("应急指挥中心值班")){
  265. wayValue = "2";
  266. }
  267. detail.setDuty_way(wayValue);
  268. childSheet.getRow(r).getCell(5).setCellType(Cell.CELL_TYPE_STRING);
  269. detail.setAdministration_duty(childSheet.getRow(r).getCell(5).getStringCellValue());
  270. childSheet.getRow(r).getCell(6).setCellType(Cell.CELL_TYPE_STRING);
  271. detail.setAdministration_duty_tel(childSheet.getRow(r).getCell(6).getStringCellValue());
  272. childSheet.getRow(r).getCell(7).setCellType(Cell.CELL_TYPE_STRING);
  273. detail.setEveryday_duty(childSheet.getRow(r).getCell(7).getStringCellValue());
  274. childSheet.getRow(r).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
  275. detail.setEveryday_duty_tel(childSheet.getRow(r).getCell(8).getStringCellValue());
  276. childSheet.getRow(r).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
  277. detail.setSpot_duty(childSheet.getRow(r).getCell(9).getStringCellValue());
  278. childSheet.getRow(r).getCell(10).setCellType(Cell.CELL_TYPE_STRING);
  279. detail.setSpot_duty_tel(childSheet.getRow(r).getCell(10).getStringCellValue());
  280. childSheet.getRow(r).getCell(11).setCellType(Cell.CELL_TYPE_STRING);
  281. detail.setSpot_duty_address(childSheet.getRow(r).getCell(11).getStringCellValue());
  282. childSheet.getRow(r).getCell(12).setCellType(Cell.CELL_TYPE_STRING);
  283. detail.setSpot_duty_car(childSheet.getRow(r).getCell(12).getStringCellValue());
  284. datailList.add(detail);
  285. }
  286. workbook.close();
  287. EcmDutyDao dao = new EcmDutyDao();
  288. if(dao.saveDutyBatch(duty, datailList)){
  289. msg.setSucsess("true");
  290. }
  291. }
  292. //}
  293. } catch (Exception e) {
  294. throw new RuntimeException(e.getMessage());
  295. }
  296. return msg;
  297. }
  298. /**
  299. * 获取Excel中某个单元格的值
  300. *
  301. * @param cell
  302. * @return
  303. * @throws Exception
  304. */
  305. @SuppressWarnings("deprecation")
  306. public String getValue(HSSFCell cell) throws Exception {
  307. String value = "";
  308. switch (cell.getCellType()) {
  309. case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
  310. System.out.println("数字类型");
  311. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  312. // 如果是date类型则 ,获取该cell的date值
  313. value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
  314. .toString();
  315. java.util.Date date1 = new Date(value);
  316. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  317. value = format.format(date1);
  318. } else {// 纯数字
  319. value = String.valueOf(cell.getNumericCellValue()).split("[.]")[0];
  320. }
  321. break;
  322. /* 此行表示单元格的内容为string类型 */
  323. case HSSFCell.CELL_TYPE_STRING: // 字符串型
  324. value = cell.getStringCellValue();
  325. break;
  326. case HSSFCell.CELL_TYPE_FORMULA:// 公式型
  327. // 读公式计算值
  328. value = String.valueOf(cell.getNumericCellValue());
  329. if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
  330. value = cell.getStringCellValue().toString();
  331. }
  332. cell.getCellFormula();
  333. break;
  334. case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
  335. value = " " + cell.getBooleanCellValue();
  336. break;
  337. /* 此行表示该单元格值为空 */
  338. case HSSFCell.CELL_TYPE_BLANK: // 空值
  339. value = "";
  340. break;
  341. case HSSFCell.CELL_TYPE_ERROR: // 故障
  342. value = "";
  343. break;
  344. default:
  345. value = cell.getStringCellValue().toString();
  346. }
  347. if(checkValueTrim(value)){
  348. return "";
  349. }
  350. return value;
  351. }
  352. private boolean checkValueTrim(String value) {
  353. boolean flagg = false;
  354. if("".equals(value.trim())){
  355. flagg = true;
  356. }
  357. return flagg;
  358. }
  359. }