ExcelUtil.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package com.iamberry.wechat.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.poi.hssf.usermodel.HSSFCell;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import org.apache.poi.ss.usermodel.*;
  6. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.stereotype.Component;
  9. import java.io.*;
  10. import java.text.DecimalFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.*;
  13. /**
  14. * Excel工具类
  15. *
  16. * @author
  17. * @company 深圳爱贝源科技有限公司
  18. * @website www.iamberry.com
  19. * @tel 18271840547
  20. * @date 2017/5/25
  21. */
  22. @Component
  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内容 -- 行数是否超过100,超过false 不超过true;
  119. * @param filePath
  120. * @return
  121. */
  122. public static boolean readExcelNumber(String filePath,Integer maxNuber) throws IOException {
  123. // 判断文件是否存在
  124. File file = new File(filePath);
  125. if (!file.exists()) {
  126. return false;
  127. }
  128. // 获取Workbook
  129. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  130. Workbook wb = null;
  131. if (filePath.endsWith("xls")) {
  132. wb = new HSSFWorkbook(inputStream);
  133. } else {
  134. wb = new XSSFWorkbook(inputStream);
  135. }
  136. inputStream.close();
  137. // 获取Sheet
  138. Sheet sheet = wb.getSheetAt(0);
  139. Integer number = 0; //控制有效行数在maxNumber之内
  140. Integer i = 0; //控制总行数在MaxNumber之内,因为会出现无效行数的问题,如果一直循环,会循环65535行
  141. Iterator<Row> rows = sheet.rowIterator();
  142. while (rows.hasNext()) {
  143. Row row = rows.next();
  144. Cell cell = row.getCell(0);
  145. // 非空的列需要使用
  146. if (cell == null) {
  147. continue;
  148. }
  149. String name = getValue(cell);
  150. String cellValue = "";
  151. if (name != null && !"".equals(name.trim())) {
  152. cellValue = getValue(cell);
  153. }
  154. if (cellValue != null && !"".equals(cellValue)) {
  155. number ++;
  156. }
  157. i++;
  158. if(number > maxNuber+1){
  159. return false;
  160. }
  161. if(i > maxNuber+1){
  162. break;
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * 读取Excel内容
  169. * 不去除重复
  170. *
  171. * @param filePath
  172. * @return
  173. */
  174. public static List<String> readCell2(String filePath, String cellName) throws IOException {
  175. // 判断文件是否存在
  176. File file = new File(filePath);
  177. if (!file.exists()) {
  178. return null;
  179. }
  180. // 获取Workbook
  181. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  182. Workbook wb = null;
  183. if (filePath.endsWith("xls")) {
  184. wb = new HSSFWorkbook(inputStream);
  185. } else {
  186. wb = new XSSFWorkbook(inputStream);
  187. }
  188. inputStream.close();
  189. // 获取Sheet
  190. Sheet sheet = wb.getSheetAt(0);
  191. Iterator<Row> rows = sheet.rowIterator();
  192. // 获取第一行的数据,匹配到表头
  193. int cellNum = -1;
  194. Row row = rows.next();
  195. Iterator<Cell> cells = row.cellIterator();
  196. while (cells.hasNext()) {
  197. Cell cell = cells.next();
  198. if (cellName.trim().equals(cell.getStringCellValue().trim())) {
  199. cellNum = cell.getColumnIndex();
  200. break;
  201. }
  202. }
  203. if (cellNum == -1) return null;
  204. // 读取数据
  205. ArrayList<String> arreyMap = new ArrayList<String>();
  206. while (rows.hasNext()) {
  207. Row temp = rows.next();
  208. Cell cell = temp.getCell(cellNum);
  209. // 非空的列需要使用
  210. if (cell == null) {
  211. continue;
  212. }
  213. String name = getValue(cell);
  214. if (name != null && !"".equals(name.trim())) {
  215. String cellValue = getValue(cell);
  216. arreyMap.add(cellValue);
  217. }
  218. }
  219. // Map to List
  220. /*Collection<String> valueCollection = map.values();*/
  221. return arreyMap;
  222. }
  223. /**
  224. * 读取Excel内容(上传excel文档)
  225. * 不去除重复
  226. *
  227. * @param filePath
  228. * @return
  229. */
  230. public static List<String> readCell3(String filePath) throws IOException {
  231. // 判断文件是否存在
  232. File file = new File(filePath);
  233. if (!file.exists()) {
  234. return null;
  235. }
  236. // 获取Workbook
  237. InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
  238. Workbook wb = null;
  239. if (filePath.endsWith("xls")) {
  240. wb = new HSSFWorkbook(inputStream);
  241. } else {
  242. wb = new XSSFWorkbook(inputStream);
  243. }
  244. inputStream.close();
  245. // 获取Sheet
  246. Sheet sheet = wb.getSheetAt(0);
  247. Iterator<Row> rows = sheet.rowIterator();
  248. // 获取第一行的数据,匹配到表头
  249. int cellNum = -1;
  250. Row row = rows.next();
  251. Iterator<Cell> cells = row.cellIterator();
  252. while (cells.hasNext()) {
  253. Cell cell = cells.next();
  254. cellNum = cell.getColumnIndex();
  255. break;
  256. }
  257. if (cellNum == -1) return null;
  258. // 读取数据
  259. ArrayList<String> arreyMap = new ArrayList<String>();
  260. while (rows.hasNext()) {
  261. Row temp = rows.next();
  262. Cell cell = temp.getCell(cellNum);
  263. // 非空的列需要使用
  264. if (cell == null) {
  265. continue;
  266. }
  267. String name = getValue(cell);
  268. if (name != null && !"".equals(name.trim())) {
  269. String cellValue = getValue(cell);
  270. arreyMap.add(cellValue);
  271. }
  272. }
  273. // Map to List
  274. /*Collection<String> valueCollection = map.values();*/
  275. return arreyMap;
  276. }
  277. /**
  278. *获取值
  279. * @param cell
  280. * @return
  281. */
  282. private static Object getExcelCell(Cell cell){
  283. Object obj;
  284. if (null != cell) {
  285. switch (cell.getCellType()) {
  286. case HSSFCell.CELL_TYPE_NUMERIC: // 数字
  287. double cellValue = cell.getNumericCellValue();
  288. obj = new DecimalFormat("#").format(cellValue);
  289. break;
  290. case HSSFCell.CELL_TYPE_STRING: // 字符串
  291. obj = cell.getStringCellValue();
  292. break;
  293. case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
  294. obj = cell.getBooleanCellValue();
  295. break;
  296. case HSSFCell.CELL_TYPE_FORMULA: // 公式
  297. obj = cell.getCellFormula();
  298. break;
  299. case HSSFCell.CELL_TYPE_BLANK: // 空值
  300. obj = "";
  301. break;
  302. case HSSFCell.CELL_TYPE_ERROR: // 故障
  303. obj = "";
  304. break;
  305. default:
  306. obj = "";
  307. break;
  308. }
  309. } else {
  310. obj = "";
  311. }
  312. return obj;
  313. }
  314. /**
  315. * 地址 - 整体切割符
  316. */
  317. private static String[] ENTIRETY_SPLIT;
  318. /**
  319. * 地址 - 省份数据切割符
  320. */
  321. private static String[][] PROVINCE_CITY_AERA_SPLIT;
  322. static {
  323. ENTIRETY_SPLIT = new String[]{" ", "-", "_", "*"};
  324. PROVINCE_CITY_AERA_SPLIT = new String[][]{
  325. {"省", "市", "区"},
  326. {"省", "市", "县"},
  327. {"省", "市", "乡"},
  328. {"省", "市", "镇"},
  329. {"省", "市", "市"},
  330. {"自治区", "市", "区"},
  331. {"自治区", "市", "县"},
  332. {"自治区", "市", "乡"},
  333. {"自治区", "市", "镇"},
  334. {"自治区", "市", "市"},
  335. {"省", "州", "区"},
  336. {"省", "州", "县"},
  337. {"省", "州", "乡"},
  338. {"省", "州", "市"},
  339. {"省", "州", "镇"},
  340. {"市", "市", "区"},
  341. {"市", "市", "县"},
  342. {"市", "市", "镇"},
  343. {"市", "市", "乡"},
  344. {"市", "区"},
  345. {"市", "乡"},
  346. {"市", "镇"},
  347. {"市", "县"},
  348. {"省", "县", "镇"}
  349. };
  350. }
  351. /**
  352. * 切割地址
  353. */
  354. private static String[] splitAddress(String addrInfo) {
  355. String[] addrs = null;
  356. // 首先通过整体切割符合切割
  357. for (String charSplit : ENTIRETY_SPLIT) {
  358. addrs = StringUtils.split(addrInfo, charSplit);
  359. if (addrs != null && addrs.length >= 4) {
  360. break;
  361. } else if (addrs != null && addrs.length == 3 && !addrs[0].contains("省")) {
  362. break;
  363. }
  364. }
  365. // 详细地址不能直接切割
  366. if (addrs != null && addrs.length >= 4) {
  367. // 防止区域数据出现小区 扰乱数据
  368. if (!addrs[2].contains("小区")) {
  369. return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
  370. }
  371. } else if (addrs != null && addrs.length == 3) {
  372. // 防止区域数据出现小区 扰乱数据
  373. if (!addrs[1].contains("小区")) {
  374. return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
  375. }
  376. }
  377. for (String[] splits : PROVINCE_CITY_AERA_SPLIT) {
  378. // 每组独立切割字符单独循环
  379. String tempInfo = addrInfo;
  380. addrs = new String[splits.length + 1];
  381. boolean isNext = false;
  382. for (int i = 0; i < splits.length; i++) {
  383. String splitChar = splits[i];
  384. String[] temps = StringUtils.split(tempInfo, splitChar);
  385. if (temps.length >= 2) {
  386. // 获取到第一个地址
  387. addrs[i] = temps[0] + splitChar;
  388. // 下一次迭代内容
  389. tempInfo = StringUtils.substring(tempInfo, temps[0].length() + splitChar.length());
  390. } else {
  391. // 如果首次匹配失败,则停止匹配
  392. isNext = true;
  393. break;
  394. }
  395. }
  396. if (!isNext) {
  397. if ((addrs.length - 1) == 2) {
  398. // 防止区域数据出现小区 扰乱数据
  399. if (addrs[1].contains("小区")) {
  400. continue;
  401. }
  402. // 只有两级地址
  403. return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
  404. } else if ((addrs.length - 1) == 3) {
  405. // 防止区域数据出现小区 扰乱数据
  406. if (addrs[2].contains("小区")) {
  407. continue;
  408. }
  409. // 三级地址
  410. return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
  411. }
  412. }
  413. }
  414. return addrs;
  415. }
  416. public static void main(String[] args) throws IOException {
  417. String[] add = splitAddress("海南省 临高县 博厚镇衍宏海港小镇(303县道西)衍宏海港小镇37栋602");
  418. if (add == null) {
  419. System.out.println("切割失败...");
  420. }
  421. for (String i : add) {
  422. System.out.println(i);
  423. }
  424. }
  425. private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  426. public static String getValue(Cell cell) {
  427. if (cell == null) return null;
  428. switch (cell.getCellType()) {
  429. case Cell.CELL_TYPE_STRING: //文本
  430. return cell.getStringCellValue();
  431. case Cell.CELL_TYPE_NUMERIC: //数字、日期
  432. if (DateUtil.isCellDateFormatted(cell)) {
  433. return fmt.format(cell.getDateCellValue()); //日期型
  434. }
  435. DecimalFormat df = new DecimalFormat("#");
  436. return df.format(new Double(cell.getNumericCellValue())); //数字
  437. case Cell.CELL_TYPE_BOOLEAN: //布尔型
  438. return String.valueOf(cell.getBooleanCellValue());
  439. case Cell.CELL_TYPE_BLANK: //空白
  440. return cell.getStringCellValue();
  441. default: //错误
  442. return "ERROR";
  443. }
  444. }
  445. /**
  446. * excel 强制获取text文本
  447. * @param cell
  448. * @return
  449. */
  450. public static String getTextValue(Cell cell) {
  451. if (cell == null) return null;
  452. return cell.getStringCellValue();
  453. }
  454. }