一个java程序即不报错 也没有任何运行结果,这是为什么?求高手帮我测试一下这个方法?
publicclassRead{//获取想要第几张工作表的数据publicListreadSet(intsheetNumber){List<List>result=new...
public class Read {
// 获取想要第几张工作表的数据
public List readSet(int sheetNumber){
List<List> result = new ArrayList<List>();
HSSFWorkbook workbook=null;//得到工作薄
try {
workbook = new HSSFWorkbook(new FileInputStream("D:/ss.xls"));
} catch (Exception e) {
e.printStackTrace();
}
//获得指定的sheet
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//获得sheet总行数
int rowCount = sheet.getLastRowNum();
if(rowCount < 1){
return result;
}
//遍历行row
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
//获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if(null != row){
Vector<Object> vector=new Vector<Object>();
//获得本行中单元格的个数
int cellCount = row.getLastCellNum();
//遍历列cell
for (short cellIndex = 0; cellIndex < cellCount; cellIndex++) {
HSSFCell cell = row.getCell(cellIndex);
//获得指定单元格中的数据
Object cellStr = this.getCellString(cell);
// map.put(arg0, arg1)
vector.add(cellStr);
}
result.add(vector);
}
}
return result;
}
// 获取一个cell的数据类型
private Object getCellString(HSSFCell cell) {
Object result = null;
if(cell != null){
//单元格型;Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell))
{
result = cell.getDateCellValue();
}else
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
System.out.println("枚举了所有类型");
break;
}
}
return result;
}
} 展开
// 获取想要第几张工作表的数据
public List readSet(int sheetNumber){
List<List> result = new ArrayList<List>();
HSSFWorkbook workbook=null;//得到工作薄
try {
workbook = new HSSFWorkbook(new FileInputStream("D:/ss.xls"));
} catch (Exception e) {
e.printStackTrace();
}
//获得指定的sheet
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//获得sheet总行数
int rowCount = sheet.getLastRowNum();
if(rowCount < 1){
return result;
}
//遍历行row
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
//获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if(null != row){
Vector<Object> vector=new Vector<Object>();
//获得本行中单元格的个数
int cellCount = row.getLastCellNum();
//遍历列cell
for (short cellIndex = 0; cellIndex < cellCount; cellIndex++) {
HSSFCell cell = row.getCell(cellIndex);
//获得指定单元格中的数据
Object cellStr = this.getCellString(cell);
// map.put(arg0, arg1)
vector.add(cellStr);
}
result.add(vector);
}
}
return result;
}
// 获取一个cell的数据类型
private Object getCellString(HSSFCell cell) {
Object result = null;
if(cell != null){
//单元格型;Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell))
{
result = cell.getDateCellValue();
}else
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
System.out.println("枚举了所有类型");
break;
}
}
return result;
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询