23 读取excel

    /**
     index    mappingname            originalname        datatype        family
     0        task_id                taskIndexCode        STRING            info
     1        task_type            resourceDescType    INT32            info
     2        camera_index_code    cameraIndexCode        STRING            info
     3        camera_address        resourceLocation    STRING            info
     * @param path
     * @return
     * @throws IOException
     */
    public static List<String> readHbaseXlsx(String path) throws IOException {

        InputStream is = new FileInputStream(path);

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);

        List<String> jsonList = new ArrayList<>();

        int sheetNum = xssfWorkbook.getNumberOfSheets();

        // Read the Sheet
        for (int numSheet = 0; numSheet < sheetNum; numSheet++) {

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);

            if (xssfSheet == null) {

                continue;
            }
            // Read the Row
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {

                XSSFRow xssfRow = xssfSheet.getRow(rowNum);

                if (xssfRow != null) {

                    XSSFCell indexCell = xssfRow.getCell(0);

                    if (indexCell == null) continue;

                    XSSFCell mappingnameCell = xssfRow.getCell(1);

                    XSSFCell originalnameCell= xssfRow.getCell(2);

                    XSSFCell datatypeCell = xssfRow.getCell(3);

                    XSSFCell familyCell = xssfRow.getCell(4);


                    String index=indexCell.getStringCellValue();
                    String originalname;
                    try {
                        originalname=originalnameCell.getStringCellValue();
                    }catch (NullPointerException e){
                        originalname="";
                    }
                    if(null==originalname){
                        originalname="";
                    }
                    String mappingname=mappingnameCell.getStringCellValue();
                    String datatype=datatypeCell.getStringCellValue();
                    String family=familyCell.getStringCellValue();

                    String json=index+","+originalname+","+mappingname+","+datatype+","+family;
                    jsonList.add(json);

                }
            }

            xssfWorkbook.cloneSheet(numSheet);
        }

        is.close();

        return jsonList;
    }
    /**
     index    mappingname            originalname        datatype        family
     0        task_id                taskIndexCode        STRING            info
     1        task_type            resourceDescType    INT32            info
     2        camera_index_code    cameraIndexCode        STRING            info
     3        camera_address        resourceLocation    STRING            info
     * @param path
     * @return
     * @throws IOException
     */
    public static List<String> readEsXlsx(String path) throws IOException {

        InputStream is = new FileInputStream(path);

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);

        List<String> jsonList = new ArrayList<>();

        int sheetNum = xssfWorkbook.getNumberOfSheets();

        // Read the Sheet
        for (int numSheet = 0; numSheet < sheetNum; numSheet++) {

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);

            if (xssfSheet == null) {

                continue;
            }
            // Read the Row
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {

                XSSFRow xssfRow = xssfSheet.getRow(rowNum);

                if (xssfRow != null) {

                    XSSFCell indexCell = xssfRow.getCell(0);

                    if (indexCell == null) continue;

                    XSSFCell mappingnameCell = xssfRow.getCell(1);

                    XSSFCell originalnameCell= xssfRow.getCell(2);

                    XSSFCell datatypeCell = xssfRow.getCell(3);


                    String index=indexCell.getStringCellValue();
                    String originalname;
                    try {
                        originalname=originalnameCell.getStringCellValue();
                    }catch (NullPointerException e){
                        originalname="";
                    }
                    if(null==originalname){
                        originalname="";
                    }
                    String mappingname=mappingnameCell.getStringCellValue();
                    String datatype=datatypeCell.getStringCellValue();

                    String json=index+","+originalname+","+mappingname+","+datatype;
                    jsonList.add(json);

                }
            }

            xssfWorkbook.cloneSheet(numSheet);
        }

        is.close();

        return jsonList;
    }

    /**
     * @param xssfRow excel cell
     * @return 单元格数据
     * @Method Description 2010excel单元格的数据,单元格数据分为:Boolean、numeric、string
     */
    private static Object getValue(XSSFCell xssfRow) {

        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {

            return xssfRow.getBooleanCellValue();

        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {

            return xssfRow.getNumericCellValue();

        } else {

            return xssfRow.getStringCellValue();

        }
    }

 

写字符串到文件中

public static Boolean writeStringToFile(String filePath,String str){
        Boolean success=true;
        if(!new File(filePath).exists()){
            try {
                new File(filePath).createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            new File(filePath).delete();
            try {
                new File(filePath).createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter fileWriter = null;
        try {
            fileWriter=new FileWriter(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(null!=fileWriter) {
            try {
                fileWriter.write(str);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

 

posted @ 2016-11-08 19:15  life_start  阅读(246)  评论(0编辑  收藏  举报