Example usage for org.apache.poi.hssf.usermodel HSSFRow getCell

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getCell

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow getCell.

Prototype

@Override
public HSSFCell getCell(int cellnum) 

Source Link

Document

Get the hssfcell representing a given column (logical cell) 0-based.

Usage

From source file:com.cms.utils.ExcelReader.java

public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow,
        Map<Integer, HSSFCellStyle> styleMap) {
    Set<CellRangeAddress> mergedRegions = new TreeSet<CellRangeAddress>();
    destRow.setHeight(srcRow.getHeight());
    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
        HSSFCell oldCell = srcRow.getCell(j);
        HSSFCell newCell = destRow.getCell(j);
        if (oldCell != null) {
            if (newCell == null) {
                newCell = destRow.createCell(j);
            }//  w  ww  . ja  v  a  2  s.  com
            copyCell(oldCell, newCell, styleMap);
            CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),
                    (short) oldCell.getColumnIndex());
            if (mergedRegion != null) {
                CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(),
                        mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn());
                if (isNewMergedRegion(newMergedRegion, mergedRegions)) {
                    mergedRegions.add(newMergedRegion);
                    destSheet.addMergedRegion(newMergedRegion);
                }
            }
        }
    }

}

From source file:com.cn.controller.BaseInfoController.java

/**
 * ?//ww w .j  ava 2  s  . c  o  m
 *
 * @param fileName
 * @return
 */
public int importData(String fileName) {
    InputStream inputStream = null;
    try {
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<BaseInfo> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 4) {
                BaseInfo info = new BaseInfo();
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(0).getStringCellValue());
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(1).getStringCellValue());
                if (Units.strIsEmpty(info.getJianHao()))
                    continue;
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setCarNum((int) row.getCell(3).getNumericCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());

                imports.add(info);
            }
        }
        DatabaseOpt opt;
        Connection conn = null;
        CallableStatement statement = null;
        opt = new DatabaseOpt();
        try {
            conn = opt.getConnect();
            conn.setAutoCommit(false);
            statement = conn.prepareCall("insert into tbBaseInfo(baseId, pinMing, jianHao, carModel, carNum)"
                    + "values(BASEID.NEXTVAL, ?, ?, ?, ?)");
            for (BaseInfo infoImport : imports) {
                statement.setString(1, infoImport.getPinMing());
                statement.setString(2, infoImport.getJianHao());
                statement.setString(3, infoImport.getCarModel());
                statement.setInt(4, infoImport.getCarNum());
                statement.addBatch();
            }
            statement.executeBatch();
            conn.commit();
            return 0;
        } catch (SQLException ex) {
            try {
                if (conn != null)
                    conn.rollback();
            } catch (SQLException ex1) {
                logger.error("?", ex1);
            }
            logger.error("?", ex);
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                logger.error("?", ex);
            }
        }

    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.MaterialBaseInfoController.java

/**
 * ?//from   w w w .  j a  va  2s . c  om
 *
 * @param fileName
 * @param carrierName
 * @return
 */
public int importData(String fileName, String carrierName) {
    InputStream inputStream = null;
    try {
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<MaterialBaseInfo> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 4) {
                MaterialBaseInfo info = new MaterialBaseInfo();
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(0).getStringCellValue());
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(1).getStringCellValue());
                if (Units.strIsEmpty(info.getJianHao()))
                    continue;
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductJianHao(row.getCell(3).getStringCellValue());
                row.getCell(4).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setCarNum((int) row.getCell(4).getNumericCellValue());

                imports.add(info);
            }
        }
        DatabaseOpt opt;
        Connection conn = null;
        CallableStatement statement = null;
        opt = new DatabaseOpt();
        try {
            conn = opt.getConnect();
            conn.setAutoCommit(false);
            statement = conn.prepareCall(
                    "insert into tbMaterialBaseInfo(materialBaseId, pinMing, jianHao, carModel, carNum, carrierName, productJianHao)"
                            + "values(MATERIALBASEID.NEXTVAL, ?, ?, ?, ?, ?, ?)");
            for (MaterialBaseInfo infoImport : imports) {
                statement.setString(1, infoImport.getPinMing());
                statement.setString(2, infoImport.getJianHao());
                statement.setString(3, infoImport.getCarModel());
                statement.setInt(4, infoImport.getCarNum());
                statement.setString(5, carrierName);
                statement.setString(6, infoImport.getProductJianHao());
                statement.addBatch();
            }
            statement.executeBatch();
            conn.commit();
            return 0;
        } catch (SQLException ex) {
            try {
                if (conn != null)
                    conn.rollback();
            } catch (SQLException ex1) {
                logger.error("?", ex1);
            }
            logger.error("?", ex);
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                logger.error("?", ex);
            }
        }

    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.MaterialInController.java

