gpt4 book ai didi

java - CellType 上的 Apache POI 4.0.0 编译错误

转载 作者:行者123 更新时间:2023-11-30 02:02:19 28 4
gpt4 key购买 nike

编译失败并出现此错误:

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:如果我将“getCellType”替换为“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();
}
}

}

文件导入.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>

最佳答案

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

关于java - CellType 上的 Apache POI 4.0.0 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52439572/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com