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

おすすめ

転載: blog.csdn.net/moerduo0/article/details/113833765