0

I use Apache-poi 3.9 into my Struts 1.3.10 project. I have two errors when i compile in this functionality:

private boolean parserHSSFSheet(HSSFSheet pageAccord, StringBuffer idPaa, StringBuffer idGroupe,
            StringBuffer idFournisseur, StringBuffer idFamille, StringBuffer periode, Map<Integer, Marque> mapMarque,
            Map<Integer, Ristournable> mapRistournable, Map<Integer, PerimetreProduitEnum> mapTypeDeclaration,
            Map<Integer, FamilleDeProduitsNomenclature> mapFamille, Map<Integer, String> mapMarqueProduit,
            TreeMap<Integer, TreeMap<Integer, BigDecimal>> mapColonneAdherentMontant,
            TreeMap<Integer, BigDecimal> mapAdherentQuantite) throws Exception {

...   

for (Iterator<HSSFRow> rit = pageAccord.rowIterator(); rit.hasNext();) {
    HSSFRow row = (HSSFRow) rit.next();

    String typeCellule = "";

    for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>) row.cellIterator(); cit.hasNext();) {
        HSSFCell cell = cit.next();

        if (cell.getCellNum() == ((short) 0)) {

...

}

Errors:

pageAccord.rowIterator();

Type mismatch: cannot convert from Iterator to Iterator

And

(Iterator<HSSFCell>) row.cellIterator();

Cannot cast from Iterator to Iterator

3 Answers 3

2

Have you seen the docs?? https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html says that rowIterator returns java.util.Iterator<Row> so you cannot cast it "onwards". The same is regarding cell etc.

change

Iterator<HSSFRow> rit = pageAccord.rowIterator(); rit.hasNext();

To

Iterator<Row> rit = pageAccord.rowIterator(); rit.hasNext();

And do the same for the cellIterator

Second cast, Cell into HSSFCell should work if iterator indeed will return compatibile type with HSSFCell.

Sign up to request clarification or add additional context in comments.

Comments

2

As per the API docs a call to

  • pageAccord.rowIterator() returns a java.util.Iterator<Row>. See here.
  • row.cellIterator() returns a java.util.Iterator<Cell>. See here.

Both Row and Cell are only interfaces. Still I would work with those when possible and defer the explicit downcasting to places where this is actually necessary (and allowed).

Thus: Amend your iterators to comply with the types listed above (this could also mean using a generic Iterator<?> in places) and only downcast later (such as in your HSSFRow row = (HSSFRow) rit.next();).

Comments

-2

Please find the below program for the quick solution

workbook = WorkbookFactory.create(new FileInputStream(envFilePath + "\\"+ listOfFiles[i].getName()));
                            // Get the first sheet.
                            Sheet sheet = workbook.getSheetAt(0);

                            // Get the first cell.
                            Row row = sheet.getRow(0);
                            Cell cell = row.getCell(0);

Test program which can help you

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class TestExcelFile {

    public static void main(String[] args) {
        String envFilePath = System.getenv("AZURE_FILE_PATH");

        // upload list of files/directory to blob storage
        File folder = new File(envFilePath);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());

                Workbook workbook;
                try {
                        workbook = WorkbookFactory.create(new FileInputStream(envFilePath + "\\"+ listOfFiles[i].getName()));
                        // Get the first sheet.
                        Sheet sheet = workbook.getSheetAt(0);

                        // Get the first cell.
                        Row row = sheet.getRow(0);
                        Cell cell = row.getCell(0);

                        // Show what is being read.
                        System.out.println(cell.toString());
                        for (Cell cell1 : row) {
                            System.out.println(cell1.toString());
                        }
                } catch (InvalidFormatException | IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2 Comments

As per poi.apache.org/spreadsheet/quick-guide.html#FileInputStream don't use an InputStream if you have a File!
This problem resolved from 3.14 version of POI dependency, if some one use below version then this is the solution. I am using the 3.9 Library to my project, File not possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.