POI读取不同的数据类型

简介: POI读取不同的数据类型

首先准备一个多种数据类型的excel

image.png

里面有以下数据

image.png

获取全部表头信息

row.getPhysicalNumberOfCells(),获取全部的列并返回行数

获取全部表头数并且打印输出


package com.wyh.Test;
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.junit.Test;
import javax.sql.RowSet;
import java.io.FileInputStream;
/**
 * @program: JavaExecl
 * @description: 读取不同的excel数据类型
 * @author: 魏一鹤
 * @createDate: 2021-12-16 23:42
 **/
public class ReadManyInfo {
//全局路径,供我们操作使用方便
    static String path="D:\\Tools\\JavaWorkSpace\\JavaExecl\\";
    @Test
public void BigDateExcelWrite07() throws Exception {
//需要读取,肯定需要流 所以这边我们创建流
        FileInputStream filterInputStream = new FileInputStream(path+"会员消费商品明细表.xls");
// 1 创建工作簿 使用excel可以完成的操作这边通过poi都可以完成
        //把我们的流放在工作簿用 用于读取excel数据
        Workbook workbook = new HSSFWorkbook(filterInputStream);
//2 获取工作表
        Sheet sheet = workbook.getSheetAt(0);
//3获取行(表头)
        Row rowTitle = sheet.getRow(0); //其实这就是我们的表头 最上面的部分
        //判断行不为空才读
        if(rowTitle!=null){
//如果行不为空才去读列的信息
            //getPhysicalNumberOfCells()获取全部的列并且返回行数
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            System.out.println("cellCount = " + cellCount);
for (int cellNum = 0; cellNum<cellCount; cellNum++) {
//得到每一行的数据
                Cell cell = rowTitle.getCell(cellNum);
//判断每一行是否为空 不为空再做处理
                if(cell!=null){
//获取全部行的数据类型
                    int cellType = cell.getCellType();
//获取行的值
                    String stringCellValue = cell.getStringCellValue();
//进行输出 这里就不换行了 直接一行显示用竖线分割
                    System.out.print(stringCellValue+"|");
                }
            }
//打印完一行换行打印另外一行
            System.out.println();
        }
//关闭流
        filterInputStream.close();
    }
}


image.png

打印结果和我们的excel表头内容个数是完全对的上的

image.png

获取表中内容

row.getPhysicalNumberOfCells()获取全部的列

比较复杂的就是不同的数据类型进行判断,我们可以把这些提取成一个公用的方法


image.png


package com.wyh.Test;
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 org.junit.Test;
import javax.sql.RowSet;
import java.io.FileInputStream;
import java.util.Date;
/**
 * @program: JavaExecl
 * @description: 读取不同的excel数据类型
 * @author: 魏一鹤
 * @createDate: 2021-12-16 23:42
 **/
