zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    LDA-math-MCMC 和 Gibbs Sampling
    本地blast详解
    【shell命令】拆分、合并、排序、比较文件
    数学中竟然还有这样的定理!
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    glean工具和maker工具
    Perl语言中的ig
    批量替换某个目录下所有文件里的内容
    RepeatMasker使用
    Hi-C文库制备的连接酶连接效果
  • 原文地址:https://www.cnblogs.com/yangh2016/p/6044203.html
Copyright © 2011-2022 走看看