Spring Boot项目实战之POI开放源码函式库入门

一、POI简介

  官网:http://poi.apache.org/

  Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Micrsoft Office格式档案读和写的功能。
   HSSF: 提供读写Microsoft Excel格式档案的功能。(.xls)

  XSSF : 提供读写Microsoft Excel OOXML格式档案的功能。(.xlsx)

  HWPF: 提供读写Microsoft Word格式档案的功能。

  HSLF :提供读写Microsoft PowerPoint格式档案的功能。

  HDGF : 提供读写Microsoft Visio格式档案的功能。

二、excel版本

  (1)03excel版本:xls,最大储存行数是65536;

  (2)07excel版本:xlsx

三、excel操作术语

  (1)整个excel文档称为workbook

  (2)每个workbook包括多个sheet(工作单元);

  (3)每个sheet包括多个row(行);

  (4)每个row包括多个cell(单元格/列);

  (5)每个cell包括多个内容。

四、POI大数据写操作

  (1)03版本excel中存储行数限制是65536,使用对象HSSFWorkbook;

  (2)07版本excel中能存储,使用对象XSSFWorkbook,但是效率很低;

  (3)写入大数据量的execl文件内容,在poi提供了一个专门针对大数据写操作的对象SXSSFWorkbook,推荐使用;

  (4)批处理操作。

03&07版本写测试类
/**
     * 功能描述
     * @MethodName: textWriterExcel03
     * @MethodParam: []
     * @Return: void
     * @Description: TODO--03Excel版本测试写操作(.xls)
     * @Author: LinHong
     * @CreateDate: 2019/11/18 15:43
     */
    @Test
    public void textWriterExcel03() throws IOException {
        //1、创建workbook( 工作簿 )
        Workbook workbook = new HSSFWorkbook();
        //2、使用workbook( 工作表 名为"xx管理")
        Sheet sheet = workbook.createSheet("xx管理");
        //3、使用sheet创建row(行)
        Row row = sheet.createRow(0);
        //4、使用row创建cell(单元格)
        Cell cell = row.createCell(0);
        //5、向cell设置值
        cell.setCellValue("Hello World!");
        //6、使用流的方式写入到文件中
        FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xls");

        workbook.write(out);
        //关闭文件
        out.close();
    }
    /**
     * 功能描述
     * @MethodName: textWriterExcel07
     * @MethodParam: []
     * @Return: void
     * @Description: TODO--07Excel版本测试写操作(.xlsx)
     * @Author: LinHong
     * @CreateDate: 2019/11/18 15:44
     */
    @Test
    public void textWriterExcel07() throws IOException {
        //1、创建workbook(工作簿)
        Workbook workbook = new XSSFWorkbook();
        //2、使用workbook(工作表 名为"xx管理")
        Sheet sheet = workbook.createSheet("xx管理");
        //3、使用sheet创建row(行)
        Row row = sheet.createRow(0);
        //4、使用row创建cell(单元格)
        Cell cell = row.createCell(0);
        //5、向cell设置值
        cell.setCellValue("Hello World!");
        //6、使用流的方式写入到文件中
        FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xlsx");

        workbook.write(out);
        //关闭文件
        out.close();
    }
03版本大数据写测试类
 /**
     * 功能描述
     * @MethodName: textWriterExcel031
     * @MethodParam: []
     * @Return: void
     * @Description: TODO--03Excel版本大数据测试写操作(.xls)65536
     * @Author: LinHong
     * @CreateDate: 2019/11/18 15:43
     */
    @Test
    public void textWriterExcel031() throws IOException {
        //1、创建workbook( 工作簿 )
        Workbook workbook = new HSSFWorkbook();
        //2、使用workbook( 工作表 名为"xx管理")
        Sheet sheet = workbook.createSheet("xx管理");

        try {
            for (int i = 0; i <=66666; i++) { //会报错
                //3、使用sheet创建row(行)
                Row row = sheet.createRow(i);
                //4、使用row创建cell(单元格)
                Cell cell = row.createCell(0);
                //5、向cell设置值
                cell.setCellValue("Hello World!"+i);
            }

            //6、使用流的方式写入到文件中
            FileOutputStream out = new FileOutputStream("E:/(000)DevelopTools/poi-test-space/test-write03.xls");

            workbook.write(out);
            //关闭文件
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
报错

在这里插入图片描述

五、POI读操作
(1)03版本
  /**
     * 功能描述
     * @MethodName: textReadExcel03
     * @MethodParam: []
     * @Return: void
     * @Description: TODO 03Excel版本测试读操作
     * @Author: LinHong
     * @CreateDate: 2019/11/18 15:42
     */
    @Test
    public void textReadExcel03() throws Exception{
        //1、获取文件
        FileInputStream inputStream = new FileInputStream("E:/(000)DevelopTools/poi-test-space/test-read03.xls");
        //2、创建workbook
        Workbook workbook = new HSSFWorkbook(inputStream);
        //3、获取sheet
        Sheet sheet = workbook.getSheetAt(0);
        //4、获取row
        Row row = sheet.getRow(0);
        //5、获取cell
        Cell cell1 = row.getCell(0);
        Cell cell2 = row.getCell(1);
        Cell cell3 = row.getCell(2);
        //6、获取cell值
        String stringCellValue1 = cell1.getStringCellValue();
        //注意类型 String stringCellValue2 = cell2.getStringCellValue();
        double numericCellValue2 = cell2.getNumericCellValue();
        String stringCellValue3 = cell3.getStringCellValue();

        System.out.println(stringCellValue1);
        System.out.println(numericCellValue2);
        System.out.println(stringCellValue3);
    }
(2)07版本同上

  小结:excel读操作时,excel的行数可以不确定,但是多少列和每列的内容必须是规定好的!

♚学习、实战、总结、分享,让生活变得更美好!
☞林大侠博客:https://coding0110lin.blog.csdn.net/  欢迎转载,一起技术交流探讨!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值