Springboot 之 使用POI读取解析Excel文件

本文章来自【知识林】

在上一篇文章《Springboot 之 POI导出Word文件》中讲述了使用POI对Word的导出操作,这一篇将继续讲述POI的其他使用:对Excel表格的读写操作。

  • 准备Excel源文件

这里简单准备了一个Excel表格,将此文件命名为:web-info.xls,放到resources目录下,内容如下图:

POI读取Excel文件的源文件内容

  • 读取文件标题
//读取单个单元格
@Test
public void testRead() throws Exception {
    //Excel文件
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:web-info.xls")));
    //Excel工作表
    HSSFSheet sheet = wb.getSheetAt(0);

    //表头那一行
    HSSFRow titleRow = sheet.getRow(0);

    //表头那个单元格
    HSSFCell titleCell = titleRow.getCell(0);

    String title = titleCell.getStringCellValue();

    System.out.println("标题是:"+title);
}

执行测试后将得到:标题是:网站信息管理

  • 将数据项读取到List中

准备一个DTO文件方便创建对象和输出内容

public class WebDto {

    //网站名称
    private String name;

    //网址
    private String url;

    //用户名
    private String username;

    //密码
    private String password;

    //日均访问量
    private Integer readCount;

    public WebDto(String name, String url, String username, String password, Integer readCount) {
        this.name = name;
        this.url = url;
        this.username = username;
        this.password = password;
        this.readCount = readCount;
    }

    public WebDto() {}

    @Override
    public String toString() {
        return "WebDto{" +
                "name='" + name + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", readCount=" + readCount +
                '}';
    }
}

具体的测试方法:

//读取到列表
@Test
public void testReadList() throws Exception {
    List<WebDto> list = new ArrayList<WebDto>();

    HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:web-info.xls")));

    HSSFSheet sheet = book.getSheetAt(0);

    for(int i=2; i<sheet.getLastRowNum()+1; i++) {
        HSSFRow row = sheet.getRow(i);
        String name = row.getCell(0).getStringCellValue(); //名称
        String url = row.getCell(1).getStringCellValue(); //url
        String username = row.getCell(2).getStringCellValue();
        String password = row.getCell(3).getStringCellValue();
        Integer readCount = (int) row.getCell(4).getNumericCellValue();

        list.add(new WebDto(name, url, username, password, readCount));
    }

    System.out.println("共有 " + list.size() + " 条数据:");
    for(WebDto wd : list) {
        System.out.println(wd);
    }
}

执行测试方法后将得到:

共有 2 条数据:
WebDto{name='知识林', url='http://www.zslin.com', username='admin', password='111111', readCount=500}
WebDto{name='用户管理', url='http://basic.zslin.com/admin', username='admin', password='111111', readCount=123}

示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study15

本文章来自【知识林】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值