Excel version introduction and writing refer to the previous article:
https://blog.csdn.net/moerduo0/article/details/113833765
Create a maven project and import poi dependencies
<?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>
Big Data 03 (.xls) version 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.junit.Test;
import java.io.FileOutputStream;
public class Text {
//文件生成路径
String path="D:\\idea_gzkj\\poi\\";
//03版本的大数据写入
@Test
public void text03() throws Exception{
//开始时间
long begin=System.currentTimeMillis();
//创建一个簿
Workbook workbook=new HSSFWorkbook();
//创建表
Sheet sheet = workbook.createSheet();
//写入数据 03版本的excel最多只有65536行
for (int i = 0; i < 65536; i++) {
//创建行
Row row = sheet.createRow(i);
for (int y = 0; y < 10; y++) {
//每行写0-10 10列数据
Cell cell = row.createCell(y);
cell.setCellValue(y);
}
}
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);
}
}
Effect: Very fast in 2 seconds
Big Data 07 (.xlsx) version 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.junit.Test;
import java.io.FileOutputStream;
public class Text {
//文件生成路径
String path="D:\\idea_gzkj\\poi\\";
//07版本的大数据写入
@Test
public void text07() throws Exception{
//开始时间
long begin=System.currentTimeMillis();
//创建一个簿
Workbook workbook=new XSSFWorkbook();
//创建表
Sheet sheet = workbook.createSheet();
//写入数据 03版本的excel最多只有65536行
for (int i = 0; i < 65536; i++) {
//创建行
Row row = sheet.createRow(i);
for (int y = 0; y < 10; y++) {
//每行写0-10 10列数据
Cell cell = row.createCell(y);
cell.setCellValue(y);
}
}
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);
}
}
Effect: It is also much slower to write 65536 lines 07 than 03
Optimization scheme: use cache
Use the XSSFWorkbook interface.
Advantages: fast writing speed and less memory usage.
Note: temporary files will be generated during the process and need to be cleaned
Optimized code
Write 100,000 rows of data here
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 Text {
//文件生成路径
String path="D:\\idea_gzkj\\poi\\";
//07版本的大数据写入加速版
@Test
public void text07() throws Exception{
//开始时间
long begin=System.currentTimeMillis();
//创建一个簿
Workbook workbook=new SXSSFWorkbook();
//创建表
Sheet sheet = workbook.createSheet();
//写入数据 03版本的excel最多只有65536行
for (int i = 0; i < 1000; i++) {
//创建行
Row row = sheet.createRow(i);
for (int y = 0; y < 10; y++) {
//每行写0-10 10列数据
Cell cell = row.createCell(y);
cell.setCellValue(y);
}
}
System.out.println("写入完成");
//创建文件
FileOutputStream fileOutputStream=new FileOutputStream(path+"07大数据S.xlsx");
//写
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
//删除临时文件
((SXSSFWorkbook) workbook).dispose();
//结束时间
long end=System.currentTimeMillis();
System.out.println((double) (end-begin)/1000);
}
}
Effect:
Writing 100,000 rows of data is much faster than the previous 65536 rows