我写了用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 + " ");
}
}}
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 + " ");
}
}}
解决方案 »
- 怎么会有这样的错误提示?java.lang.NullPointerException?
- 关于 socket
- Java中JTable中单元格的内容如何显示成一张图片
- 求一个写文件的代码,要求不覆盖原来的文件
- 请高手救命啊!~!~一个关于JAVA 访问 SQL 的问题 JAVA LinkedList 里面有个东东连接后老是有问题
- 在java中如何实现弹出dos窗口,我的是win2000
- 跟服务器之间传送字节形式的二进制流,入参为String类型,怎样转变呢?50分!
- 问一个Apache的log4j写NT日志的问题
- 哪里有jbuilder的电子书下载?
- 請問怎樣把一個OutputStream 轉換成 InputStream啊,
- SWT中浏览器对象调用HTML文件 相对路径怎么给?
- SSH2问题
java.io.IOException: Invalid header signature; read 8243089445407770957, expected -2226271756974174256
昨晚用poi读word文件,也是Invalid header signature错误,后来发现这个文件是纯文本文件(虽然扩展名是doc)