java 读取excel中列信息

java 读取excel中列信息

导入依赖

        <!--excel下载-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

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

java代码实现

package com.cudatec.overseas.cfg.util;

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 java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: 
 * @description:
 * @author: 
 * @create: 
 **/
public class ImportExcelUtil {

    public static List<String> getUsers(String filePath) {
        //excel文件
        File excelFile = null;
        // 输入流对象
        InputStream inputStream = null;
        // 单元格,最终按字符串处理
        String cellStr = null;
        // 返回封装数据的List
        List<String> users = new ArrayList<>();
        try {
            excelFile = new File(filePath);
            // 获取文件输入流
            inputStream = new FileInputStream(excelFile);
            // 创建Excel文件对象
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
            // 取出第一个工作表,索引是0
            HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
            // 开始循环遍历行,表头不处理,从1开始
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                // 获取行对象
                HSSFRow row = sheet.getRow(i);
                // 如果为空,不处理
                if (row == null) {
                    continue;
                }
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    // 获取单元格对象
                    HSSFCell cell = row.getCell(j);
                    // 单元格为空设置cellStr为空串
                    if (cell == null) {
                        cellStr = "";
                        // 对布尔值的处理
                    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                        cellStr = String.valueOf(cell.getBooleanCellValue());
                        // 对数字值的处理
                    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                        cellStr = cell.getNumericCellValue() + "";
                    } else {
                        // 其余按照字符串处理
                        cellStr = cell.getStringCellValue();
                    }
                }
                // 数据装入List
                users.add(cellStr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭文件流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        for(String su :users){
            System.out.println(su);
        }
        return users;
    }
}

如果帮助到了你,麻烦关注下,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值