Use poi to write to Excel

Excel version:

The 03 version ends with xxx.xls, the highest is only 65536 rows, the
07 version ends with xxx.xlsx, the number of rows is unlimited

. Workbook ----->Worksheet -----> Row ----->Column
Insert picture description here

Create a maven project and import poi dependencies

Import the dependency time formatting and testing tools of version 03 and 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 version (.xls) code:

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版本写入完毕");
    }
}


effect:

Insert picture description here

07 version (.xlsx) code

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版本写入完毕");
    }
}

Effect:
Insert picture description here
poi writes big data to Excel and tuning:
https://blog.csdn.net/moerduo0/article/details/113835059

Guess you like

Origin blog.csdn.net/moerduo0/article/details/113833765