在开发过程中工具类可以简化我们的操作,提高代码重用.下面是我操作poi(一个Excel操作组件)的工具类.请大家指点
java 代码
-
- import org.apache.poi.hssf.usermodel.HSSFCell;
-
- public class PoiUtil {
-
- public static String getStringCell(HSSFCell cell) {
- String str = null;
- if (cell != null) {
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_NUMERIC:
- case HSSFCell.CELL_TYPE_FORMULA:
- str = new Long((long) cell.getNumericCellValue()).toString();
- break;
- case HSSFCell.CELL_TYPE_STRING:
- str = cell.getStringCellValue().trim();
- break;
- default:
- break;
- }
- }
-
- return str;
- }
-
- public static double getNumericCell(HSSFCell cell) {
- double doub = 0;
- if (cell != null) {
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_NUMERIC:
- case HSSFCell.CELL_TYPE_FORMULA:
- doub = cell.getNumericCellValue();
- break;
- case HSSFCell.CELL_TYPE_STRING:
- doub = Double.parseDouble(cell.getStringCellValue().trim());
- break;
- default:
- break;
- }
- }
-
- return doub;
- }
-
- public static long getLongCell(HSSFCell cell) {
-
- return (long) getNumericCell(cell);
- }
-
- public static int getIntCell(HSSFCell cell) {
-
- return (int) getNumericCell(cell);
- }
-
- public static String getDateCell(HSSFCell cell) {
- String date = null;
- if (cell != null) {
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_STRING:
- date = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- case HSSFCell.CELL_TYPE_BOOLEAN:
- case HSSFCell.CELL_TYPE_ERROR:
- break;
- default:
- date = DateUtil.date2str(cell.getDateCellValue(), "yyyy-MM-dd");
- }
- }
-
- return date;
- }
- }
不知大家是否还有更好的封装.拿出来交流下~O~