Excel读取

来源:互联网 发布:ubuntu关闭apache服务 编辑:程序博客网 时间:2024/06/06 19:14

1、读取,传入excel地址,如:D:\1.xls

public static List Excel(String path) throws Exception {        FileInputStream file = new FileInputStream(path);        HSSFWorkbook wb = new HSSFWorkbook(file);        HSSFSheet sheet = wb.getSheetAt(0);        List<Map<String, Object>> mapList = new ArrayList<>();        //获取Excel总行数,然后循环        for (int j = 2; j <= sheet.getLastRowNum(); j++) {            Row row = sheet.getRow(j);//获取某行            int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();//获得总列数            Map<String, Object> map = new HashMap<>();            String merc_order_no = getCell((HSSFCell) row.getCell(4));            System.out.println("order_amt:" + order_amt + ",");        }}public static String getCell(HSSFCell cell) {        if (cell == null)            return "";        switch (cell.getCellType()) {            case HSSFCell.CELL_TYPE_NUMERIC:                return cell.getNumericCellValue() + "";            case HSSFCell.CELL_TYPE_STRING:                return cell.getStringCellValue();            case HSSFCell.CELL_TYPE_FORMULA:                return cell.getCellFormula();            case HSSFCell.CELL_TYPE_BLANK:                return "";            case HSSFCell.CELL_TYPE_BOOLEAN:                return cell.getBooleanCellValue() + "";            case HSSFCell.CELL_TYPE_ERROR:                return cell.getErrorCellValue() + "";        }        return "";    }
原创粉丝点击