使用poi写入Excel

Excel版本:

03版本结尾为xxx.xls 最高只有65536行
07版本结尾为xxx.xlsx 行数无限制

工作簿----->工作表----->行----->列
在这里插入图片描述

创建一个maven项目并导入poi依赖

导入03版本和07版本的依赖
时间格式化和测试工具

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zkd</groupId>
    <artifactId>poi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
<!--        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>
    </dependencies>
</project>

03版本(.xls)代码:

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 Text {

    //文件生成路径
    String path="D:\\idea_gzkj\\poi\\";
    //03版本
    @Test
    public void text03() throws  Exception{
        //1.创建一个工作簿
        Workbook workbook=new HSSFWorkbook();

        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");

        //列:x 行:y
        //(0,0)
        //3.创建一行 0代表第一行
        Row row1=sheet.createRow(0);

        //4.创建一个单元格
        Cell cell01=row1.createCell(0);
        cell01.setCellValue("姓名");

        //(1,0)第一行第二列
        Cell cell02=row1.createCell(1);
        cell02.setCellValue("时间");

        //(0,1)
        Row row2=sheet.createRow(1);//行

        Cell cell03=row2.createCell(0);//列
        cell03.setCellValue("张三");

        //(1,1)
        Cell cell04=row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell04.setCellValue(time);

        //生成一张表(IO流) 03版本使用xls结尾:
        FileOutputStream fileOutputStream=new FileOutputStream(path+"统计文件.xls");

        //写
        workbook.write(fileOutputStream);

        //关闭
        fileOutputStream.close();

        System.out.println("03版本写入完毕");
    }
}


效果:

在这里插入图片描述

07版本(.xlsx)代码

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 Text {

    //文件生成路径
    String path="D:\\idea_gzkj\\poi\\";
  

    //07版本
    @Test
    public void text07() throws  Exception{
        //1.创建一个工作簿  07XSSFWorkbook
        Workbook workbook=new XSSFWorkbook();

        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");

        //列:x 行:y
        //(0,0)
        //3.创建一行 0代表第一行
        Row row1=sheet.createRow(0);

        //4.创建一个单元格
        Cell cell01=row1.createCell(0);
        cell01.setCellValue("姓名");

        //(1,0)第一行第二列
        Cell cell02=row1.createCell(1);
        cell02.setCellValue("时间");

        //(0,1)
        Row row2=sheet.createRow(1);//行

        Cell cell03=row2.createCell(0);//列
        cell03.setCellValue("张三");

        //(1,1)
        Cell cell04=row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell04.setCellValue(time);

        //生成一张表(IO流) 03版本使用xlsx结尾:
        FileOutputStream fileOutputStream=new FileOutputStream(path+"统计文件07版本.xlsx");

        //写
        workbook.write(fileOutputStream);

        //关闭
        fileOutputStream.close();

        System.out.println("07版本写入完毕");
    }
}

效果:
在这里插入图片描述
poi写入大数据到Excel及调优:
https://blog.csdn.net/moerduo0/article/details/113835059

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值