这个问题在java贴吧里常见,并且有很多方式完成,这里就给你贴一份,你试着在贴吧里和百度知道里,学习程序方面的问题解答。
public String[][] readExcel(String filePath) {
String[][] s = null;
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
s = new String[rows][];
if (rows > 0) {
// 获取总列数`
int cells = sheet.getRow(0).getPhysicalNumberOfCells();
for (int r =0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
String[] cellsvalue = new String[cells+1];
for (short c = 0; c < cells; c++) {
String value = "";
HSSFCell cell = row.getCell(c);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
value = new java.text.SimpleDateFormat(
"yyyy-MM-dd").format(cell
.getDateCellValue());
} else {
value = String.valueOf(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value="";
default:
break;
}
if (cell == null) {
value="";
}
}
cellsvalue[c] = value;
if(value.endsWith(".0")){
cellsvalue[c] = value.substring(0, value.length()-2);
}
}
s[r] = cellsvalue;
java.io.File myfile = new java.io.File(filePath);
if (myfile.exists()) {
myfile.delete();
}
}
}
} catch (Exception ex) {
// TODO 自动生成 catch 块
ex.printStackTrace();
}
return s;
}