java读取excel2003输出正确,但是还是报空指针错误,求解决,源码如下
publicclassExcel{privatePOIFSFileSystemfs;privateHSSFWorkbookwb;privateHSSFSheetsheet...
public class Excel {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
public static void main(String[] args) throws Exception {
File f=new File("F:/java读写文件/instance.xls");
InputStream ins = new FileInputStream(f);
Excel excelReader=new Excel();
excelReader.readExcelContent(ins);
}
public void readExcelContent(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
System.out.println(rowNum);
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
System.out.println(colNum);
String str = "";
for (int i = 0; i <= rowNum; i++) {
for(int j=0;j<colNum;j++){
row = sheet.getRow(j);
str=getStringCellValue(row.getCell(i));
System.out.println(str+"、");
}
System.out.println();
}
}
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
}
return strCell;
}
} 展开
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
public static void main(String[] args) throws Exception {
File f=new File("F:/java读写文件/instance.xls");
InputStream ins = new FileInputStream(f);
Excel excelReader=new Excel();
excelReader.readExcelContent(ins);
}
public void readExcelContent(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
System.out.println(rowNum);
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
System.out.println(colNum);
String str = "";
for (int i = 0; i <= rowNum; i++) {
for(int j=0;j<colNum;j++){
row = sheet.getRow(j);
str=getStringCellValue(row.getCell(i));
System.out.println(str+"、");
}
System.out.println();
}
}
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
}
return strCell;
}
} 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询