import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class ExcelUtil {
public static Sheet loadExcel(String strPath)
{
HSSFWorkbook wb = null;
POIFSFileSystem fs = null;
Sheet sheet1 = null;
try {
fs = new POIFSFileSystem(new FileInputStream(strPath));
wb = new HSSFWorkbook(fs);
sheet1 = wb.getSheetAt(0);
} catch (IOException e) {
e.printStackTrace();
}
return sheet1;
}
public static String getCellValue(Cell cell)
{
String strValue;
cell.setCellType (Cell.CELL_TYPE_STRING);
strValue = cell.toString();
/*
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
strValue = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
strValue = cell.getDateCellValue().toString();
} else {
strValue = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
strValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
strValue = String.valueOf(cell.getCellFormula());
break;
default:
strValue = "";
}
*/
//System.out.println(strValue);
return strValue;
}
public static String getCellValue(Sheet sheet1,int intRow,int intColumn)
{
Row row = sheet1.getRow(intRow);
Cell cell = row.getCell(intColumn);
return getCellValue(cell);
}
}
调用时:
ExcelUtil.getCellValue(sheet1, 6, 1)
好处,读取 excel中 12345
不会读出12345.0