Apache POI入门学习

说明介绍与使用场景:

说明

Apache POI 可用于处理Miscrosoft Office各种文件格式。可以使用 POI 在 Java 程序中对Miscrosoft Office各种文件进行读写操作。

使用场景:

一般情况下,POI 都是用于操作 Excel 文件,生成流水数据,统计报表等

使用:

一:导入maven坐标:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

二:POI写入文件基本使用:

基本使用语句代码中有介绍

package com.sky.test;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;

public class POITesk {

    /**
     * 创建Excel文件,并且写入
     */
    public static void write() throws Exception{
        //创建
        XSSFWorkbook excel=new XSSFWorkbook();
        //创建Sheet页
        XSSFSheet sheet=excel.createSheet("info");
        //创建行对象,编号是从0开始
        XSSFRow row=sheet.createRow(1);

        //创建单元格,并写入内容
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("城市");

        row= sheet.createRow(2);
        row.createCell(1).setCellValue("张三");
        row.createCell(2).setCellValue("北京");


        row= sheet.createRow(3);
        row.createCell(1).setCellValue("李四");
        row.createCell(2).setCellValue("南京");

        //通过输出流将内存中Excel文件写入到磁盘中
        FileOutputStream out=new FileOutputStream(new File("D:\\File"));
        excel.write(out);

        out.close();
        excel.close();


    }

    public static void main(String[] args)throws Exception {
        write();
    }
}

创建结果:
POI创建文件是在内存中创建,还需要通过输入流将内存中创建的文件保存到磁盘中
image.png

POI读取文件基本使用:

要先指定要读取的文件路径创建输出流读取到文件

/**
 * 通过POI读取文件
 * @throws IOException
 */
public static void read() throws IOException {
    InputStream in = new FileInputStream(new File("D:\\File"));

    //读取文件
    XSSFWorkbook excel = new XSSFWorkbook(in);

    //读取文件的第一个Sheet页
    XSSFSheet sheet= excel.getSheetAt(0);

    //获取最后一行的行号
    int lastRowNum=sheet.getLastRowNum();

    for (int i = 1; i < lastRowNum; i++) {
        //获得某一行
        XSSFRow row=sheet.getRow(i);
        //获取到单元格对象
        String cellValue1=row.getCell(1).getStringCellValue();
        String cellValue2=row.getCell(2).getStringCellValue();
        System.out.println(cellValue1+"  "+cellValue2);
    }
    in.close();
    excel.close();

}

实际使用:

苍穹外卖项目中用于统计导出近期的运营数据
添加好模板文件
image.png

代码开发

/**
 * 导出运营数据报表
 * @param response
 */
@Override
public void exportBusinessData(HttpServletResponse response) {
    //查询数据库,获取最近三十天的运营数据
    LocalDate dateBegin=LocalDate.now().minusDays(30);
    LocalDate dateEnd=LocalDate.now().minusDays(1);

    //查询概览数据
    BusinessDataVO businessDataVO=workspaceService
            .getBusinessData(LocalDateTime.of(dateBegin,LocalTime.MIN),LocalDateTime.of(dateEnd,LocalTime.MAX));

    //通过POI将数据写入到文件中
    InputStream in=this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");

    try {
        //基于模板创建一个新的excel文件
        XSSFWorkbook excel=new XSSFWorkbook(in);

        XSSFSheet sheet=excel.getSheet("Sheet1");
        sheet.getRow(1).getCell(1).setCellValue("时间:"+dateBegin+"至"+dateEnd);

        //填充数据
        //获取第四行
        XSSFRow row=sheet.getRow(3);
        row.getCell(2).setCellValue(businessDataVO.getTurnover());
        row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
        row.getCell(6).setCellValue(businessDataVO.getNewUsers());

        //第五行
        row=sheet.getRow(4);
        row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
        row.getCell(4).setCellValue(businessDataVO.getUnitPrice());

        for (int i = 0; i < 30; i++) {
            LocalDate date = dateBegin.plusDays(i);
            //准备明细数据
            BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date,LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
            row = sheet.getRow(7 + i);
            row.getCell(1).setCellValue(date.toString());
            row.getCell(2).setCellValue(businessData.getTurnover());
            row.getCell(3).setCellValue(businessData.getValidOrderCount());
            row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
            row.getCell(5).setCellValue(businessData.getUnitPrice());
            row.getCell(6).setCellValue(businessData.getNewUsers());
        }

        //通过输出流将excel文件下载到客户端浏览器
        ServletOutputStream out=response.getOutputStream();
        excel.write(out);


    } catch (IOException e) {
        throw new RuntimeException(e);
    }



}

导出效果:

image.png
image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值