How to read excel(.xlsx) in java using poi?

前端 未结 5 1870
遇见更好的自我
遇见更好的自我 2021-01-05 17:19

I am trying to read excel in java.I have following code.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.         


        
相关标签:
5条回答
  • 2021-01-05 17:36

    add xmlbeans-2.3.0.jar file to your classpath.

    0 讨论(0)
  • 2021-01-05 17:44

    Add

    xmlbeans-2.3.0.jar
    dom4j-1.6.1.jar 
    

    along with regular POI XMLs, it will surely solve the issue.

    0 讨论(0)
  • 2021-01-05 17:45

    Use this : Input String filePath Output is list of list of json objects

    Have these dependencies
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>
    
    
    /**
     * Function reads the input excel and gives list(sheet) of objects(rows of each sheet)
     * @param filePath
     * @return list of list of json objects
     */
    public static List<List> excelFileReader(String filePath){
    
        List<List>totalSheetList=new ArrayList<>();
        XSSFWorkbook workbook= null;
        try {
            workbook = new XSSFWorkbook(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        //iteration for sheets in a workbook
        for(int sheetIndex=0;sheetIndex<workbook.getNumberOfSheets();sheetIndex++){
            XSSFSheet sheet=workbook.getSheetAt(sheetIndex);
            XSSFRow row=null;
            List<JSONObject> singleSheetList=new ArrayList<>();
    
            //excel header - first row and non-empty
            String[] header=new String[sheet.getRow(0).getPhysicalNumberOfCells()];
            if (sheet.getPhysicalNumberOfRows()!=0 && sheet.getRow(0).getRowNum()==0) {
                row=sheet.getRow(0);
                for(int index=0;index<row.getPhysicalNumberOfCells();index++){
                    header[index]=(row.getCell(index).getStringCellValue());
                }
            }
    
            //if header exists , start reading the values excluding first row (header)
            if(header.length!=0){
                for(int rowIndex=1;rowIndex<sheet.getPhysicalNumberOfRows();rowIndex++){
                    row=sheet.getRow(rowIndex);
                    HashMap<String,Object> eachRow=new HashMap<>();
                    for(int colIndex=0;colIndex<header.length;colIndex++) {
                        String cell = "";
                        if(row.getCell(colIndex)==null){
                            cell="";
                        }else{
                            if(row.getCell(colIndex).getCellType()==Cell.CELL_TYPE_STRING){
                                cell = row.getCell(colIndex).getRichStringCellValue().toString();
                            }else{
                                row.getCell(colIndex).setCellType(Cell.CELL_TYPE_STRING);
                                cell=row.getCell(colIndex).getStringCellValue();
                            }
                        }
                        eachRow.put(header[colIndex], cell);
                    }
                    JSONObject eachRowJsonObject = new JSONObject(eachRow);
                    singleSheetList.add(eachRowJsonObject);
                }
            }
            totalSheetList.add(singleSheetList);
        }
        return totalSheetList;
    }
    
    0 讨论(0)
  • 2021-01-05 17:52

    Add following jars and add them to your classpath then run your project.

    1. dom4j-1.6.1-sources.jar
    2. dom4j.jar
    3. log4j-1.2.17.jar
    4. poi-3.5-FINAL.jar
    5. poi-ooxml-3.5-beta5.jar
    6. poi-ooxml-schemas-3.9.jar
    7. xmlbeans-2.3.0.jar
    0 讨论(0)
  • 2021-01-05 17:56

    Add following jar files:

    • poi-3.9.jar
    • poi-ooxml-3.9.jar
    • poi-ooxml-schemas-3.7.jar
    • xmlbeans-2.3.0.jar
    • dom4j-1.6.1.jar
    0 讨论(0)
提交回复
热议问题