Java框架之Java读excel

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>rg</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
<!--        &lt;!&ndash;mysql依赖&ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--            <version>8.0.23</version>-->
<!--            <scope>runtime</scope>-->
<!--        </dependency>-->

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>


    </dependencies>
    
</project>
package com.rg;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelUtil {

    public String excelPath() {
        String excelPath = "D:\\pro\\rg\\src\\main\\resources\\1主题模型表_PL00_202109291418.xlsx";
        return excelPath;
    }


    public File excel(String excelPath) {
        File excel = new File(excelPath);
        if (excel.isFile() && excel.exists()) {
            return excel;
        }
        throw new RuntimeException("文件不存在");
    }


    public String excelType(String excelPath) {
        String[] strs = excelPath.split("\\.");
        if (strs == null || strs.length < 1) {
            throw new RuntimeException("文件类型异常");
        }
        int len = strs.length;
        String excelType = strs[len - 1];
        if ("xls".equals(excelType)) {
            return "xls";
        } else if ("xlsx".equals(excelType)) {
            return "xlsx";
        } else {
            throw new RuntimeException("文件类型异常");
        }
    }

    public Workbook getWorkbook(String excelType) throws IOException, InvalidFormatException {
        Workbook wb = null;
        if ("xls".equals(excelType)) {
            FileInputStream fis = new FileInputStream(excel(excelType));   //文件流对象
            wb = new HSSFWorkbook(fis);
        } else if ("xlsx".equals(excelType)) {
            wb = new XSSFWorkbook(excel(excelPath()));
        }
        return wb;
    }

    public void readSheet(Workbook workbook, int sheetPage) {
        Sheet sheet = workbook.getSheetAt(sheetPage);
        int firstRowIndex = sheet.getFirstRowNum() + 2;   //第一行是列名,所以不读
        int lastRowIndex = sheet.getLastRowNum();
        for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
            Row row = sheet.getRow(rIndex);
            if (row != null) {
                int firstCellIndex = row.getFirstCellNum();
                int lastCellIndex = row.getLastCellNum();
                for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                    Cell cell = row.getCell(cIndex);
                    if (cell != null) {
                        System.out.printf("%s    ",cell.toString());
                    }
                }
                System.out.println();
            }
        }
    }

}

package com.rg;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        ExcelUtil excelUtil=new ExcelUtil();
        excelUtil.readSheet(excelUtil.getWorkbook(excelUtil.excelType(excelUtil.excelPath())),0);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值