CellType上的Apache POI 4.0.0编译错误

时间:2018-09-21 08:23:17

标签: java apache-poi

编译失败,并显示以下错误:

cannot find symbol
case FORMULA:
symbol: variable FORMULA

接下来的情况下也会发生相同的错误。

我正在将Apache POI 4.0.0与Java 8结合使用。

我的代码是:

try (Workbook wb = WorkbookFactory.create(inputStream)) {
    Sheet sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                    case FORMULA:
                        [code]
                        break;
                }
            }
        }
}

注1:从NetBeans 8.2部署到本地GlassFish 4.1.2可行,但是构建失败。

注2:如果将“ getCellTypeEnum”替换为“ getCellTypeEnum”(已弃用),则构建完成而没有错误。

版本:

  • Java 1.8u172(i586)/然后是Java 1.8u181(i586)
  • poi-4.0.0
  • poi-ooxml-4.0.0
  • poi-ooxml-schemas-4.0.0
  • xmlbeans-3.0.1
  • commons-codec-1.11
  • commons-collections4-4.2
  • commons-logging-1.2
  • commons-math3-3.6.1
  • jsf-api-2.2.15
  • jsf-impl-2.2.15
  • primefaces-6.2.6

完整代码

POIUtils.java

package fr.mycompany.config.files;

import fr.mycompany.config.exception.MyAppException;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.faces.application.FacesMessage;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
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;
import org.apache.poi.ss.usermodel.CellType;

public class POIUtils {

    public static List<List<String>> parseExcelFile(InputStream is) throws MyAppException {

        List<List<String>> lines = new ArrayList<>();

        try (Workbook wb = WorkbookFactory.create(is)) {
            Sheet sheet = wb.getSheetAt(0);
            DataFormatter df = new DataFormatter(true);
            int numRow = 0;
            for (Row row : sheet) {
                int numCol = 0;
                List<String> currentLine = new ArrayList<>();
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case FORMULA:
                            currentLine.add(numCol, "");
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                LocalDate ld = cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                                String frLocalDate = ld.format(DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.FRANCE));
                                currentLine.add(numCol, frLocalDate);
                            } else {
                                String cellValue = df.formatCellValue(cell);
                                currentLine.add(numCol, cellValue);
                            }
                            break;
                        default:
                            String cellValue = df.formatCellValue(cell);
                            currentLine.add(numCol, cellValue);
                            break;
                    }
                    numCol++;
                }
                while (row.getRowNum() > numRow) {
                    lines.add(new ArrayList<>());
                    numRow++;
                }
                lines.add(row.getRowNum(), currentLine);
                numRow++;
            }
            if (lines.isEmpty()) {
                throw new MyAppException(FacesMessage.SEVERITY_WARN, "Le fichier est vide.");
            }
        } catch (IOException ex) {
            throw new MyAppException(FacesMessage.SEVERITY_FATAL, "Une erreur est survenue lors du traitement du fichier.", ex);
        } catch (EncryptedDocumentException ex) {
            throw new MyAppException(FacesMessage.SEVERITY_FATAL, "Le fichier est protégé par un mot de passe et n'a pas pu être lu.", ex);
        }

        return lines;

    }    

}

FileImportBean.java

package fr.mycompany.coordination;

import fr.mycompany.config.exception.MyAppException;
import fr.mycompany.config.files.POIUtils;
import java.io.Serializable;
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.List;
import java.util.ArrayList;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.UploadedFile;

@Named
@ViewScoped
public class FileImportBean implements Serializable {

    private transient UploadedFile file;
    private List<List<String>> parsedContent = new ArrayList<>();

    public void handleFileUpload (FileUploadEvent event) {
        try {
            file = event.getFile();
            if (null != file) {
                switch (FileMagic.valueOf(FileMagic.prepareToCheckMagic(file.getInputstream()))) {
                    case OOXML:
                        parsedContent = POIUtils.parseExcelFile(file.getInputstream());
                        break;
                    default:
                        (new MyAppException(FacesMessage.SEVERITY_ERROR, "Type de fichier non géré.", ex)).doFacesMessage();
                        break;
                }
            } else {
            }
        } catch (IOException ex) {
            (new MyAppException(FacesMessage.SEVERITY_FATAL, "Une erreur est survenue lors du traitement du fichier.", ex)).doFacesMessage();
        } catch (MyAppException ex) {
            ex.doFacesMessage();
        }
    }

}

fileImport.xhtml

<h:form enctype="multipart/form-data">
    <p:messages showDetail="true" />
    <p:fileUpload
        mode="advanced"
        skinSimple="true"
        auto="true"
        multiple="false"
        required="true"
        update="@form"
        fileUploadListener="#{fileImportBean.handleFileUpload}"
</h:form>

1 个答案:

答案 0 :(得分:2)

由于@AxelRichter在评论部分提出的想法,我发现我包含了使用旧版POI(3.15)的JAR(由我的同事制作)。我也将此升级到了4.0.0,瞧! 至少我今天学到了一些东西... 谢谢!