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 API Android开发
【POI框架实战】——POI设置Excel单元格格式
 “这个excel中的数据怎么不能做加法啊?”、“标头这一行的字体怎么这么小啊,我都看不清”、“这一列能不能换个颜色,明显一些”、“你把这一列的数据给我留两个小数位。”、“这些数据能不能以货币的类型展示啊,就每个三位一个小逗号那种……”
【POI框架实战】——POI设置Excel单元格格式
|
API
POI读取公式的值
excel中的数据:   package poi; import java.io.FileInputStream; import java.io.IOException; import java.
2404 0
Excel读取并数据List/Map-POI
Excel读取并数据List/Map-POI
|
8月前
|
XML Java API
Apache POI详解及Word文档读取示例
apache poi资料详解,包括内部jar包依赖关系,及与使用文档的对应关系
847 0
|
9月前
|
Java API Apache
Apache POI 读写 Excel 教程
Apache POI 读写 Excel 教程
212 0
【POI框架实战】——POI导出Excel时设置单元格类型为数值类型
  最近做的一个ITFIN的项目中,后台需要用POI实现导出功能,导出的数据中有文本格式,也有货币格式,所以为了方便在将来导出的表格中做计算,存放货币的单元格需要设置为数值类型。
【POI框架实战】——POI导出Excel时设置单元格类型为数值类型
|
XML 数据格式
POI读取word时读取${}分段了
两片内容相同的word,poi分割成XWPFRun集合的时候,发现相同的段落内容有空行。
409 0
POI读取word时读取${}分段了
POI Excel 合并数据相同的行
import java.io.Serializable; /** * POI Excel报表导出,列合并实体 * * @author WQ * */ public class PoiModel implements Serializable{ /** * ...
1703 0
|
Java API
操作Excel,除了使用POI你还会其他的?
操作Excel,除了使用POI你还会其他的?
299 0
操作Excel,除了使用POI你还会其他的?