我在Struts 1.3.10项目中使用Apache-poi3.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();
不能从迭代器转换为迭代器
发布于 2016-01-20 14:04:36
您见过这些文档吗??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兼容的类型,则将HSSFCell转换为HSSFCell。
发布于 2016-01-20 14:13:34
发布于 2017-03-21 10:57:16
请查找下面的快速解决方案程序
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();
}
}
}
}
}https://stackoverflow.com/questions/34901837
复制相似问题