poi读写操作

1.poi介绍

pache POI [1]  是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)

2.导入依赖

03版本xml结尾的只能有65535行,07版以xlsx结尾,没有限制

  <!--xls(03)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <!--xlsx(07)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <!--日期格式化工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

 3.写入Excel

03版本用的是HSSF,07版本用的是XSSF

3-1.03测试代码 xls

package com.example.list.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;



//写测试
public class ExcelWriteTest {
    String PATH = "E:\\java\\list\\";
    @Test
    public void test01() throws Exception {
        //1.创建工作簿
        Workbook workbook = new HSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");
        //第一行
        //3.创建一个行
        Row row1 = sheet.createRow(0);
        //4.创建一个单元格//(1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        //4.创建一个单元格//(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(6666);

        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完毕");

    }
}

3-2.07版本xlsx

package com.example.list.controller;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;



//写测试
public class ExcelWriteTest {
    String PATH = "E:\\java\\list\\";
    @Test
    public void test01() throws Exception {
        //1.创建工作簿
        Workbook workbook = new XSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");
        //第一行
        //3.创建一个行
        Row row1 = sheet.createRow(0);
        //4.创建一个单元格//(1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        //4.创建一个单元格//(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(6666);

        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完毕");

    }
}

 4.大数据导入

4-1 03版本

03版本耗时2.83秒(每个人电脑性能不一样) 03版本只能处理65536行,过程中写入缓存,不操作磁盘,写入速度快

package com.example.list.controller;



import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import org.junit.Test;
import java.io.FileOutputStream;


//写测试
public class ExcelWriteTest {
    String PATH = "E:\\java\\list\\";

    @Test
    public void testBigData() throws Exception {
        //开始时间
        long begin = System.currentTimeMillis();
        //创建一个薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        //行
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);

            }

        }
        System.out.println("完成");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double)( end-begin)/1000);

    }
}

4-2 07版本

07版本速度慢,写入耗内存,也会内存溢出,我电脑是 4.28秒,处理65536行。

package com.example.list.controller;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;


//写测试
public class ExcelWriteTest {
    String PATH = "E:\\java\\list\\";

    @Test
    public void testBigData() throws Exception {
        //开始时间
        long begin = System.currentTimeMillis();
        //创建一个薄
        Workbook workbook = new XSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        //行
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);

            }

        }
        System.out.println("完成");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - begin) / 1000);

    }
}

对于写入慢的问题,大文件可以写SCSSF,在写入过程中会生成临时文件,内存只有100条,可以自己修改 

package com.example.list.controller;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;


//写测试
public class ExcelWriteTest {
    String PATH = "E:\\java\\list\\";

    @Test
    public void testBigData() throws Exception {
        //开始时间
        long begin = System.currentTimeMillis();
        //创建一个薄
        Workbook workbook = new SXSSFWorkbook();
        //创建一个表
        Sheet sheet = workbook.createSheet();
        //写入数据
        //行
        for (int rowNum = 0; rowNum < 1000000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);

            }

        }
        System.out.println("完成");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //清楚临时文件
        ((SXSSFWorkbook)workbook).dispose();
        long end = System.currentTimeMillis();
        System.out.println((double) (end - begin) / 1000);

    }
}

5.excel读

5-1 03版本

package com.example.list.controller;



import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.Test;

import java.io.FileInputStream;

public class ExcelReadTest {
    public static final String PATH = "E:\\java\\list\\";

    @Test
    public void testRead03() throws Exception {

        //获取文件流
        FileInputStream inputStream = new FileInputStream(PATH+"统计表03.xls");
        //创建一个工作薄,
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        //得到表
        Sheet sheet = workbook.getSheetAt(0);
        //得到行
        Row row = sheet.getRow(0);
        //得到列
        Cell cell = row.getCell(0);

        System.out.println(cell.getStringCellValue());
        inputStream.close();


    }
}

5-2 07版本

package com.example.list.controller;




import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;

public class ExcelReadTest {
    public static final String PATH = "E:\\java\\list\\";

    @Test
    public void testRead07() throws Exception {

        //获取文件流
        FileInputStream inputStream = new FileInputStream(PATH+"统计表07.xlsx");
        //创建一个工作薄,
        Workbook workbook = new XSSFWorkbook(inputStream);
        //得到表
        Sheet sheet = workbook.getSheetAt(0);
        //得到行
        Row row = sheet.getRow(0);
        //得到列
        Cell cell = row.getCell(0);

        System.out.println(cell.getStringCellValue());
        inputStream.close();


    }
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royalreairman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值