简体   繁体   中英

java program to group the data in excel by a specific name using apache poi?

This is my whole program to extract data from excel sheet and sort them. After rectifying the mistakes there is still problem in the code. It is executing fine but displaying nothing Can someone figure out the mistake

package com.pack.group;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class demo {

private void readExcelFile(String fileName) {
           List cellDataList = new ArrayList();
    try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
        POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
        HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
        HSSFSheet hssfSheet = workBook.getSheetAt(0);
        Iterator rowIterator = hssfSheet.rowIterator();

        while (rowIterator.hasNext()) {
            HSSFRow hssfRow = (HSSFRow) rowIterator.next();
            Iterator iterator = hssfRow.cellIterator();
            List cellTempList = new ArrayList();
            while (iterator.hasNext()) {
                HSSFCell hssfCell = (HSSFCell) iterator.next();
                cellTempList.add(hssfCell);
            }
            cellDataList.add(cellTempList);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
       printToConsole(cellDataList);
} private void printToConsole(List cellDataList) {  

    for (int i = 0; i < cellDataList.size(); i++) 

    {
        List cellTempList = (List) cellDataList.get(i);
        if(((HSSFCell) cellTempList.get(2)).toString().equals("1")){
        {
        for (int j = 0; j < cellTempList.size(); j++) {

            HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
            String stringCellValue = hssfCell.toString();

            System.out.print(stringCellValue + "\t");
        }
      }
       }
        else { 
         i++;
        }
        System.out.println();
    }
}
public static void main(String[] args) {
    String fileName = "D:/datasheet.xls";
    new demo().readExcelFile(fileName);
}}

That condition shuold be written like this.

if(((HSSFCell) cellTempList.get(2)).toString().equals("1")){
    //...
}

You were trying to cast a boolean to HSSFCell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM