我写了用poi读取excel表格中指定列的数据的测试程序,但总是报如下异常:
F:\plan.xls
java.io.FileInputStream@1fb8ee3
java.io.IOException: Invalid header signature; read 8243089445407770957, expected -2226271756974174256
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:112)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:312)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:293)
at test.pro.login.util.ExcelReader.readExcelTitle(ExcelReader.java:24)
at test.pro.login.util.ExcelReader.main(ExcelReader.java:90)
Exception in thread "main" java.lang.NullPointerException
at test.pro.login.util.ExcelReader.readExcelTitle(ExcelReader.java:28)
at test.pro.login.util.ExcelReader.main(ExcelReader.java:90)
我实在找不出那里出错了,希望大家帮帮忙!
程序如下:
package test.pro.login.util;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExcelReader {
private HSSFWorkbook wb;    
private HSSFSheet sheet;    
private HSSFRow row;
    public String[] readExcelTitle() {        
     try { 
     File file=new File("F:\\plan.xls");
     System.out.println(file);
     InputStream f=new FileInputStream(file);
    
     System.out.println(f);
         wb = new HSSFWorkbook(f);        
     } catch (IOException e) {           
     e.printStackTrace();        
     }        
     sheet = wb.getSheetAt(0);        
     row = sheet.getRow(0);        
     // 标题总列数        
     //int colNum = row.getPhysicalNumberOfCells(); 
     short[] colNum={10,0,1,7,2,3,4,6,9,13,20,21,22,23,24,25,26,27,28,30,33,5,14,15,16,17};        
     String[] title = new String[colNum.length];        
     for (int i = 0; i < colNum.length; i++) {            
     //title[i] = getStringCellValue(row.getCell((short) i));            
     title[i] = getCellFormatValue(row.getCell(colNum[i]));
     }        
     return title;    
    }
    private String getCellFormatValue(HSSFCell cell){
     String cellvalue="";
     cellvalue=cell.getRichStringCellValue().getString();
     System.out.print(cellvalue+" ");
     return cellvalue;
    }
/**
 * @param args
 */
public static void main(String[] args) {
ExcelReader excelReader = new ExcelReader();            
String[] title = excelReader.readExcelTitle();            
System.out.println("获得标题:");            
for (String s : title) {                
System.out.print(s + " ");            
}            
}}