目录
- pom.xml
- ExcelWriteTest .java
pom.xml
<?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.smy</groupId>
<artifactId>smy-poi</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
ExcelWriteTest .java
package com.smy;
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.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWriteTest {
String PATH = "D:\\practise\\PoiExcelTestSuite\\smy-poi\\";
@Test
public void testWrite03() throws IOException {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheetTest1");
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("smy111");
Cell cell12 = row1.createCell(1);
cell12.setCellValue(true);
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表03.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("观众统计表03 生成完毕!");
}
@Test
public void testWrite07() throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheetTest1");
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("第一行第一个单元格值为smy111");
Cell cell12 = row1.createCell(1);
cell12.setCellValue("第一行第二个单元格值为smy222");
Cell cell15 = row1.createCell(4);
cell15.setCellValue("测试布尔类型");
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("统计时间");
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
Cell cell25 = row2.createCell(4);
cell25.setCellType(XSSFCell.CELL_TYPE_BOOLEAN);
cell25.setCellValue(true);
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表07.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("观众统计表07 生成完毕!");
}
@Test
public void testWrite03BigData() throws IOException {
long beginTime = System.currentTimeMillis();
System.out.println("开始时间:" + beginTime);
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("smy测试表1");
for (int rowNum = 0; rowNum < 65536; rowNum++){
Row row = sheet.createRow(rowNum);
for(int cellNum = 0; cellNum < 10; cellNum++){
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite03BigData.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("结束时间:" + endTime);
System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);
}
@Test
public void testWrite07BigData() throws IOException {
long beginTime = System.currentTimeMillis();
System.out.println("开始时间:" + beginTime);
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("smy测试表1");
for (int rowNum = 0; rowNum < 100000; rowNum++){
Row row = sheet.createRow(rowNum);
for(int cellNum = 0; cellNum < 10; cellNum++){
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite07BigData.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("结束时间:" + endTime);
System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);
}
@Test
public void testWrite07BigDataS() throws IOException {
long beginTime = System.currentTimeMillis();
System.out.println("开始时间:" + beginTime);
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet("smy测试表1");
for (int rowNum = 0; rowNum < 100000; rowNum++){
Row row = sheet.createRow(rowNum);
for(int cellNum = 0; cellNum < 10; cellNum++){
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite07BigDataS.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
((SXSSFWorkbook) workbook).dispose();
long endTime = System.currentTimeMillis();
System.out.println("结束时间:" + endTime);
System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);
}
}