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

前端 未结 5 1911
遇见更好的自我
遇见更好的自我 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:45

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

    Have these dependencies
        
            org.apache.poi
            poi-ooxml
            3.9
        
        
            org.apache.commons
            commons-collections4
            4.0
        
    
    
    /**
     * 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 excelFileReader(String filePath){
    
        ListtotalSheetList=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 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 eachRow=new HashMap<>();
                    for(int colIndex=0;colIndex

提交回复
热议问题