123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- package com.iamberry.wechat.utils;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import java.io.*;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- @Component
- public class ExcelUtil {
- private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
-
- public static List<String> readExcelHead(String filePath) throws IOException {
- List<String> heads = new ArrayList<String>();
-
- File file = new File(filePath);
- if (!file.exists()) {
- return null;
- }
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- Workbook wb = null;
- if (filePath.endsWith("xls")) {
- wb = new HSSFWorkbook(inputStream);
- } else {
- wb = new XSSFWorkbook(inputStream);
- }
- inputStream.close();
-
- Sheet sheet = wb.getSheetAt(0);
-
- Row row = sheet.rowIterator().next();
- Iterator<Cell> cells = row.cellIterator();
-
- while (cells.hasNext()) {
- Cell cell = cells.next();
- heads.add(cell.getStringCellValue());
- }
- return heads;
- }
-
- public static List<String> readCell(String filePath, String cellName) throws IOException {
-
- File file = new File(filePath);
- if (!file.exists()) {
- return null;
- }
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- Workbook wb = null;
- if (filePath.endsWith("xls")) {
- wb = new HSSFWorkbook(inputStream);
- } else {
- wb = new XSSFWorkbook(inputStream);
- }
- inputStream.close();
-
- Sheet sheet = wb.getSheetAt(0);
- Iterator<Row> rows = sheet.rowIterator();
-
- int cellNum = -1;
- Row row = rows.next();
- Iterator<Cell> cells = row.cellIterator();
- while (cells.hasNext()) {
- Cell cell = cells.next();
- if (cellName.trim().equals(cell.getStringCellValue().trim())) {
- cellNum = cell.getColumnIndex();
- break;
- }
- }
- if (cellNum == -1) return null;
-
- Map<String, String> map = new HashMap<>();
- while (rows.hasNext()) {
- Row temp = rows.next();
- Cell cell = temp.getCell(cellNum);
-
- if (cell == null) {
- continue;
- }
- String name = getValue(cell);
- if (name != null && !"".equals(name.trim())) {
- String cellValue = getValue(cell);
- if (map.get(cellValue) == null) {
- map.put(cellValue, cellValue);
- }
- }
- }
-
- Collection<String> valueCollection = map.values();
- return new ArrayList<String>(valueCollection);
- }
-
- public static boolean readExcelNumber(String filePath,Integer maxNuber) throws IOException {
-
- File file = new File(filePath);
- if (!file.exists()) {
- return false;
- }
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- Workbook wb = null;
- if (filePath.endsWith("xls")) {
- wb = new HSSFWorkbook(inputStream);
- } else {
- wb = new XSSFWorkbook(inputStream);
- }
- inputStream.close();
-
- Sheet sheet = wb.getSheetAt(0);
- Integer number = 0;
- Integer i = 0;
- Iterator<Row> rows = sheet.rowIterator();
- while (rows.hasNext()) {
- Row row = rows.next();
- Cell cell = row.getCell(0);
-
- if (cell == null) {
- continue;
- }
- String name = getValue(cell);
- String cellValue = "";
- if (name != null && !"".equals(name.trim())) {
- cellValue = getValue(cell);
- }
- if (cellValue != null && !"".equals(cellValue)) {
- number ++;
- }
- i++;
- if(number > maxNuber+1){
- return false;
- }
- if(i > maxNuber+1){
- break;
- }
- }
- return true;
- }
-
- public static List<String> readCell2(String filePath, String cellName) throws IOException {
-
- File file = new File(filePath);
- if (!file.exists()) {
- return null;
- }
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- Workbook wb = null;
- if (filePath.endsWith("xls")) {
- wb = new HSSFWorkbook(inputStream);
- } else {
- wb = new XSSFWorkbook(inputStream);
- }
- inputStream.close();
-
- Sheet sheet = wb.getSheetAt(0);
- Iterator<Row> rows = sheet.rowIterator();
-
- int cellNum = -1;
- Row row = rows.next();
- Iterator<Cell> cells = row.cellIterator();
- while (cells.hasNext()) {
- Cell cell = cells.next();
- if (cellName.trim().equals(cell.getStringCellValue().trim())) {
- cellNum = cell.getColumnIndex();
- break;
- }
- }
- if (cellNum == -1) return null;
-
- ArrayList<String> arreyMap = new ArrayList<String>();
- while (rows.hasNext()) {
- Row temp = rows.next();
- Cell cell = temp.getCell(cellNum);
-
- if (cell == null) {
- continue;
- }
- String name = getValue(cell);
- if (name != null && !"".equals(name.trim())) {
- String cellValue = getValue(cell);
- arreyMap.add(cellValue);
- }
- }
-
-
- return arreyMap;
- }
-
- public static List<String> readCell3(String filePath) throws IOException {
-
- File file = new File(filePath);
- if (!file.exists()) {
- return null;
- }
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- Workbook wb = null;
- if (filePath.endsWith("xls")) {
- wb = new HSSFWorkbook(inputStream);
- } else {
- wb = new XSSFWorkbook(inputStream);
- }
- inputStream.close();
-
- Sheet sheet = wb.getSheetAt(0);
- Iterator<Row> rows = sheet.rowIterator();
-
- int cellNum = -1;
- Row row = rows.next();
- Iterator<Cell> cells = row.cellIterator();
- while (cells.hasNext()) {
- Cell cell = cells.next();
- cellNum = cell.getColumnIndex();
- break;
- }
- if (cellNum == -1) return null;
-
- ArrayList<String> arreyMap = new ArrayList<String>();
- while (rows.hasNext()) {
- Row temp = rows.next();
- Cell cell = temp.getCell(cellNum);
-
- if (cell == null) {
- continue;
- }
- String name = getValue(cell);
- if (name != null && !"".equals(name.trim())) {
- String cellValue = getValue(cell);
- arreyMap.add(cellValue);
- }
- }
-
-
- return arreyMap;
- }
-
- private static Object getExcelCell(Cell cell){
- Object obj;
- if (null != cell) {
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_NUMERIC:
- double cellValue = cell.getNumericCellValue();
- obj = new DecimalFormat("#").format(cellValue);
- break;
- case HSSFCell.CELL_TYPE_STRING:
- obj = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- obj = cell.getBooleanCellValue();
- break;
- case HSSFCell.CELL_TYPE_FORMULA:
- obj = cell.getCellFormula();
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- obj = "";
- break;
- case HSSFCell.CELL_TYPE_ERROR:
- obj = "";
- break;
- default:
- obj = "";
- break;
- }
- } else {
- obj = "";
- }
- return obj;
- }
-
- private static String[] ENTIRETY_SPLIT;
-
- private static String[][] PROVINCE_CITY_AERA_SPLIT;
- static {
- ENTIRETY_SPLIT = new String[]{" ", "-", "_", "*"};
- PROVINCE_CITY_AERA_SPLIT = new String[][]{
- {"省", "市", "区"},
- {"省", "市", "县"},
- {"省", "市", "乡"},
- {"省", "市", "镇"},
- {"省", "市", "市"},
- {"自治区", "市", "区"},
- {"自治区", "市", "县"},
- {"自治区", "市", "乡"},
- {"自治区", "市", "镇"},
- {"自治区", "市", "市"},
- {"省", "州", "区"},
- {"省", "州", "县"},
- {"省", "州", "乡"},
- {"省", "州", "市"},
- {"省", "州", "镇"},
- {"市", "市", "区"},
- {"市", "市", "县"},
- {"市", "市", "镇"},
- {"市", "市", "乡"},
- {"市", "区"},
- {"市", "乡"},
- {"市", "镇"},
- {"市", "县"},
- {"省", "县", "镇"}
- };
- }
-
- private static String[] splitAddress(String addrInfo) {
- String[] addrs = null;
-
- for (String charSplit : ENTIRETY_SPLIT) {
- addrs = StringUtils.split(addrInfo, charSplit);
- if (addrs != null && addrs.length >= 4) {
- break;
- } else if (addrs != null && addrs.length == 3 && !addrs[0].contains("省")) {
- break;
- }
- }
-
- if (addrs != null && addrs.length >= 4) {
-
- if (!addrs[2].contains("小区")) {
- return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
- }
- } else if (addrs != null && addrs.length == 3) {
-
- if (!addrs[1].contains("小区")) {
- return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
- }
- }
- for (String[] splits : PROVINCE_CITY_AERA_SPLIT) {
-
- String tempInfo = addrInfo;
- addrs = new String[splits.length + 1];
- boolean isNext = false;
- for (int i = 0; i < splits.length; i++) {
- String splitChar = splits[i];
- String[] temps = StringUtils.split(tempInfo, splitChar);
- if (temps.length >= 2) {
-
- addrs[i] = temps[0] + splitChar;
-
- tempInfo = StringUtils.substring(tempInfo, temps[0].length() + splitChar.length());
- } else {
-
- isNext = true;
- break;
- }
- }
- if (!isNext) {
- if ((addrs.length - 1) == 2) {
-
- if (addrs[1].contains("小区")) {
- continue;
- }
-
- return new String[]{addrs[0], addrs[0], addrs[1], addrInfo};
- } else if ((addrs.length - 1) == 3) {
-
- if (addrs[2].contains("小区")) {
- continue;
- }
-
- return new String[]{addrs[0], addrs[1], addrs[2], addrInfo};
- }
- }
- }
- return addrs;
- }
- public static void main(String[] args) throws IOException {
- String[] add = splitAddress("海南省 临高县 博厚镇衍宏海港小镇(303县道西)衍宏海港小镇37栋602");
- if (add == null) {
- System.out.println("切割失败...");
- }
- for (String i : add) {
- System.out.println(i);
- }
- }
- private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
- public static String getValue(Cell cell) {
- if (cell == null) return null;
- switch (cell.getCellType()) {
- case Cell.CELL_TYPE_STRING:
- return cell.getStringCellValue();
- case Cell.CELL_TYPE_NUMERIC:
- if (DateUtil.isCellDateFormatted(cell)) {
- return fmt.format(cell.getDateCellValue());
- }
- DecimalFormat df = new DecimalFormat("#");
- return df.format(new Double(cell.getNumericCellValue()));
- case Cell.CELL_TYPE_BOOLEAN:
- return String.valueOf(cell.getBooleanCellValue());
- case Cell.CELL_TYPE_BLANK:
- return cell.getStringCellValue();
- default:
- return "ERROR";
- }
- }
-
- public static String getTextValue(Cell cell) {
- if (cell == null) return null;
- return cell.getStringCellValue();
- }
- }
|