JAVA realizes reading data in Excel

public static void main(String[] args) {
		File file = new File("C:\\Users\\Administrator\\Desktop\\read test.xlsx");
		if(file.exists()){
			try {
				Workbook book = WorkbookFactory.create(file);
				List<Object> rowDataList = null ;
				List<List<Object>> allRowDataList = new ArrayList<List<Object>>();
				Sheet sheet = null; //a sheet
				Row row = null ; //one row
				Cell cell = null; //a cell
				int maxColumn = 0 ;
				int rowIndex = 1;//Counting from 0, since the first row is the title or field name, it starts from the second row, which is 1
				int columnIndex = 0 ;
				System.out.println(book.getNumberOfSheets());
				for(int sheetIndex = 0 ; sheetIndex<book.getNumberOfSheets();sheetIndex++){
					/ / Loop to get the worksheet according to sheetIndex
					sheet = book.getSheetAt(sheetIndex);
					row = sheet.getRow(1); //take the first row to get the maximum column value
					if(row==null){
						continue;
					}
					//get the maximum column value
					maxColumn = row.getLastCellNum();
					for( ;rowIndex<=sheet.getLastRowNum();rowIndex++ ){
						// get row
						row = sheet.getRow(rowIndex);
						if(row==null){
							continue;
						}
						rowDataList = new ArrayList<Object>();
						for(columnIndex = row.getFirstCellNum();columnIndex < maxColumn; columnIndex++){
							//get column
							cell = row.getCell(columnIndex);
							//DecimalFormat format = new  DecimalFormat("#.##");
							//String cellValueStr = format.format(cell.getNumericCellValue());
							//Object cellValue = new BigDecimal(cellValueStr);
							int cellValue = (int)cell.getNumericCellValue();
							rowDataList.add(cellValue);
						}
						allRowDataList.add(rowDataList);
					}
				}
				for(List<Object> list : allRowDataList){
					for(Object arrList : list){
						System.out.println(arrList);
					}
				}
			} catch (Exception e) {
				e.printStackTrace ();
			}
		}else{
			System.out.println("File error");
		}
		
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326745099&siteId=291194637