ExcelUtil.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. package com.iamberry.rst.utils;
  2. import com.iamberry.rst.core.order.EfastOrder;
  3. import com.iamberry.rst.core.order.ProductColor;
  4. import com.iamberry.rst.core.pts.PtsBomComponents;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.poi.hssf.usermodel.*;
  7. import org.apache.poi.ss.usermodel.*;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import org.slf4j.LoggerFactory;
  10. import java.io.*;
  11. import java.text.DecimalFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.*;
  14. /**
  15. * Excel工具类
  16. *
  17. * @author 献
  18. * @company 深圳爱贝源科技有限公司
  19. * @website www.iamberry.com
  20. * @tel 18271840547
  21. * @date 2017/5/25
  22. */
  23. public class ExcelUtil {
  24. private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
  25. /**
  26. * 读取Excel文件头
  27. *
  28. * @param filePath
  29. * @return
  30. */
  31. public static List<String> readExcelHead(String filePath) throws IOException {
  32. List<String> heads = new ArrayList<String>();
  33. // 判断文件是否存在
  34. File file = new File(filePath);
  35. if (!file.exists()) {
  36. return null;
  37. }
  38. // 获取Workbook
  39. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  40. Workbook wb = null;
  41. if (filePath.endsWith("xls")) {
  42. wb = new HSSFWorkbook(inputStream);
  43. } else {
  44. wb = new XSSFWorkbook(inputStream);
  45. }
  46. inputStream.close();
  47. // 获取Sheet
  48. Sheet sheet = wb.getSheetAt(0);
  49. // 获取第一行数据
  50. Row row = sheet.rowIterator().next();
  51. Iterator<Cell> cells = row.cellIterator();
  52. // 获得第一行的迭代器,并遍历cell
  53. while (cells.hasNext()) {
  54. Cell cell = cells.next();
  55. heads.add(cell.getStringCellValue());
  56. }
  57. return heads;
  58. }
  59. /**
  60. * 读取Excel内容
  61. * 去除重复
  62. *
  63. * @param filePath
  64. * @return
  65. */
  66. public static List<String> readCell(String filePath, String cellName) throws IOException {
  67. // 判断文件是否存在
  68. File file = new File(filePath);
  69. if (!file.exists()) {
  70. return null;
  71. }
  72. // 获取Workbook
  73. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  74. Workbook wb = null;
  75. if (filePath.endsWith("xls")) {
  76. wb = new HSSFWorkbook(inputStream);
  77. } else {
  78. wb = new XSSFWorkbook(inputStream);
  79. }
  80. inputStream.close();
  81. // 获取Sheet
  82. Sheet sheet = wb.getSheetAt(0);
  83. Iterator<Row> rows = sheet.rowIterator();
  84. // 获取第一行的数据,匹配到表头
  85. int cellNum = -1;
  86. Row row = rows.next();
  87. Iterator<Cell> cells = row.cellIterator();
  88. while (cells.hasNext()) {
  89. Cell cell = cells.next();
  90. if (cellName.trim().equals(cell.getStringCellValue().trim())) {
  91. cellNum = cell.getColumnIndex();
  92. break;
  93. }
  94. }
  95. if (cellNum == -1) return null;
  96. // 读取数据
  97. Map<String, String> map = new HashMap<>();
  98. while (rows.hasNext()) {
  99. Row temp = rows.next();
  100. Cell cell = temp.getCell(cellNum);
  101. // 非空的列需要使用
  102. if (cell == null) {
  103. continue;
  104. }
  105. String name = getValue(cell);
  106. if (name != null && !"".equals(name.trim())) {
  107. String cellValue = getValue(cell);
  108. if (map.get(cellValue) == null) {
  109. map.put(cellValue, cellValue);
  110. }
  111. }
  112. }
  113. // Map to List
  114. Collection<String> valueCollection = map.values();
  115. return new ArrayList<String>(valueCollection);
  116. }
  117. /**
  118. * 读取Excel内容
  119. * 读取bom单excel的内容
  120. *
  121. * @param filePath
  122. * @return
  123. */
  124. public static Map<String, Object> readExcelBom(String filePath) throws IOException {
  125. // 判断文件是否存在
  126. File file = new File(filePath);
  127. if (!file.exists()) {
  128. return null;
  129. }
  130. // 获取Workbook
  131. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  132. Workbook wb = null;
  133. if (filePath.endsWith("xls")) {
  134. wb = new HSSFWorkbook(inputStream);
  135. } else {
  136. wb = new XSSFWorkbook(inputStream);
  137. }
  138. inputStream.close();
  139. // 获取Sheet
  140. Sheet sheet = wb.getSheetAt(0);
  141. //获取bom单名称
  142. Row rows = sheet.getRow(1);// 获取行
  143. Cell cell = null;
  144. cell = rows.getCell(1);//列
  145. String bomName = "";
  146. if(cell != null){
  147. bomName = cell.getStringCellValue();
  148. }
  149. //获取产品名称
  150. rows = sheet.getRow(2);// 获取行
  151. cell = rows.getCell(1);//列
  152. String produceName = "";
  153. if(cell != null){
  154. produceName = cell.getStringCellValue();
  155. }
  156. //硬件版本号
  157. rows = sheet.getRow(3);// 获取行
  158. cell = rows.getCell(1);//列
  159. String bomVersion = "";
  160. if(cell != null){
  161. bomVersion = cell.getStringCellValue();
  162. }
  163. //软件版本号
  164. rows = sheet.getRow(4);// 获取行
  165. cell = rows.getCell(1);//列
  166. String machineVersionNo = "";
  167. if(cell != null){
  168. machineVersionNo = cell.getStringCellValue();
  169. }
  170. //备注
  171. rows = sheet.getRow(5);// 获取行
  172. cell = rows.getCell(1);//列
  173. String bomRemarks = "";
  174. if(cell != null){
  175. bomRemarks = cell.getStringCellValue();
  176. }
  177. List<PtsBomComponents> componentsList = new ArrayList<PtsBomComponents>();
  178. Iterator<Row> rowss = sheet.rowIterator();
  179. int i = 0;
  180. while (rowss.hasNext()) {
  181. Row ro = rowss.next();
  182. if(i>7){
  183. Iterator<Cell> cells = ro.cellIterator();
  184. if(ro == null){
  185. break;
  186. }
  187. int j=0;
  188. PtsBomComponents bomComponents = new PtsBomComponents();
  189. String componentsNo = "";
  190. String bomComponentsQuantity = "";
  191. while (cells.hasNext()) {
  192. Cell ce = cells.next();
  193. if (ce == null){
  194. break;
  195. }
  196. if(j == 0){
  197. componentsNo = ce.getStringCellValue();
  198. }else if(j == 1){
  199. bomComponentsQuantity = ce.getStringCellValue();
  200. }
  201. j++;
  202. }
  203. bomComponents.setComponentsNo(componentsNo);
  204. bomComponents.setBomComponentsQuantity(Integer.valueOf(bomComponentsQuantity) * 100);
  205. componentsList.add(bomComponents);
  206. }
  207. i++;
  208. }
  209. Map<String,Object> map = new HashMap<String, Object>();
  210. map.put("bomName",bomName);
  211. map.put("produceName",produceName);
  212. map.put("bomVersion",bomVersion);
  213. map.put("machineVersionNo",machineVersionNo);
  214. map.put("bomRemarks",bomRemarks);
  215. map.put("componentsList",componentsList);
  216. return map;
  217. }
  218. /**
  219. * 读取Excel内容
  220. * 不去除重复
  221. *
  222. * @param filePath
  223. * @return
  224. */
  225. public static List<String> readCell2(String filePath, String cellName) throws IOException {
  226. // 判断文件是否存在
  227. File file = new File(filePath);
  228. if (!file.exists()) {
  229. return null;
  230. }
  231. // 获取Workbook
  232. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  233. Workbook wb = null;
  234. if (filePath.endsWith("xls")) {
  235. wb = new HSSFWorkbook(inputStream);
  236. } else {
  237. wb = new XSSFWorkbook(inputStream);
  238. }
  239. inputStream.close();
  240. // 获取Sheet
  241. Sheet sheet = wb.getSheetAt(0);
  242. Iterator<Row> rows = sheet.rowIterator();
  243. // 获取第一行的数据,匹配到表头
  244. int cellNum = -1;
  245. Row row = rows.next();
  246. Iterator<Cell> cells = row.cellIterator();
  247. while (cells.hasNext()) {
  248. Cell cell = cells.next();
  249. if (cellName.trim().equals(cell.getStringCellValue().trim())) {
  250. cellNum = cell.getColumnIndex();
  251. break;
  252. }
  253. }
  254. if (cellNum == -1) return null;
  255. // 读取数据
  256. ArrayList<String> arreyMap = new ArrayList<String>();
  257. while (rows.hasNext()) {
  258. Row temp = rows.next();
  259. Cell cell = temp.getCell(cellNum);
  260. // 非空的列需要使用
  261. if (cell == null) {
  262. continue;
  263. }
  264. String name = getValue(cell);
  265. if (name != null && !"".equals(name.trim())) {
  266. String cellValue = getValue(cell);
  267. arreyMap.add(cellValue);
  268. }
  269. }
  270. // Map to List
  271. /*Collection<String> valueCollection = map.values();*/
  272. return arreyMap;
  273. }
  274. public static List<EfastOrder> readCell(String filePath, String productName, String[] maps,
  275. String[] infos, String tel, String name,String platformOrder, String num,
  276. Map<String, ProductColor> price, String postType, String shopId)
  277. throws IOException {
  278. Map<String, String> map = new HashMap<>();
  279. for (String t : maps) {
  280. String[] temp = StringUtils.split(t, "_");
  281. map.put(temp[1], (temp[0]));
  282. }
  283. // 判断文件是否存在
  284. File file = new File(filePath);
  285. if (!file.exists()) {
  286. return null;
  287. }
  288. // 获取Workbook
  289. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  290. Workbook wb = null;
  291. if (filePath.endsWith("xls")) {
  292. wb = new HSSFWorkbook(inputStream);
  293. } else {
  294. wb = new XSSFWorkbook(inputStream);
  295. }
  296. inputStream.close();
  297. // 获取Sheet
  298. Sheet sheet = wb.getSheetAt(0);
  299. Iterator<Row> rows = sheet.rowIterator();
  300. // 获取第一行的数据,匹配到表头
  301. int nameColumnIndex = -1;
  302. int platformOrderColumnIndex = -1;
  303. int telColumnIndex = -1;
  304. int productColumnIndex = -1;
  305. int numColumnIndex = -1;
  306. int[] infoColumnIndex = new int[infos.length];
  307. int infoCount = 0;
  308. // 读取数据
  309. Row row = rows.next();
  310. Iterator<Cell> cells = row.cellIterator();
  311. while (cells.hasNext()) {
  312. Cell cell = cells.next();
  313. if (name.equals(cell.getStringCellValue().trim())) {
  314. // 匹配姓名列
  315. nameColumnIndex = cell.getColumnIndex();
  316. } else if (platformOrder.equals(cell.getStringCellValue().trim())) {
  317. // 匹配订单编号
  318. platformOrderColumnIndex = cell.getColumnIndex();
  319. } else if (tel.equals(cell.getStringCellValue().trim())) {
  320. // 匹配电话
  321. telColumnIndex = cell.getColumnIndex();
  322. } else if (productName.equals(cell.getStringCellValue().trim())) {
  323. // 匹配产品列
  324. productColumnIndex = cell.getColumnIndex();
  325. } else if (num.equals(cell.getStringCellValue().trim())) {
  326. // 匹配购买数量
  327. numColumnIndex = cell.getColumnIndex();
  328. } else {
  329. // 匹配地址(可能是多个,需要组合连接)
  330. for (int i = 0; i < infos.length; i++) {
  331. if (infos[i].equals(cell.getStringCellValue().trim())) {
  332. infoColumnIndex[i] = cell.getColumnIndex();
  333. }
  334. }
  335. }
  336. }
  337. List<EfastOrder> orderEfasts = new ArrayList<>();
  338. // 样式
  339. HSSFCellStyle cellStyle = (HSSFCellStyle) wb.createCellStyle();
  340. cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
  341. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  342. // 读取数据
  343. int oid = 0;
  344. while (rows.hasNext()) {
  345. Row temp = rows.next();
  346. try {
  347. String nameValue = getValue(temp.getCell(nameColumnIndex)); // 读取name
  348. String platformOrderIdValue = getValue(temp.getCell(platformOrderColumnIndex)); // 读取平台id
  349. //获取手机号码
  350. //String userTel = getValue(temp.getCell(telColumnIndex)).trim(); // 读取tel
  351. Cell telCell = temp.getCell(telColumnIndex);
  352. String userTel = String.valueOf(getExcelCell(telCell));
  353. String OrderProductBarCodeValue = map.get(getValue(temp.getCell(productColumnIndex))); // 读取对应的产品id
  354. if(StringUtils.isEmpty(nameValue) && StringUtils.isEmpty(userTel) && StringUtils.isEmpty(OrderProductBarCodeValue)){
  355. break;
  356. }
  357. EfastOrder order = new EfastOrder();
  358. order.setOrderAddressName(nameValue);
  359. if(!StringUtils.isEmpty(platformOrderIdValue)){
  360. order.setPlatformOrderId(platformOrderIdValue.trim());
  361. }else{
  362. order.setPlatformOrderId("1");
  363. }
  364. // 如果手机号码校验不通过,则应该不录入
  365. if (!ValidateUtil.checkMobile(userTel.trim())) {
  366. //temp.getCell(telColumnIndex).setCellStyle(cellStyle);
  367. order.setReturnStatus("error");
  368. order.setReturnMsg("手机号码不正确");
  369. //continue;
  370. }
  371. order.setOrderAddressTel(userTel.trim());
  372. order.setOrderProductBarCode(OrderProductBarCodeValue);
  373. String orderNum = getValue(temp.getCell(numColumnIndex));
  374. if(orderNum == null || "".equals(orderNum)){
  375. order.setReturnStatus("error");
  376. order.setReturnMsg("数量不正确");
  377. order.setOrderNum(0); // 读取购买数量
  378. }else{
  379. order.setOrderNum(Integer.valueOf(orderNum)); // 读取购买数量
  380. }
  381. if (infoColumnIndex.length == 1){
  382. // 如果infoColumnIndex = 1, 则表示需要截取(可能是-、“ ”)
  383. Cell cell = temp.getCell(infoColumnIndex[0]);
  384. String addrInfo = getValue(cell);
  385. if (addrInfo == null || "".equals(addrInfo)) {
  386. // 地址为空
  387. cell.setCellStyle(cellStyle);
  388. continue;
  389. }
  390. // 处理地址信息
  391. String[] addrs = splitAddress(addrInfo);
  392. if (addrs == null && addrs.length < 4) {
  393. // 地址不合法
  394. cell.setCellStyle(cellStyle);
  395. continue;
  396. }
  397. order.setOrderProvince(addrs[0].trim());
  398. order.setOrderCity(addrs[1].trim());
  399. order.setOrderArea(addrs[2].trim());
  400. order.setOrderAddress(addrInfo.trim());
  401. } else if (infoColumnIndex.length == 4) {
  402. order.setOrderProvince(getValue(temp.getCell(infoColumnIndex[0])).trim());
  403. order.setOrderCity(getValue(temp.getCell(infoColumnIndex[1])).trim());
  404. order.setOrderArea(getValue(temp.getCell(infoColumnIndex[2])).trim());
  405. order.setOrderAddress(getValue(temp.getCell(infoColumnIndex[3])).trim());
  406. } else if (infoColumnIndex.length == 3) {
  407. order.setOrderProvince(getValue(temp.getCell(infoColumnIndex[0])).trim());
  408. order.setOrderCity(getValue(temp.getCell(infoColumnIndex[1])).trim());
  409. order.setOrderArea(getValue(temp.getCell(infoColumnIndex[1])).trim());
  410. order.setOrderAddress(getValue(temp.getCell(infoColumnIndex[2])).trim());
  411. } else {
  412. for (int i : infoColumnIndex) {
  413. temp.getCell(infoColumnIndex[i]).setCellStyle(cellStyle);
  414. }
  415. continue;
  416. }
  417. // 支付方式
  418. order.setOrderPayType(1); // 微信支付
  419. order.setOrderPayMoney(price.get(order.getOrderProductBarCode()).getColorDiscount() * order.getOrderNum());
  420. order.setOrderAmount(price.get(order.getOrderProductBarCode()).getColorDiscount() * order.getOrderNum());
  421. order.setOrderOpenId(order.getOrderAddressTel());
  422. order.setOrderProductName(price.get(order.getOrderProductBarCode()).getColorName());
  423. order.setOrderId(OrderNoUtil.createOrderCode(Math.abs(new Random().nextInt(1000))));
  424. order.setOrderRemark("");
  425. order.setOrderStoreId(shopId); // 微商/代理商仓库
  426. order.setOid(oid); // oid
  427. if (postType.equals("ems")) {
  428. if (order.getOrderProvince().contains("广东")) {
  429. order.setOrderPostType("ems");
  430. } else {
  431. order.setOrderPostType("eyb");
  432. }
  433. } else {
  434. order.setOrderPostType(postType);
  435. }
  436. // 保存
  437. orderEfasts.add(order);
  438. } catch (Exception e) {
  439. row.getCell(0).setCellStyle(cellStyle);
  440. logger.error(e.getMessage(), e);
  441. }
  442. // 迭代
  443. oid++;
  444. }
  445. // 写入
  446. OutputStream output = new BufferedOutputStream(new FileOutputStream(filePath));
  447. wb.write(output);
  448. output.close();
  449. return orderEfasts;
  450. }
  451. /**
  452. *获取值
  453. * @param cell
  454. * @return
  455. */
  456. private static Object getExcelCell(Cell cell){
  457. Object obj;
  458. if (null != cell) {
  459. switch (cell.getCellType()) {
  460. case HSSFCell.CELL_TYPE_NUMERIC: // 数字
  461. double cellValue = cell.getNumericCellValue();
  462. obj = new DecimalFormat("#").format(cellValue);
  463. break;
  464. case HSSFCell.CELL_TYPE_STRING: // 字符串
  465. obj = cell.getStringCellValue();
  466. break;
  467. case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
  468. obj = cell.getBooleanCellValue();
  469. break;
  470. case HSSFCell.CELL_TYPE_FORMULA: // 公式
  471. obj = cell.getCellFormula();
  472. break;
  473. case HSSFCell.CELL_TYPE_BLANK: // 空值
  474. obj = "";
  475. break;
  476. case HSSFCell.CELL_TYPE_ERROR: // 故障
  477. obj = "";
  478. break;
  479. default:
  480. obj = "";
  481. break;
  482. }
  483. } else {
  484. obj = "";
  485. }
  486. return obj;
  487. }
  488. /**
  489. * 地址 - 整体切割符
  490. */
  491. private static String[] ENTIRETY_SPLIT;
  492. /**
  493. * 地址 - 省份数据切割符
  494. */
  495. private static String[][] PROVINCE_CITY_AERA_SPLIT;
  496. static {
  497. ENTIRETY_SPLIT = new String[]{" ", "-", "_", "*"};
  498. PROVINCE_CITY_AERA_SPLIT = new String[][]{
  499. {"省", "市", "区"},
  500. {"省", "市", "县"},
  501. {"省", "市", "乡"},
  502. {"省", "市", "镇"},
  503. {"省", "市", "市"},
  504. {"自治区", "市", "区"},
  505. {"自治区", "市", "县"},
  506. {"自治区", "市", "乡"},
  507. {"自治区", "市", "镇"},
  508. {"自治区", "市", "市"},
  509. {"省", "州", "区"},
  510. {"省", "州", "县"},
  511. {"省", "州", "乡"},
  512. {"省", "州", "市"},
  513. {"省", "州", "镇"},
  514. {"市", "市", "区"},
  515. {"市", "市", "县"},
  516. {"市", "市", "镇"},
  517. {"市", "市", "乡"},
  518. {"市", "区"},
  519. {"市", "乡"},
  520. {"市", "镇"},
  521. {"市", "县"}
  522. };
  523. }
  524. /**
  525. * 切割地址
  526. */
  527. private static String[] splitAddress(String addrInfo) {
  528. String[] addrs = null;
  529. // 首先通过整体切割符合切割
  530. for (String charSplit : ENTIRETY_SPLIT) {
  531. addrs = StringUtils.split(addrInfo, charSplit);
  532. if (addrs != null && addrs.length >= 4) {
  533. break;
  534. } else if (addrs != null && addrs.length == 3 && !addrs[0].contains("省")) {
  535. break;
  536. }
  537. }
  538. // 详细地址不能直接切割
  539. if (addrs != null && addrs.length >= 4) {
  540. // 防止区域数据出现小区 扰乱数据
  541. if (!addrs[2].contains("小区")) {
  542. return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
  543. }
  544. } else if (addrs != null && addrs.length == 3) {
  545. // 防止区域数据出现小区 扰乱数据
  546. if (!addrs[1].contains("小区")) {
  547. return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
  548. }
  549. }
  550. for (String[] splits : PROVINCE_CITY_AERA_SPLIT) {
  551. // 每组独立切割字符单独循环
  552. String tempInfo = addrInfo;
  553. addrs = new String[splits.length + 1];
  554. boolean isNext = false;
  555. for (int i = 0; i < splits.length; i++) {
  556. String splitChar = splits[i];
  557. String[] temps = StringUtils.split(tempInfo, splitChar);
  558. if (temps.length >= 2) {
  559. // 获取到第一个地址
  560. addrs[i] = temps[0] + splitChar;
  561. // 下一次迭代内容
  562. tempInfo = StringUtils.substring(tempInfo, temps[0].length() + splitChar.length());
  563. } else {
  564. // 如果首次匹配失败,则停止匹配
  565. isNext = true;
  566. break;
  567. }
  568. }
  569. if (!isNext) {
  570. if ((addrs.length - 1) == 2) {
  571. // 防止区域数据出现小区 扰乱数据
  572. if (addrs[1].contains("小区")) {
  573. continue;
  574. }
  575. // 只有两级地址
  576. return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
  577. } else if ((addrs.length - 1) == 3) {
  578. // 防止区域数据出现小区 扰乱数据
  579. if (addrs[2].contains("小区")) {
  580. continue;
  581. }
  582. // 三级地址
  583. return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
  584. }
  585. }
  586. }
  587. return addrs;
  588. }
  589. public static void main(String[] args) throws IOException {
  590. String[] add = splitAddress("新疆维吾尔自治区乌鲁木齐市沙依巴克镇农大东路311号新疆农业大学外国语学院(830002).");
  591. if (add == null) {
  592. System.out.println("切割失败...");
  593. }
  594. for (String i : add) {
  595. System.out.println(i);
  596. }
  597. }
  598. private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  599. public static String getValue(Cell cell) {
  600. if (cell == null) return null;
  601. switch (cell.getCellType()) {
  602. case Cell.CELL_TYPE_STRING: //文本
  603. return cell.getStringCellValue();
  604. case Cell.CELL_TYPE_NUMERIC: //数字、日期
  605. if (DateUtil.isCellDateFormatted(cell)) {
  606. return fmt.format(cell.getDateCellValue()); //日期型
  607. }
  608. return String.valueOf((new Double(cell.getNumericCellValue())).intValue()); //数字
  609. case Cell.CELL_TYPE_BOOLEAN: //布尔型
  610. return String.valueOf(cell.getBooleanCellValue());
  611. case Cell.CELL_TYPE_BLANK: //空白
  612. return cell.getStringCellValue();
  613. default: //错误
  614. return "ERROR";
  615. }
  616. }
  617. }