[Java]通过Poi包读取Excel表格

本文介绍了一种使用Java从Excel文件中读取名片信息的方法,包括解析Excel表格并将其转换为对象列表的过程。通过逐行读取数据并填充对象属性,实现了对名片信息的有效管理和利用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public List<EntBusinessCard> readCardExcel() throws IOException {
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\buyal\\名片总和.xlsx"));

        EntBusinessCard card = null;
        List<EntBusinessCard> list = new ArrayList<EntBusinessCard>();

        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
            XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

            if(hssfSheet == null){
                continue;
            }

            for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
                XSSFRow hssfRow = hssfSheet.getRow(rowNum);

                if(hssfRow != null){
                    card = new EntBusinessCard();
                    card.setProperty(hssfRow.getCell(0) == null ?"":getValue(hssfRow.getCell(0)));
                    card.setRole(hssfRow.getCell(1) == null ?"":getValue(hssfRow.getCell(1)));
                    card.setName(hssfRow.getCell(2) == null ?"":getValue(hssfRow.getCell(2)));
                    card.setDirect(hssfRow.getCell(3) == null ?"":getValue(hssfRow.getCell(3)));
                    card.setPostcode(hssfRow.getCell(4) == null ?"":getValue(hssfRow.getCell(4)));
                    card.setRealaddress(hssfRow.getCell(5) == null ?"":getValue(hssfRow.getCell(5)));
                    card.setEnttype(hssfRow.getCell(6) == null ?"":getValue(hssfRow.getCell(6)));
                    card.setLine(hssfRow.getCell(7) == null ?"":getValue(hssfRow.getCell(7)));
                    card.setMainproduct(hssfRow.getCell(8) == null ?"":getValue(hssfRow.getCell(8)));
                    card.setMainproductpic(hssfRow.getCell(9) == null ?"":getValue(hssfRow.getCell(9)));
                    card.setScale(hssfRow.getCell(10) == null ?"":getValue(hssfRow.getCell(10)));
                    card.setUrl(hssfRow.getCell(11) == null ?"":getValue(hssfRow.getCell(11)));
                    card.setFax(hssfRow.getCell(12) == null ?"":getValue(hssfRow.getCell(12)));
                    card.setWorkshopstatus(hssfRow.getCell(13) == null ?"":getValue(hssfRow.getCell(13)));
                    card.setCapacity(hssfRow.getCell(14) == null ?"":getValue(hssfRow.getCell(14)));
                    card.setContract(hssfRow.getCell(15) == null ?"":getValue(hssfRow.getCell(15)));
                    card.setTele(hssfRow.getCell(17) == null ?"":getValue(hssfRow.getCell(17)));
                    card.setEmail(hssfRow.getCell(18) == null ?"":getValue(hssfRow.getCell(18)));
                    card.setIntroduce(hssfRow.getCell(19) == null ?"":getValue(hssfRow.getCell(19)));

                    list.add(card);
                }
            }

        }
        return list;
    }

getValue()

    public String getValue(XSSFCell hssfCell){
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            // 返回数值类型的值
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            // 返回字符串类型的值
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }

EntBusinessCard 类

public class EntBusinessCard {
    private String property;
    private String role;
    private String name;
    private String direct;
    private String postcode;
    private String realaddress;
    private String enttype;
    private String line;
    private String mainproduct;
    private String mainproductpic;
    private String scale;
    private String url;
    private String fax;
    private String workshopstatus;
    private String capacity;
    private String contract;
    private String tele;
    private String email;
    private String introduce;

getter()。。。
setter()。。。
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值