使用Java语言,引入依赖apache-poi中的Excel文档操作

前言:

|-------|--------------------|-------|
| HSSF | 针对于excel03之前版本 | .xls |
| XSSF | 针对于excel07之后版本 | .xlsx |
| SXSSF | 针对于excel07之后版本,升级版 | .xlsx |

apache-poi官网图片

首先引入相关文档:

java 复制代码
    <dependencies>
        <!--    03- 版本的excel .xls    -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!--   07+ 版本的excel .xlsx   -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

一、读取操作

目标excel文档如下:

  1. 获取通过文件流读取Excel的工作簿Workbook
  2. 通过workbook 获取工作表
  3. 通过sheet获取row行
  4. 通过row获取cell单元格
  5. 使用cell.getStringCellValue()获取单元格的值
java 复制代码
public static void main(String[] args) throws Exception {
        readExcel07();
//        readExcel07All();
    }

    /**
     * 使用文件流读取.xlsx中的某个数据
     * @throws Exception
     */
    public static void readExcel07() throws Exception{
        //1.通过文件流读取Excel工作部
        FileInputStream inputStream = new FileInputStream("./测试.xlsx");
        //2.获取工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);
        //3.获取工作表
        Sheet sheet = workbook.getSheet("07xlsx版本");
        //4.获取行
        Row row = sheet.getRow(0);
        //5.获取列
        Cell cell = row.getCell(0);
//        RichTextString cellValue = cell.getRichStringCellValue();
        String cellValue = cell.getStringCellValue();
        System.out.println(cellValue);

        //关闭资源
        workbook.close();
        inputStream.close();

    }

    /**
     * 读取文件中所有的非空数据
     * @throws Exception
     */
    public static void readExcel07All() throws Exception{
        //1.通过文件流读取Excel工作部
        FileInputStream inputStream = new FileInputStream("./测试.xlsx");
        //2.获取工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);
        //3.获取工作表
        Sheet sheet = workbook.getSheet("07xlsx版本");
        //4.获取行
        Row row = sheet.getRow(0);
        if(row!=null){
            for (int i = 0; i < row.getRowNum(); i++) {
                //5.获取列
                Cell cell = row.getCell(i);
                if(cell!=null){
                    System.out.print(cell.getStringCellValue() + "/t");
                }
            }
        }
        //关闭资源
        workbook.close();
        inputStream.close();

    }

运行结果如下:

二、使用依赖向excel中写入相关数据

java 复制代码
 public static void main(String[] args) throws Exception {
//        createDemoXLS();
        createDemoXLSX();
    }

    public static void createDemoXLS() throws Exception {
        //首先创建工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("03xls版本");
        //创建第一行
        Row row = sheet.createRow(0);
        //创建单元格
        Cell cell0 = row.createCell(0);
        cell0.setCellValue("序号");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("商品名称");

        //创建第二行
        Row row1 = sheet.createRow(1);
        Cell cell10 = row1.createCell(0);
        cell10.setCellValue("0");
        Cell cell11 = row1.createCell(1);
        cell11.setCellValue("电脑");

        //输出流
        FileOutputStream fileOutputStream = new FileOutputStream("./测试.xls");
        workbook.write(fileOutputStream);

        //关闭输出流
        workbook.close();
        fileOutputStream.close();

    }

    public static void createDemoXLSX() throws Exception {
        //首先创建工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("07xlsx版本");
        //创建第一行
        Row row = sheet.createRow(0);
        //创建单元格
        Cell cell0 = row.createCell(0);
        cell0.setCellValue("序号");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue("商品名称");

        //创建第二行
        Row row1 = sheet.createRow(1);
        Cell cell10 = row1.createCell(0);
        cell10.setCellValue("0");
        Cell cell11 = row1.createCell(1);
        cell11.setCellValue("平板");

        //输出流
        FileOutputStream fileOutputStream = new FileOutputStream("./测试.xlsx");
        workbook.write(fileOutputStream);

        //关闭输出流
        workbook.close();
        fileOutputStream.close();

    }

三、批量插入

