23 读取excel

Posted life_start

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }

 

以上是关于23 读取excel的主要内容,如果未能解决你的问题,请参考以下文章

Python3 关于excel 文件格式xls之读取写入和追加

如何用java导入Excel数据到数据库?

Java-poi-excel-对空值单元格的读取

想用vs2010的c,读取excel里的数字,怎么读取呢

java 取Excel带有小数点数据,不能取完整,只能取小数点的后面2位,怎么解决?

onActivityResult 未在 Android API 23 的片段上调用