Java使用poi进行excel的读取操作

516 阅读6分钟

我正在参加「掘金·启航计划」

poi excel读

1 03版poi excel读

package com.wyh.Test;



import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

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.junit.Test;



import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FilterInputStream;



 /**

 *  @program:  JavaExecl

 *  @description:  03版poi读

 *  @author:  魏一鹤

 *  @createDate:  2021-12-15 23:20

 **/



public class BigDateExcelRead03 {

    //全局路径,供我们操作使用方便

  static String path = "D:\Tools\JavaWorkSpace\JavaExecl\" ;



    @Test

    public void BigDateExcelWrite03() throws Exception {

        //需要读取,肯定需要流 所以这边我们创建流

 FileInputStream filterInputStream = new FileInputStream(path+ "用户测试03.xls" );

        // 1 创建工作簿 使用excel可以完成的操作这边通过poi都可以完成

 //把我们的流放在工作簿用 用于读取excel数据

 Workbook workbook = new HSSFWorkbook(filterInputStream);

        //2 获取工作表  可以通过名称和下标获取工作表 这边使用下标获取 0就是第一个工作表

 Sheet sheet = workbook.getSheetAt(0);

        //行列组成单元格

 //3 获取行  下标为0就是第一个

 Row row = sheet.getRow(0);

        //4 获取列  下标为0就是第一个

 Cell cell1 = row.getCell(0);

        Cell cell2 = row.getCell(1);

        //获取单元格内容

 // cell  有很多方法,获取不同的数据需要用到不同的方法getStringCellValue获取字符串

 //读取值的时候 一定要只要数据类型 根据不同的数据类型使用不同的方法

 String stringCellValue = cell1.getStringCellValue();

        double numericCellValue = cell2.getNumericCellValue();

        System.out.println(stringCellValue);

        System.out.println(numericCellValue);

        //关闭流

 filterInputStream.close();



    }

}

2 07版poi excel读

只需要把引用对象(XSSF)和后缀(xlsx)改变即可

package com.wyh.Test;



import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

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.XSSFWorkbook;

import org.junit.Test;



import java.io.FileInputStream;



 /**

 *  @program:  JavaExecl

 *  @description:  03版poi读

 *  @author:  魏一鹤

 *  @createDate:  2021-12-15 23:20

 **/



public class BigDateExcelRead07 {

    //全局路径,供我们操作使用方便

  static String path = "D:\Tools\JavaWorkSpace\JavaExecl\" ;



    @Test

    public void BigDateExcelWrite07() throws Exception {

        //需要读取,肯定需要流 所以这边我们创建流

 FileInputStream filterInputStream = new FileInputStream(path+ "用户测试07.xlsx" );

        // 1 创建工作簿 使用excel可以完成的操作这边通过poi都可以完成

 //把我们的流放在工作簿用 用于读取excel数据

 Workbook workbook = new XSSFWorkbook(filterInputStream);

        //2 获取工作表  可以通过名称和下标获取工作表 这边使用下标获取 0就是第一个工作表

 Sheet sheet = workbook.getSheetAt(0);

        //行列组成单元格

 //3 获取行  下标为0就是第一个

 Row row = sheet.getRow(0);

        //4 获取列  下标为0就是第一个

 Cell cell1 = row.getCell(0);

        Cell cell2 = row.getCell(1);

        //获取单元格内容

 // cell  有很多方法,获取不同的数据需要用到不同的方法getStringCellValue获取字符串

 //读取值的时候 一定要只要数据类型 根据不同的数据类型使用不同的方法

 String stringCellValue1 = cell1.getStringCellValue();

         String stringCellValue2 = cell2.getStringCellValue();



        System.out.println(stringCellValue1);

        System.out.println(stringCellValue2);

        //关闭流

 filterInputStream.close();



    }

}

读取不同的数据类型

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

获取全部表头信息

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();



    }

}

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

获取表中内容

row.getPhysicalNumberOfCells()获取全部的列

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

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();



    }

}

计算公式

首先需要准备一个excel

需要获取计算公式eval FormulaEvaluator

如果我们用没有计算公式的行,那么不会有用