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版本@Testpublic 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版本@Testpublic void text07() throws Exception{//1.创建一个工作簿 07XSSFWorkbookWorkbook 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