public int importData(String fileName, String carrierName) {
    InputStream inputStream = null;
    try {/*from   ww  w . j  a  va 2  s.  c om*/
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<MaterialIn> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 5) {
                MaterialIn info = new MaterialIn();

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(0).getStringCellValue());
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(1).getStringCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setCarCount((int) row.getCell(3).getNumericCellValue());
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                info.setRemark(row.getCell(4).getStringCellValue());

                imports.add(info);
            }
        }
        return batchAddMaterial(carrierName, imports);

    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.MaterialInitController.java

/**
 * ????/*ww  w .jav  a  2  s  . c  om*/
 *
 * @param fileName
 * @param carrierName
 * @return
 */
public int importData(String fileName, String carrierName) {
    InputStream inputStream = null;
    try {
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<MaterialInit> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 4) {
                MaterialInit info = new MaterialInit();
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(0).getStringCellValue());
                if (Units.strIsEmpty(info.getJianHao())) {
                    continue;
                }
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(1).getStringCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setInitNum((int) row.getCell(3).getNumericCellValue());

                if (row.getCell(4).getCellType() == Cell.CELL_TYPE_STRING) {
                    info.setInitDate(row.getCell(4).getStringCellValue());
                } else {
                    if (HSSFDateUtil.isCellDateFormatted(row.getCell(4))) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue());
                        info.setInitDate(dateFormat.format(date));
                    } else {
                        DecimalFormat df = new DecimalFormat("0");
                        info.setInitDate(df.format(row.getCell(4).getNumericCellValue()));
                    }
                }
                info.setCarrierName(carrierName);
                imports.add(info);
            }
        }

        return batchAddData(carrierName, imports);
    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.MaterialOutController.java

public int importData(String fileName, String carrierName) {
    InputStream inputStream = null;
    try {/*from w w  w  . j a  va 2  s  . c om*/
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<MaterialOut> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 5) {
                MaterialOut info = new MaterialOut();

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(0).getStringCellValue());
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(1).getStringCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setCarCount((int) row.getCell(3).getNumericCellValue());
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                info.setRemark(row.getCell(4).getStringCellValue());
                imports.add(info);
            }
        }
        return batchAddMaterial(carrierName, imports);
    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.OrderController.java

/**
 * ?(???, ?)//from   www . j a v  a 2  s.  co  m
 *
 * @param fileName
 * @param productLineId
 * @param isEmergency
 * @param isGuanShu
 * @return
 * @throws IllegalStateException
 */
public int importOrder(String fileName, int productLineId, int isEmergency, int isGuanShu)
        throws IllegalStateException {
    InputStream inputStream = null;
    try {
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<OrderInfo> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 8) {
                OrderInfo info = new OrderInfo();
                info.setProductLineId(productLineId);

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductCommand(row.getCell(0).getStringCellValue());
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductCode(row.getCell(1).getStringCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductName(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductUnit(row.getCell(3).getStringCellValue());
                row.getCell(4).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setPlanNum((int) row.getCell(4).getNumericCellValue());
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                info.setProductStandard(row.getCell(5).getStringCellValue());

                if (row.getCell(6).getCellType() == Cell.CELL_TYPE_STRING) {
                    info.setPlanFinishTime(row.getCell(6).getStringCellValue());
                } else {
                    if (HSSFDateUtil.isCellDateFormatted(row.getCell(6))) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = HSSFDateUtil.getJavaDate(row.getCell(6).getNumericCellValue());
                        info.setPlanFinishTime(dateFormat.format(date));
                    } else {
                        DecimalFormat df = new DecimalFormat("0");
                        info.setPlanFinishTime(df.format(row.getCell(6).getNumericCellValue()));
                    }
                }
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                info.setCustomerName(row.getCell(7).getStringCellValue());
                imports.add(info);
            }
        }

        DatabaseOpt opt;
        Connection conn = null;
        CallableStatement statement = null;
        opt = new DatabaseOpt();
        try {
            conn = opt.getConnect();
            conn.setAutoCommit(false);
            statement = conn.prepareCall("{call [tbAddOrderInfo](?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}");
            for (OrderInfo infoImport : imports) {
                statement.setInt("productLineId", infoImport.getProductLineId());
                statement.setInt("planNum", infoImport.getPlanNum());
                statement.setString("productCommand", infoImport.getProductCommand());
                statement.setString("productCode", infoImport.getProductCode());
                statement.setString("productName", infoImport.getProductName());
                statement.setString("productUnit", infoImport.getProductUnit());
                statement.setString("productStandard", infoImport.getProductStandard());
                statement.setString("planFinishedTime", infoImport.getPlanFinishTime());
                statement.setInt("isEmergency", isEmergency);
                statement.setInt("isGuanShu", isGuanShu);
                statement.setString("customerName", infoImport.getCustomerName());
                statement.addBatch();
            }
            statement.executeBatch();
            conn.commit();
            return 0;
        } catch (SQLException ex) {
            try {
                if (conn != null)
                    conn.rollback();
            } catch (SQLException ex1) {
                logger.error("?", ex1);
            }
            logger.error("?", ex);
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                logger.error("?", ex);
            }
        }

    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.controller.ProductInitController.java

