ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文件流以及Excel解析

2022-09-12 18:01:45  阅读:218  来源: 互联网

标签:文件 sheet Excel System cell println FileInputStream 解析 out


1、excel的常用操作

package com.lemon.excle;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ExcelDemo {

    public static void main(String[] args) throws Exception {
        //poi
        /*
         * 1、加载excel文件
         * 2、获取所有sheet
         * 3、选取第一个sheet
         * 4、获取第一行
         * 5、获取第二列
         * 6、打印出单元格中的内容
         * */
        //1、加载excel
        FileInputStream fis = new FileInputStream("src/test/resources/java24.xlsx");
        //2、获取所有的表单sheet
        Workbook sheets = WorkbookFactory.create(fis);
        //3、从中取第一个sheet
        Sheet sheet = sheets.getSheetAt(0);

        //4、获取第一个sheet表单中的第一行
        Row row = sheet.getRow(0);

        //5、获取第二列
        Cell cell = row.getCell(2);
        System.out.println("第一行 第二列的值为:" + cell);


        //枚举:可穷举,防止调用方式时传入错误的值。
        Cell cell2 = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
        // null new cell();
        cell2.setCellType(CellType.STRING);
        System.out.println(cell);


    }
}

2、Excel的读取操作

package com.lemon.excle;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ExcelRead {

    public static void main(String[] args) throws Exception {
        //1、加载excel文件
        FileInputStream fis = new FileInputStream("src/test/resources/java24.xlsx");
        //2、获取所有sheet
        //多态  xlsx xls
        Workbook sheets = WorkbookFactory.create(fis);
        //3、选取第一个sheet
        Sheet sheet = sheets.getSheetAt(0);

        //4、获取所有的行
        for (Row row : sheet) {
            //5、获取所有的列
            for (Cell cell : row) {
                cell.setCellType(CellType.STRING);
                System.out.println(cell + ",");
            }
            System.out.println("所有行的内容");

        }

        //获取最后一行
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            int lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                cell.setCellType(CellType.STRING);
                System.out.print(cell + ",");
            }
            System.out.println("==========");
        }



    }
}

 3、Excel写入操作

package com.lemon.excle;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ExcelWrite {
    public static void main(String[] args) throws Exception {
        //修改 = 读取 + 改
        //1、加载excel文件
        FileInputStream fis = new FileInputStream("src/test/resources/java24.xls");
        //清空文件
        //2、获取所有sheet
        //多态  xlsx xls
        Workbook sheets = WorkbookFactory.create(fis);
        //3、选取第一个sheet
        Sheet sheet = sheets.getSheetAt(0);
        //4、获取第一行
        Row row = sheet.getRow(0);
        //5、获取第二列
//        Cell cell = row.getCell(1);
        //枚举:可穷举,防止调用方式时传入错误的值。
        Cell cell = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
        //修改前提:必须要拿到对应的单元格,cell。
        cell.setCellValue("姓名");
        //null  new Cell();
        //6、打印出单元格中的内容
        cell.setCellType(CellType.STRING);
        System.out.println(cell);
        //先备份再操作。
        FileOutputStream fos = new FileOutputStream("src/test/resources/java24.xls");
        sheets.write(fos);
        //7、关流
        fis.close();
    }
}

4、Properties

properties如下:

 

 

package com.lemon.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class PropertiesDemo {
    public static void main(String[] args) throws IOException { //抛出异常,我不处理
        //Properties prop = new Properties();//HashMap 表兄弟
        //Properties没有泛型,但是你可以理解为String,String
        //prop.setProperty("username","root");
        //prop.setProperty("password","123456");
        //prop.setProperty("url","127.0.0.1");
        //System.out.println(prop);


        //获取或者写入配置文件。 读取
        //FileOutputStream fos = new FileOutputStream("src/test/resources/config2.properties");
        //写
        //prop.store(fos,"备注");
        //fos.close();

        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("src/test/resources/config2.properties");
        //读取
        prop.load(fis);
        System.out.println(prop);
        System.out.println(prop.getProperty("username"));
        fis.close();


        //IO input output 输入输出流  a
        int a = 10;
        try {
            //放可能会出现异常的代码
            method();
            System.out.println("========1");
            System.out.println("========2");
            System.out.println("========3");
        } catch (Exception e) {
            //出现异常之后,补偿操作。
            e.printStackTrace();
            System.out.println("========4");
        } finally {
            //不论是否发生异常都会执行,释放资源
            System.out.println("==========6");
        }

        System.out.println("========5");
    }

    public static void method() throws Exception { //可以一直往外抛出异常
        FileInputStream fis = new FileInputStream("src/test/resources/config2.properties");
    }
}

  

标签:文件,sheet,Excel,System,cell,println,FileInputStream,解析,out
来源: https://www.cnblogs.com/yuansufeng/p/16686796.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有