java 复制代码
    public static void main(String[] args) throws Exception {
//        BatchXlsInsert();
//        BatchXlsxInsert();
        BatchXlsxBigInsert();
    }

    //大内存插入数据 .xls 03-
    public static void BatchXlsInsert() throws Exception{
        //记录当前时间
        long start = System.currentTimeMillis();
        //先创建工作部
        Workbook workbook = new HSSFWorkbook();
        //工作表
        Sheet sheet = workbook.createSheet("03测试");
        //调用复合组件
        rowOriginSheet(sheet);
        //输出
        FileOutputStream outputStream = new FileOutputStream("./03BatchInsert.xls");
        workbook.write(outputStream);
        long end = System.currentTimeMillis();
        System.out.println("创建03.xls耗时 ====> "+(end-start)/1000 + "s");
    }

    //大内存插入问题 07+ .xlsx
    public static void BatchXlsxInsert() throws Exception{
        //记录当前时间
        long start = System.currentTimeMillis();
        //先创建工作部
        Workbook workbook = new XSSFWorkbook();
        //工作表
        Sheet sheet = workbook.createSheet("07测试");
        //调用复合组件
        rowOriginSheet(sheet);
        //输出
        FileOutputStream outputStream = new FileOutputStream("./07BatchInsert.xlsx");
        workbook.write(outputStream);
        long end = System.currentTimeMillis();
        System.out.println("创建07.xls耗时 ====>"+(end-start)/1000 + "s");
    }

    //大内存插入问题 07+ .xlsx 升级版
    public static void BatchXlsxBigInsert() throws Exception{
        //记录当前时间
        long start = System.currentTimeMillis();
        //先创建工作部
        Workbook workbook = new SXSSFWorkbook();
        //工作表
        Sheet sheet = workbook.createSheet("07测试");
        //调用复合组件
        rowOriginSheet(sheet);
        //输出
        FileOutputStream outputStream = new FileOutputStream("./07BatchBigInsert.xlsx");
        workbook.write(outputStream);
        long end = System.currentTimeMillis();
        System.out.println("创建07.xls耗时 ====>"+(end-start)/1000 + "s");
    }
    public static void rowOriginSheet(Sheet sheet){
        //创建行
//        for (int i = 0; i < 65536; i++) {
        for (int i = 0; i < 1045536; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 20; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j+1);
            }
        }
    }

其中,HSSFWorkbook只能插入少于65536行的数据,而XSSFWorkbook可以插入少于1045536条数据,但是其插入的速度较慢,因此使用SXSSF进行了升级,插入的速度更快。

相关推荐
码界奇点3 小时前
Apache IoTDB 架构特性与 PrometheusGrafana 监控体系部署实践
架构·apache·grafana·prometheus·iotdb
一个天蝎座 白勺 程序猿16 小时前
Apache IoTDB(7):设备模板管理——工业物联网元数据标准化的破局之道
数据库·apache·时序数据库·iotdb
惜.己16 小时前
apache启动失败Failed to start The Apache HTTP Server.
apache
一个天蝎座 白勺 程序猿1 天前
Apache IoTDB(8):时间序列管理——从创建到分析的实战指南
数据库·apache·时序数据库·iotdb
SelectDB1 天前
更高效的数据处理解决方案:基于 MinIO 部署 Apache Doris 存算分离版本实践
数据库·数据分析·apache
Wang's Blog2 天前
Linux小课堂: Apache虚拟主机配置之基于IP与域名的服务器部署指南
linux·服务器·apache
Wang's Blog2 天前
Linux小课堂: Apache服务在CentOS上的安装与基础配置指南
linux·centos·apache
DolphinScheduler社区2 天前
小白指南:Apache DolphinScheduler 补数据功能实操演示
java·大数据·开源·apache·海豚调度·大数据工作流调度
北邮-吴怀玉2 天前
3.1.1.1 大数据方法论与实践指南-开源工具说明-Apache NiFi
大数据·开源·apache
Apache Flink2 天前
理想汽车基于 Hologres + Flink 构建万亿级车联网信号实时分析平台
阿里云·flink·apache