/**
 * ????/*from  w  w w  .j a va 2 s  . c  o  m*/
 *
 * @param fileName
 * @param carrierName
 * @return
 */
public int importData(String fileName, String carrierName) {
    InputStream inputStream = null;
    try {
        File file = new File(fileName);
        inputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        ArrayList<ProductInit> imports = new ArrayList<>();
        for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
            //                logger.info("count row num:" + sheet.getPhysicalNumberOfRows() + ",the row num is:" + i);
            HSSFRow row = sheet.getRow(i);
            if (null == row) {
                continue;
            }

            int cellNum = row.getPhysicalNumberOfCells();
            //                logger.info("count cell num is:" + cellNum);
            if (cellNum >= 4) {
                ProductInit info = new ProductInit();
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                info.setJianHao(row.getCell(0).getStringCellValue());
                if (Units.strIsEmpty(info.getJianHao()))
                    continue;
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                info.setPinMing(row.getCell(1).getStringCellValue());
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                info.setCarModel(row.getCell(2).getStringCellValue());
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);
                info.setInitNum((int) row.getCell(3).getNumericCellValue());

                if (row.getCell(4).getCellType() == Cell.CELL_TYPE_STRING) {
                    info.setInitDate(row.getCell(4).getStringCellValue());
                } else {
                    if (HSSFDateUtil.isCellDateFormatted(row.getCell(4))) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue());
                        info.setInitDate(dateFormat.format(date));
                    } else {
                        DecimalFormat df = new DecimalFormat("0");
                        info.setInitDate(df.format(row.getCell(4).getNumericCellValue()));
                    }
                }
                info.setCarrierName(carrierName);
                imports.add(info);
            }
        }
        DatabaseOpt opt;
        Connection conn = null;
        CallableStatement statement = null;
        opt = new DatabaseOpt();
        try {
            conn = opt.getConnect();
            conn.setAutoCommit(false);
            statement = conn.prepareCall(
                    "insert into tbProductInit(productInitId, pinMing, jianHao, carModel, initNum, initDate, carrierName)"
                            + "values(productInitId.NEXTVAL, ?, ?, ?, ?, ?, ?)");
            for (ProductInit infoImport : imports) {
                statement.setString(1, infoImport.getPinMing());
                statement.setString(2, infoImport.getJianHao());
                statement.setString(3, infoImport.getCarModel());
                statement.setInt(4, infoImport.getInitNum());
                statement.setString(5, infoImport.getInitDate());
                statement.setString(6, infoImport.getCarrierName());
                statement.addBatch();
            }
            statement.executeBatch();
            conn.commit();
            return 0;
        } catch (SQLException ex) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException ex1) {
                logger.error("?", ex1);
            }
            logger.error("?", ex);
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                logger.error("?", ex);
            }
        }

    } catch (FileNotFoundException ex) {
        logger.error("", ex);
    } catch (IOException ex) {
        logger.error("IO", ex);
    } finally {
        try {
            if (null != inputStream) {
                inputStream.close();
            }
        } catch (IOException ex) {
            logger.error("?", ex);
        }
    }
    return -1;
}

From source file:com.cn.util.ExcelImport.java

/**
* ?2003excel// w w  w .j  a va2s.c o m
* @param file
* @return
*/
private static List<List<Object>> read2003Excel(InputStream inputStream) throws IOException {
    List<List<Object>> dataList = new ArrayList<>();
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = null;
    HSSFCell cell = null;
    Object val = null;
    DecimalFormat df = new DecimalFormat("0");// ?
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ?

    for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
        row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        List<Object> objList = new ArrayList<>();
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell == null) {
                val = null;
                objList.add(val);
                continue;
            }
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else if ("General".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else {
                    val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                }
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                val = cell.getBooleanCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                val = "";
                break;
            default:
                val = cell.toString();
                break;
            }
            objList.add(val);
        }
        dataList.add(objList);
    }
    return dataList;
}

From source file:com.cn.util.ExcelImport.java

/**
 * ?excel// www .j a  v  a2  s  .co  m
 * @param file
 * @return
 * @throws IOException
 */
public static String[] readExcelHead(File file) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = null;
    HSSFCell cell = null;
    row = sheet.getRow(0);
    String[] buff = new String[row.getLastCellNum()];
    for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
        cell = row.getCell(i);
        buff[i] = cell.getStringCellValue();
    }
    return buff;
}