我是Java新手,並且遇到導入我的Excel工作表的問題。我試圖使用Apache Poi庫將Excel電子表格打印到Java控制檯中。當我運行我的代碼時,出現以下異常:Excel Java導入問題,頭標籤無效?
java.io.IOException:無效的頭部簽名;閱讀 0x656D614E2C234449,預計0xE11AB1A1E011CFD0
如何解決它,所以它我的頭相匹配?這是這個樣子......
ID#, Name, Age, Street Address, City, State, Zip, Employer, Prescription
下面是我的代碼:
//imports, blah blah, etc...
public static void main(String [] args) {
try {
InputStream input = new BufferedInputStream(new FileInputStream(
"c:/Users/sgoetz/Desktop/Week3ProgrammingData.csv"));
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = row.cellIterator();
while(cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
System.out.print(cell.getNumericCellValue()+" ");
else
if(HSSFCell.CELL_TYPE_STRING==cell.getCellType())
System.out.print(cell.getStringCellValue()+" ");
else
if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType())
System.out.print(cell.getBooleanCellValue()+" ");
else
if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
System.out.print("BLANK ");
else
System.out.print("Unknown cell type");
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我懷疑試圖打開一個'csv'文件,就像它是一個'xls'工作簿的工作,因此文件頭不匹配'xls'format的錯誤。 –
'HSSFWorkbook'現在已經很老了。你應該考慮轉向更可靠和最新的'XSSFWorkbook'。 – christopher