public class ReadManyInfo {
//全局路径,供我们操作使用方便
    static String path="D:\\Tools\\JavaWorkSpace\\JavaExecl\\";
    @Test
public void BigDateExcelWrite07() throws Exception {
//需要读取,肯定需要流 所以这边我们创建流
        FileInputStream filterInputStream = new FileInputStream(path+"会员消费商品明细表.xls");
        switchDataType(filterInputStream);
    }
public void switchDataType( FileInputStream filterInputStream ) throws Exception {
// 1 创建工作簿 使用excel可以完成的操作这边通过poi都可以完成
        //把我们的流放在工作簿用 用于读取excel数据
        Workbook workbook = new HSSFWorkbook(filterInputStream);
//2 获取工作表
        Sheet sheet = workbook.getSheetAt(0);
//3获取行(表头)
        Row rowTitle = sheet.getRow(0); //其实这就是我们的表头 最上面的部分
        //判断行不为空才读
        if(rowTitle!=null){
//如果行不为空才去读列的信息
            //getPhysicalNumberOfCells()获取全部的列并且返回行数
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            System.out.println("cellCount = " + cellCount);
for (int cellNum = 0; cellNum<cellCount; cellNum++) {
//得到每一行的数据
                Cell cell = rowTitle.getCell(cellNum);
//判断每一行是否为空 不为空再做处理
                if(cell!=null){
//获取全部行的数据类型
                    int cellType = cell.getCellType();
//获取行的值
                    String stringCellValue = cell.getStringCellValue();
//进行输出 这里就不换行了 直接一行显示用竖线分割
                    System.out.print(stringCellValue+"|");
                }
            }
//打印完一行换行打印另外一行
            System.out.println();
        }
//获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
//循环获取数据
        for (int rowNum = 0; rowNum < rowCount; rowNum++) {
            Row row = sheet.getRow(rowNum);
//不为空再做处理
            if(row!=null){
//读取行中的列    getPhysicalNumberOfCells获取全部的列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("["+(rowNum+1)+"-"+(cellNum + 1)+"]");
//获取数据
                    Cell cell = row.getCell(cellNum);
//因为不知道列的数据类型 所以这里我们要匹配数据类型
                    //如果不为空
                    if(cell != null){
//获取类型
                        int cellType = cell.getCellType();
                        String cellValue="";
//判断cell的数据类型
                        switch (cellType) {
case HSSFCell.CELL_TYPE_STRING://字符串
                                System.out.print("【STRING】");
                                cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN://布尔
                                System.out.print("【BOOLEAN】");
                                cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK://空
                                System.out.print("【BLANK】");
break;
case HSSFCell.CELL_TYPE_NUMERIC:
                                System.out.print("【NUMERIC】");
//cellValue = String.valueOf(cell.getNumericCellValue());
                                if (HSSFDateUtil.isCellDateFormatted(cell)) {//日期
                                    System.out.print("【日期】");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                } else {
// 不是日期格式,则防止当数字过长时以科学计数法显示
                                    System.out.print("【转换成字符串】");
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
break;
case Cell.CELL_TYPE_ERROR:
                                System.out.print("【数据类型错误】");
break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }
//关闭流
        filterInputStream.close();
    }
}


package com.wyh.Test;
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 org.junit.Test;
import javax.sql.RowSet;
import java.io.FileInputStream;
import java.util.Date;
/**
 * @program: JavaExecl
 * @description: 读取不同的excel数据类型
 * @author: 魏一鹤
 * @createDate: 2021-12-16 23:42
 **/
public class ReadManyInfo {
//全局路径,供我们操作使用方便
    static String path="D:\\Tools\\JavaWorkSpace\\JavaExecl\\";
    @Test
public void BigDateExcelWrite07() throws Exception {
//需要读取,肯定需要流 所以这边我们创建流
        FileInputStream filterInputStream = new FileInputStream(path+"会员消费商品明细表.xls");
        switchDataType(filterInputStream);
    }
public void switchDataType( FileInputStream filterInputStream ) throws Exception {
// 1 创建工作簿 使用excel可以完成的操作这边通过poi都可以完成
        //把我们的流放在工作簿用 用于读取excel数据
        Workbook workbook = new HSSFWorkbook(filterInputStream);
//2 获取工作表
        Sheet sheet = workbook.getSheetAt(0);
//3获取行(表头)
        Row rowTitle = sheet.getRow(0); //其实这就是我们的表头 最上面的部分
        //判断行不为空才读
        if(rowTitle!=null){
//如果行不为空才去读列的信息
            //getPhysicalNumberOfCells()获取全部的列并且返回行数
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            System.out.println("cellCount = " + cellCount);
for (int cellNum = 0; cellNum<cellCount; cellNum++) {
//得到每一行的数据
                Cell cell = rowTitle.getCell(cellNum);
//判断每一行是否为空 不为空再做处理
                if(cell!=null){
//获取全部行的数据类型
                    int cellType = cell.getCellType();
//获取行的值
                    String stringCellValue = cell.getStringCellValue();
//进行输出 这里就不换行了 直接一行显示用竖线分割
                    System.out.print(stringCellValue+"|");
                }
            }
//打印完一行换行打印另外一行
            System.out.println();
        }
//获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
//循环获取数据
        for (int rowNum = 0; rowNum < rowCount; rowNum++) {
            Row row = sheet.getRow(rowNum);
//不为空再做处理
            if(row!=null){
//读取行中的列    getPhysicalNumberOfCells获取全部的列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("["+(rowNum+1)+"-"+(cellNum + 1)+"]");
//获取数据
                    Cell cell = row.getCell(cellNum);
//因为不知道列的数据类型 所以这里我们要匹配数据类型
                    //如果不为空
                    if(cell != null){
//获取类型
                        int cellType = cell.getCellType();
                        String cellValue="";
//判断cell的数据类型
                        switch (cellType) {
case HSSFCell.CELL_TYPE_STRING://字符串
                                System.out.print("【STRING】");
                                cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN://布尔
                                System.out.print("【BOOLEAN】");
                                cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK://空
                                System.out.print("【BLANK】");
break;
case HSSFCell.CELL_TYPE_NUMERIC:
                                System.out.print("【NUMERIC】");
//cellValue = String.valueOf(cell.getNumericCellValue());
                                if (HSSFDateUtil.isCellDateFormatted(cell)) {//日期
                                    System.out.print("【日期】");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                } else {
// 不是日期格式,则防止当数字过长时以科学计数法显示
                                    System.out.print("【转换成字符串】");
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
break;
case Cell.CELL_TYPE_ERROR:
                                System.out.print("【数据类型错误】");
break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }
//关闭流
        filterInputStream.close();
    }
}
目录
相关文章
|
Java 开发者
Java 中的 toString() 方法详解:为什么它如此重要?
在Java开发中,`toString()`方法至关重要,用于返回对象的字符串表示。默认实现仅输出类名和哈希码,信息有限且不直观。通过重写`toString()`,可展示对象字段值,提升调试效率与代码可读性。借助Lombok的`@Data`注解,能自动生成标准化的`toString()`方法,简化开发流程,尤其适合字段较多的场景。合理运用`toString()`,可显著提高开发效率与代码质量。
1649 0
|
SQL XML Java
MyBatis Mapper中使用limit参数的查询问题
总结而言,MyBatis中使用 `limit`参数的查询可以高度定制并且灵活,基于方法签名和XML映射文件的组合来达成多样化的查询需求。通过参数化查询和动态SQL,MyBatis可以有效地处理各种复杂情境下的数据库操作,并且将SQL语句的维护与业务代码的编写相分离,提升代码的可维护性和可阅读性。
1021 13
|
人工智能 缓存 负载均衡
spring boot-MultipartFile 机制
本文详解了 Spring Boot 中 MultipartFile 的工作机制及大文件上传的解决方案。内容涵盖 MultipartFile 的解析流程、上传配置、Feign 上传大文件的内存问题及基于 RestTemplate 的流式上传实现。同时介绍了服务器端如何直接处理 application/octet-stream 类型的文件流,避免内存溢出问题。适合需要优化文件上传性能的开发者参考。
1738 0
|
Java Spring
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
983 0
|
SQL 关系型数据库 Go
《增强你的PostgreSQL:最佳扩展和插件推荐》
《增强你的PostgreSQL:最佳扩展和插件推荐》
1902 0
|
Java Apache
Apache POI java对excel表格进行操作(读、写) 有代码!!!
文章提供了使用Apache POI库在Java中创建和读取Excel文件的详细代码示例,包括写入数据到Excel和从Excel读取数据的方法。
2452 0
|
easyexcel
EasyExcel动态合并单元格
EasyExcel动态合并单元格
1419 0
|
Java Apache
Java代码使用POI导出的单元格加上边框和背景色
【5月更文挑战第3天】Java代码使用POI导出的单元格加上边框和背景色
2029 0
|
存储 传感器 SQL
influxdb 中得 fields 与 tag 区别总结
influxdb 中得 fields 与 tag 区别总结
1664 1
|
NoSQL atlas API
全面对比Amazon DocumentDB 与 MongoDB
MongoDB系列技术文章精选
2511 2