Java reading Excl file (poi-3.13)

Posted by onekoolman on Tue, 03 Dec 2019 06:39:40 +0100

Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/u010046908/article/details/50581867

Recently, the project has encountered the initialization of reading Excel data to the database. So I found one. It is found that (poi-3.13) can solve the problem. Two formats (xlsx and xls) can be parsed

Here are the implementation steps

1. Download the poi3.13 package at http://poi.apache.org/download.html

2. Learn APi.

Next, let's use demo to explain the problem:

1. Prepare Excel file:


2. Project directory structure:


Code practice:


 /** 
     * Read Excel data 
     * @param file 
     */  
    public List<Student> readExcel(File file){
    List<Student> list = new ArrayList<Student>();
        try {  
            InputStream inputStream = new FileInputStream(file);  
            String fileName = file.getName();  
            Workbook wb = null;  
            if(fileName.endsWith("xls")){  
            //Parsing xls format  
                wb = new HSSFWorkbook(inputStream);
            }else if(fileName.endsWith("xlsx")){  
            //Analysis of xlsx format 
                wb = new XSSFWorkbook(inputStream); 
            }  
            //First sheet  
            Sheet sheet = wb.getSheetAt(0);
            //Line number of the first line 
            int firstRowIndex = sheet.getFirstRowNum(); 
            //Line number of the last line
            int lastRowIndex = sheet.getLastRowNum();  
            for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){  
            //Get each line  
            Row row = sheet.getRow(rIndex);
            Student student = new Student();
                if(row != null){ 
                //Get the first example
                    int firstCellIndex = row.getFirstCellNum();  
                    int lastCellIndex = row.getLastCellNum();  
                    for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){  
                    switch (cIndex) {
case 0:
student.setNo1(row.getCell(0).toString());
break;
                        case 1:
                        student.setNo2(row.getCell(1).toString());
break;
                        case 2:
                        student.setNo3(row.getCell(2).toString());
                        break;
                        case 3:
                        student.setNo4(row.getCell(3).toString());
                        break;
                        case 4:
                        student.setNo5(row.getCell(4).toString());
                        break;
                        case 5:
                        student.setNo6(row.getCell(5).toString());
                        break;
                        case 6:
                        student.setNo7(row.getCell(6).toString());
                        break;
           
default:
break;
}
                    }  
                }
                list.add(student);
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
return list;  
    }  


Results:

That's it. If you have any questions, you can contact me QQ: 1561281670

Topics: Java Excel Database Apache