poi读写Excel记录

读写Excel 记录

第一步: 

  pom依赖

 <!--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>
<!--xls (日期格式化)-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>

如果开发使用的poi 直接操作Excel 以上两个jar 包依赖不可少
注:03 版本的Excel 特点--> 内容有限制 最大行数为65535 。读写速度快。因为是写入到内存然后在 一次性的 进行操作,所以速度快
07 版本的Excel 特点--> 网上说的理论上行数无限制,但是用java读写的话是 一行一行的读取所以数据就比较慢。

第二步(先上代码,之后会截图在对代码进行解释): 
  生成Excel 也就是写入Excel 代码如下

类中导入的所有的包
import cn.hutool.core.date.DateTime;
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 java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 测试 03版本的 excel 写入
*/
public static void testWrity03() throws IOException, InterruptedException {
for(int p = 0 ; p < 1 ;p++){
long startTime=System.currentTimeMillis(); //获取开始时间
//1.创建工作簿 Workbook 实现类 HSSFWorkbook 操作03 版本 SXSSFWorkbook/XSSFWorkbook 处理07版本
Workbook workbook = new HSSFWorkbook();
//2.创建工作表 sheet 页
Sheet sheet = workbook.createSheet("测试sheet页");//为sheet 添加名称
//3.创建行
Row row = sheet.createRow(0); //0代表 第一行
//4.创建单元格
//创建第一行 序号,用户编号,登录密码,真实姓名,是否删除,手机号码,用户邮箱,创建时间,更新时间
List<String> listTemp = new ArrayList<>();
listTemp.add("序号");
listTemp.add("用户编号");
listTemp.add("登录密码");
listTemp.add("真实姓名");
listTemp.add("是否删除");
listTemp.add("手机号码");
listTemp.add("用户邮箱");
listTemp.add("创建时间");
listTemp.add("更新时间");
//添加表头
for(int i = 0 ; i < 8 ; i++ ){
for(int j = 0 ; j < listTemp.size();j++){
Cell cell = row.createCell(j);
cell.setCellValue(listTemp.get(j));
}
}
//添加数据 从第二行开始 循环生成 1W条
for(int i = 1 ; i < 65530 ;i++){
//创建
Row rowxh = sheet.createRow(i); // 第i行
for(int j = 0 ; j <listTemp.size();j++ ){
Cell cell = rowxh.createCell(j);
if(j == 0){
cell.setCellValue(i);
}else if(j == 1){
cell.setCellValue("BH-00"+i);
}else if(j == 2){
cell.setCellValue("111111");
}else if(j == 3){
cell.setCellValue("姓名->"+i);
}else if(j == 4){
cell.setCellValue("否");
}else if(j == 5){
cell.setCellValue("18888888888");
}else if(j ==6){
cell.setCellValue("yx@163.com");
}else if(j == 7){
cell.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}else{
cell.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}
}

}
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
//生成Excel 表
FileOutputStream fileOutputStream = new FileOutputStream("F:\\IdeaWorkSpace1\\****\\******\\CE"+p+".xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
}

public static void testWrity07() throws IOException {
for(int p = 0 ; p < 6 ;p++){
long startTime=System.currentTimeMillis(); //获取开始时间
//1.创建工作簿 Workbook 实现类 HSSFWorkbook 操作03 版本 SXSSFWorkbook/XSSFWorkbook 处理07版本
Workbook workbook = new SXSSFWorkbook(); //这个地方 用SXSSFWorkbook 好处是大文件 写入比用XSSFWorkbook 速度快很多


//2.创建工作表 sheet 页
Sheet sheet = workbook.createSheet("测试07版 sheet页");//为sheet 添加名称
//3.创建行
Row row = sheet.createRow(0); //0代表 第一行
//4.创建单元格
//创建第一行 序号,用户编号,登录密码,真实姓名,是否删除,手机号码,用户邮箱,创建时间,更新时间
List<String> listTemp = new ArrayList<>();
listTemp.add("序号");
listTemp.add("用户编号");
listTemp.add("登录密码");
listTemp.add("真实姓名");
listTemp.add("是否删除");
listTemp.add("手机号码");
listTemp.add("用户邮箱");
listTemp.add("创建时间");
listTemp.add("更新时间");
//添加表头
for(int i = 0 ; i < 8 ; i++ ){
for(int j = 0 ; j < listTemp.size();j++){
Cell cell = row.createCell(j);
cell.setCellValue(listTemp.get(j));
}
}
//添加数据 从第二行开始 循环生成 1W条
for(int i = 1 ; i < 65530 ;i++){
//创建
Row rowxh = sheet.createRow(i); // 第i行
for(int j = 0 ; j <listTemp.size();j++ ){
Cell cell = rowxh.createCell(j);
if(j == 0){
cell.setCellValue(i);
}else if(j == 1){
cell.setCellValue("BH-00"+i);
}else if(j == 2){
cell.setCellValue("111111");
}else if(j == 3){
cell.setCellValue("姓名->"+i);
}else if(j == 4){
cell.setCellValue("否");
}else if(j == 5){
cell.setCellValue("18888888888");
}else if(j ==6){
cell.setCellValue("yx@163.com");
}else if(j == 7){
cell.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}else{
cell.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
}
}

}
long endTime=System.currentTimeMillis(); //获取结束时间
((SXSSFWorkbook)workbook).dispose();//清楚临时文件
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
//生成Excel 表
FileOutputStream fileOutputStream = new FileOutputStream("F:\\IdeaWorkSpace1\\****\\****\\CE07"+p+".xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
}

 

 

 

第三步:测试写入读取Excel 
  先上代码,之后截图解释
  07和03版本的基本上一致,只做了07版本的代码记录
  注:有两个不错的小点 POI
      1.sheet对象后 通过 sheetAt.getPhysicalNumberOfRows();获取当前 的sheet 一共有多少行
      2.获取row 对象后 通过 row.getPhysicalNumberOfCells(); 可以获取当 当然行 一共有多少列
导入的所有包
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
 
/**
* 读取07版本Excel
* @throws IOException
* @throws InterruptedException
*/
public static void testWrity07() throws IOException, InterruptedException {
//1.获取文件流
FileInputStream fileInputStream = new FileInputStream("F:\\IdeaWorkSpace1\\cloud\\cloud-management\\CE070.xlsx");
//创建工作簿
Workbook workbook = new XSSFWorkbook(fileInputStream);
//获取sheet 页 总页数
int numberOfSheets = workbook.getNumberOfSheets();
for(int i = 0 ; i < numberOfSheets;i++){ //遍历 sheet 页
//遍历所有的sheet页
Sheet sheetAt = workbook.getSheetAt(i); //获取第I个sheet页对象
//获取每个sheet 页面的标题,也就是第一行
Row row = sheetAt.getRow(0);
int physicalNumberOfRows = sheetAt.getPhysicalNumberOfRows();//获取当前页一共有多少行
if(row !=null){
//获取所有有数据的列 数据量(重点)
int cellCount = row.getPhysicalNumberOfCells();
for (int cellNum = 0 ;cellNum <cellCount ;cellNum++ ){
Cell cell = row.getCell(cellNum);
int cellType = cell.getCellType(); //获取数据类型
System.out.print(cell.getStringCellValue()+" "+cellType+"|| ");
}
}
System.out.println("-------获取标题行结束-----------------");
for(int j = 1 ; j < physicalNumberOfRows;j++){
if(j == 10){
break;
}
Row rowAll = sheetAt.getRow(j); //获取行,从第二列开始
if(rowAll != null){
int cells = rowAll.getPhysicalNumberOfCells(); //获取当前行一共有多少列
for(int k = 0; k < cells;k++){
Cell cell = rowAll.getCell(k);
int cellType = cell.getCellType();//获取当前列 的类型
String cellValue = "";
switch (cellType){
case HSSFCell.CELL_TYPE_NUMERIC: //0
System.out.println("数据类型为 数字 或者 日期");
//再次判断是数字还是日期
if(HSSFDateUtil.isCellDateFormatted(cell)){
System.out.println("日期类型");
Date dateCellValue = cell.getDateCellValue();
cellValue = new DateTime(dateCellValue).toString(); //日期
}else{
//不是日期 表示 当前为数字,需要转为Strin 防止数字过长
int cellTypeString = HSSFCell.CELL_TYPE_STRING;
cell.setCellType(cellTypeString);
cellValue = cell.toString();
}
break;
case HSSFCell.CELL_TYPE_STRING: //1
System.out.println("数据类型 String ");
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: //2
System.out.println("数据类型 单元类型(2)");
break;
case HSSFCell.CELL_TYPE_BLANK: //3
cellValue = cell.getStringCellValue();
System.out.println("数据类型 空白");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //4
boolean booleanCellValue = cell.getBooleanCellValue();
cellValue = String.valueOf(booleanCellValue);
System.out.println("数据类型 布尔");
break;
case HSSFCell.CELL_TYPE_ERROR: //5
byte errorCellValue = cell.getErrorCellValue();
cellValue = String.valueOf(errorCellValue);
System.out.println("数据类型 错误");
break;
}
System.out.println("结果输出:"+cellValue);
}
}
}
}
//关闭流
fileInputStream.close();
}
此处重点是在switch 循环的单元格数据类型的判断 









posted @ 2021-01-18 10:55  晏先生  阅读(80)  评论(0编辑  收藏  举报