01 -java 读 / 写 excel

1. pom.xml

     <!-- 日期时间工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--读取excel文档数据依赖的包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>


2. java 代码读excel文件 (针对07版本的excel)

 //对excel 进行读
    @Test
    public void TestRead07() throws IOException {
        //1 获取输出流
        String path="/Users/lvxiaokai/Desktop/1.xlsx";//指定文件的路径
        FileInputStream is = new FileInputStream(path);
        Workbook workbook = new XSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        //读取第一行 第一列
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        //输出单元内容
        System.out.println("输出单元内容");
        System.out.println(cell.getStringCellValue());
        //操作结束。关闭文件
        is.close();
    }

3. java 代码写 Excel 文件(针对07版本的excel)

  //对 Excel 进行写
    @Test
    public void TestWrite07() throws IOException {
       //1创建workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        //2 根据workbook创建sheet
        XSSFSheet sheet1 = workbook.createSheet("会员列表");
        //3 根据sheet创建row ->行
        XSSFRow row1 = sheet1.createRow(0);
        //4 根据行创建cell ->列
        XSSFCell cell1 = row1.createCell(0);
        //5 向cell里面设置值
        cell1.setCellValue("lucy");
        //6 使用输出流写到文件中
        String path ="/Users/lvxiaokai/Desktop/3.xlsx" ;//目标文件的本地路径
        FileOutputStream out = new FileOutputStream(path);
        //7 把workbook内容通过输出流写入到文件中
        workbook.write(out);
        //关闭流
        out.close();
        System.out.println("使用输出流写到文件中成功!");

    }

4. 针对03版本的excel pom.xml中的依赖jar包

<!-- 日期时间工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--读取03版本excel文档数据依赖的包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

java 代码参考如下

 //对03版本excel 进行读
    @Test
    public void TestRead03() throws IOException {
        //1 获取输出流
        String path="/Users/lvxiaokai/Desktop/1.xls";//指定文件的路径
        FileInputStream is = new FileInputStream(path);
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        //读取第一行 第一列
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        //输出单元内容
        System.out.println("输出单元内容");
        System.out.println(cell.getStringCellValue());
        //操作结束。关闭文件
        is.close();
    }

    //对对03版本 Excel 进行写
    @Test
    public void TestWrite03() throws IOException {
        //1创建workbook
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2 根据workbook创建sheet
        HSSFSheet sheet1 = workbook.createSheet("会员列表");
        //3 根据sheet创建row ->行
        HSSFRow row1 = sheet1.createRow(0);
        //4 根据行创建cell ->列
        HSSFCell cell1 = row1.createCell(0);
        //5 向cell里面设置值
        cell1.setCellValue("lucy");
        //6 使用输出流写到文件中
        String path ="/Users/lvxiaokai/Desktop/3.xls" ;//目标文件的本地路径
        FileOutputStream out = new FileOutputStream(path);
        //7 把workbook内容通过输出流写入到文件中
        workbook.write(out);
        //关闭流
        out.close();
        System.out.println("使用输出流写到文件中成功!");

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,729评论 0 3
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,700评论 0 4
  • 阿里巴巴 JAVA 开发手册 1 / 32 Java 开发手册 版本号 制定团队 更新日期 备 注 1.0.0 阿...
    糖宝_阅读 7,684评论 0 5
  • 1.编写POM Maven项目的核心文件是pom.xml,POM(Project Objcet Model)项目对...
    zlcook阅读 5,942评论 7 26
  • 【0307我在悦读】萧萧 第20次打卡 (ps:清单主题营来啦,等老铁你来一起报名) 书名:《力哥说理财》 作者:...
    萧萧姐阅读 155评论 0 1