码迷,mamicode.com
首页 > 其他好文 > 详细

23 读取excel

时间:2016-11-08 20:23:09      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:cat   public   return   hbase   int   camera   final   gets   book   

    /**
     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;
    }

 

23 读取excel

标签:cat   public   return   hbase   int   camera   final   gets   book   

原文地址:http://www.cnblogs.com/yangh2016/p/6044203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!