我正在尝试使用Java解析带有.csv结尾的excel文件。在做了一些研究之后,我下载并安装了ApachePOI库。但是,每次我尝试打开要解析的excel文件时,都会出现以下错误:
Exception in thread "main" java.io.IOException: Invalid header signature; read 0x4E2C53454C494D53, expected 0xE11AB1A1E011CFD0
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
at excellibrarycreation.ExcelFileProcesser.processFile(ExcelFileProcesser.java:40)
at excellibrarycreation.ExcelLibraryCreation.main(ExcelLibraryCreation.java:24)
Java Result: 1
我在Stack Overflow和其他网站上对这个问题进行了更多的研究,但是答案对我来说非常混乱,因为我从未听说过OLE2文件和标题签名以及类似的东西。如果有人能够解释问题,我会非常感激。这是我的代码:
public void processFile(File excelWorkbook) throws FileNotFoundException, IOException{
System.out.println("Processing file...");
FileInputStream fileInputStream = new FileInputStream(excelWorkbook);
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = firstSheet.iterator();
while (rowIterator.hasNext()){
Row row = rowIterator.next();
Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()){
org.apache.poi.ss.usermodel.Cell cell = cellIterator.next();
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("Cell type is boolean: "+cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println("Cell type is numeric: "+cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println("Cell type is String: "+cell.getStringCellValue());
break;
}
System.out.println("");
}
fileInputStream.close();
}
}
答案 0 :(得分:2)
为什么使用Apache POI读取逗号分隔值文件?
您可以使用opencsv。
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
答案 1 :(得分:1)
如果您没有尝试打开真正的.xls Excel文件,POI的HSSF根本无法帮助您。 HSSF用于打开97 Excel格式的.xls文件。它不适用于其他任何事情。如果您使用逗号分隔的.csv文件,请参考Paul Vargas。