使用java解析excel文件的方法

文章目录


前言

如何通过java代码解析excel文件


使用步骤

1.引入库

代码如下(示例):

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
   </dependency>
   <dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>3.17</version>    
   </dependency>

2.读入数据

代码如下(示例):

       List<Map<Integer, Object>> list = new ArrayList<>();


    //加载数据源文件
        //把目标文件转化为文件流
        File excelFile=new File("#文件路径");

        //判断是否是文件并且是否存在
        if(excelFile.isFile()&&excelFile.exists()){

            //创建输出流对象
            FileInputStream fls=new FileInputStream(excelFile);
            //将输出的流对象引入到解析excel文件的对象中
            Workbook wb=null;
            /*判断文件是xlsx结尾还是xls结尾 声明XSSF或HSSF对象*/
            String[] split  = excelFile.getName().split("\\.");
            if(split[1].equals("xlsx")){
                wb= new XSSFWorkbook(fls);
            }else if(split[1].equals("xls")){
                wb= new HSSFWorkbook(fls);
            }


        //获取工作表页数据
            //读取第1页的数据
            Sheet sheet=wb.getSheetAt(0);
        //获取工作表页中行数据
            //读取的行,如果sheet中一行数据没有返回-1,只有第一行有数据则返回0,最后有数据的行是第n行则返回n-1
            int firstRowIndex=sheet.getFirstRowNum();
            //读取的总的行数
            int lastRowIndex=sheet.getLastRowNum();

            //可以通过for循环遍历每一行的数据再在循环内对列的数据进行处理
            for(int i=firstRowIndex;i<=lastRowIndex;i++){
                Map<Integer,Object> map=new HashMap<>();
                //获取指定行的数据
                Row  row=sheet.getRow(i);
        //获取工作表页中指定行的指定列数据
                //当前行的第一个列的数据的下标
                int firstCellIndex=row.getFirstCellNum();
                //row中一列数据都没有则返回-1,只有第一列有数据则返回-1,最后有数据的列是第n列则返回n
                int lastCeeIndex=row.getLastCellNum();
                //获取当前下标(列)的单元格数据
                for(int j=firstCellIndex;j<lastCeeIndex;j++){
                    Cell cell=row.getCell(j);
                    map.put(j,cell);
                }
                list.add(map);

            }
        }else{
            System.out.println("文件类型错误");
        }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中,可以使用Apache POI库来解析Excel文件。Apache POI是一个开源的Java库,提供了读取、写入和操作Microsoft Office格式文件(如Excel、Word和PowerPoint)的功能。 要解析Excel文件,首先需要导入Apache POI库的相关依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 接下来,可以使用以下代码示例来解析Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ExcelParser { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("path/to/excel/file.xlsx"); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } } System.out.println(); } workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码使用FileInputStream来读取Excel文件,然后创建XSSFWorkbook对象表示整个Excel文件。通过getSheetAt方法获取第一个Sheet,然后使用两个嵌套的循环遍历每一行和每一个单元格。根据单元格的类型,可以使用getCellType方法获取单元格的值。 请注意,上述代码示例假设Excel文件的第一个Sheet是要解析的目标。如果需要解析其他Sheet,可以使用getSheet方法指定Sheet的名称或索引。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值