Apache-poi“编译错误:类型不匹配”

时间:2016-01-20 13:48:40

标签: java apache-poi

我在我的Struts 1.3.10项目中使用Apache-poi 3.9。 当我编译这个功能时,我有两个错误:

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)) {

...

}

错误:

pageAccord.rowIterator();

  

类型不匹配:无法从迭代器转换为迭代器

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

  

无法从迭代器转换为迭代器

3 个答案:

答案 0 :(得分:2)

你看过文档吗? https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html表示rowIterator返回java.util.Iterator<Row>,因此您无法“向前”投放它。关于细胞等也一样。

变化

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

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

cellIterator

做同样的事情

如果迭代器确实将返回与Cell的兼容类型,那么第二次转换HSSFCellHSSFCell应该有效。

答案 1 :(得分:2)

根据API文档调用

  • pageAccord.rowIterator()返回java.util.Iterator<Row>。见here
  • row.cellIterator()返回java.util.Iterator<Cell>。见here

RowCell都只是接口。在可能的情况下,我仍然会与那些人合作,并将明确的向下转发推迟到实际需要(并允许)的地方。

因此:修改您的迭代器以符合上面列出的类型(这也可能意味着在地方使用通用Iterator<?>)并且仅在以后进行转发(例如在{{1 }})。

答案 2 :(得分:-2)

请找到以下程序以获得快速解决方案

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);

可以帮助您的测试程序

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();
                }
            }
        }
    }
}
相关问题