| | |
| | | import com.hx.exception.TipsException; |
| | | import com.monitorjbl.xlsx.StreamingReader; |
| | | import org.apache.poi.hssf.usermodel.*; |
| | | import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| | | import org.apache.poi.xssf.usermodel.XSSFCell; |
| | |
| | | .bufferSize(10240) |
| | | .open(new FileInputStream(file)); |
| | | listData =readhandle(book); |
| | | }catch (Exception e){ |
| | | }catch (OLE2NotOfficeXmlFileException ex){ |
| | | ex.printStackTrace(); |
| | | throw new RuntimeException("excel版本不为2007及以上"); |
| | | } catch (Exception e){ |
| | | e.printStackTrace(); |
| | | throw new RuntimeException(e.getMessage()); |
| | | } |
| | |
| | | .open(new BufferedInputStream(file.getInputStream())); |
| | | |
| | | listData =readhandle(book); |
| | | }catch (Exception e){ |
| | | }catch (OLE2NotOfficeXmlFileException ex){ |
| | | ex.printStackTrace(); |
| | | throw new RuntimeException("excel版本不为2007及以上"); |
| | | } catch (Exception e){ |
| | | e.printStackTrace(); |
| | | throw new RuntimeException(e.getMessage()); |
| | | } |
| | |
| | | .open(file); |
| | | |
| | | listData =readhandle(book); |
| | | }catch (OLE2NotOfficeXmlFileException ex){ |
| | | ex.printStackTrace(); |
| | | throw new RuntimeException("excel版本不为2007及以上"); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | throw new RuntimeException(e.getMessage()); |
| | |
| | | Date d; |
| | | DateFormat formater; |
| | | Sheet sheet; |
| | | Cell cell; |
| | | //遍历所有的sheet |
| | | for(int i=0;i<book.getNumberOfSheets();i++) { |
| | | sheet = book.getSheetAt(i); |
| | | //列数 |
| | | Integer arrange = null; |
| | | //遍历所有的行 |
| | | for (Row row : sheet) { |
| | | if(row == null){ |
| | | continue; |
| | | } |
| | | isData = false; |
| | | arrayString = new ArrayList<>(); |
| | | if(arrange == null){ |
| | | arrange = row.getPhysicalNumberOfCells(); |
| | | } |
| | | //遍历所有的列 |
| | | for (Cell cell : row) { |
| | | for (int j = 0;j<arrange;j++) { |
| | | cell = row.getCell(j); |
| | | if (cell == null) { |
| | | arrayString.add(""); |
| | | } else if (cell.getCellType() == CellType.NUMERIC) { |
| | | isData = true; |
| | | format = cell.getCellStyle().getDataFormat(); |
| | | if (format == 14 || format == 31 || format == 57 || format == 58) { |
| | | //日期(中文时间格式的) |
| | | d = cell.getDateCellValue(); |
| | | formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| | | arrayString.add(formater.format(d)); |
| | | } else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
| | | d = cell.getDateCellValue(); |
| | | formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| | | arrayString.add(formater.format(d)); |
| | | } else { |
| | | if (CellType.STRING == cell.getCellType()) { |
| | | arrayString.add(cell.getStringCellValue()); |
| | | } else if (CellType.FORMULA == cell.getCellType()) { |
| | | arrayString.add(cell.getCellFormula()); |
| | | }else{ |
| | | if (cell.getCellType() == CellType.NUMERIC) { |
| | | isData = true; |
| | | format = cell.getCellStyle().getDataFormat(); |
| | | if (format == 14 || format == 31 || format == 57 || format == 58) { |
| | | //日期(中文时间格式的) |
| | | d = cell.getDateCellValue(); |
| | | formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| | | arrayString.add(formater.format(d)); |
| | | } else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
| | | d = cell.getDateCellValue(); |
| | | formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| | | arrayString.add(formater.format(d)); |
| | | } else { |
| | | arrayString.add(cell.getStringCellValue().trim()); |
| | | if (CellType.STRING == cell.getCellType()) { |
| | | arrayString.add(cell.getStringCellValue()); |
| | | } else if (CellType.FORMULA == cell.getCellType()) { |
| | | arrayString.add(cell.getCellFormula()); |
| | | } else { |
| | | arrayString.add(cell.getStringCellValue().trim()); |
| | | } |
| | | } |
| | | } else if (cell.getCellType() == CellType.BLANK) { |
| | | arrayString.add(""); |
| | | } else { // 如果EXCEL表格中的数据类型为字符串型 |
| | | isData = true; |
| | | arrayString.add(cell.getStringCellValue().trim()); |
| | | } |
| | | } else if (cell.getCellType() == CellType.BLANK) { |
| | | arrayString.add(""); |
| | | } else { // 如果EXCEL表格中的数据类型为字符串型 |
| | | isData = true; |
| | | arrayString.add(cell.getStringCellValue().trim()); |
| | | } |
| | | } |
| | | if (isData) { |