工具类:使用poi解析excel

工具类:使用poi解析excel

excel表格格式如下

解析excel表格的工具类

import com.cll.Article;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

//读取exel,将文件内容打印出来
public static void main(String[] args) throws IOException {
    List<Article> exceInfo = getExceInfo();
}


public static List<Article>  getExceInfo() throws IOException {
    FileInputStream fileInputStream = new FileInputStream("E:\\bigdata_29\\day06\\资料\\数据集\\baijia.xlsx");
    //获取我们解析excel表格的对象
    XSSFWorkbook xssfSheets = new XSSFWorkbook(fileInputStream);
    //获取excel的第一个sheet页
    XSSFSheet sheetAt = xssfSheets.getSheetAt(0);
    //获取我们sheet页的最后一行的数字,说白了就是看这个excel一共有多少行
    int lastRowNum = sheetAt.getLastRowNum();
    List<Article> articleList = new ArrayList<Article>();
    for(int i =1 ;i<lastRowNum;i++){
        Article article = new Article();
        //获取我们一行 行的数据
        XSSFRow row = sheetAt.getRow(i);
        //通过我们的row对象,解析里面一个个的字段
        XSSFCell title = row.getCell(0);
        XSSFCell from  = row.getCell(1);
        XSSFCell time = row.getCell(2);
        XSSFCell readCount = row.getCell(3);
        XSSFCell content = row.getCell(4);
        // System.out.println(title.toString());
        article.setId(i+"");
        article.setTitle(title.toString());
        article.setContent(content.toString());
        article.setFrom(from.toString());
        article.setReadCounts(readCount.toString());
        article.setTimes(time.toString());
        articleList.add(article);
    }
    fileInputStream.close();
    return  articleList;
}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值