设为首页 加入收藏

TOP

Java读取excel表格(二)
2017-10-12 18:17:17 】 浏览:4197
Tags:Java 读取 excel 表格
/usermodel/XSSFSheet.html.

这是在org.apache.poi.ss.usermodel包的接口。它是用于一排的电子表格的高层表示。它是代表了POI库的行所有类的超接口。

XSSFRow

这是在org.apache.poi.xssf.usermodel包的类。它实现了Row接口,因此它可以在电子表格中创建行。下面列出的是这个类在方法和构造函数。

类方法

S.No. 描述
1

createCell(int columnIndex)

创建新单元行并返回。

2

setHeight(short height)

设置短单位的高度。

对于此类的其余的方法,参考如下链接:
https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html

单元格

这是在org.apache.poi.ss.usermodel包的接口。它是代表了单元在电子表格中的行中的所有类的超接口。

单元格可以使用各种属性,例如空白,数字,日期,错误等单元格被添加到一个行之前应具有(基于0)自己的编号。

XSSFCell

这是在 org.apache.poi.xssf.usermodel 包的类。它实现了单元格界面。它是单元在电子表格中的行的一个高层次的表示。

 

现在我用Java实现读取excel表格

代码实现

public static void excel() throws Exception {
        //用流的方式先读取到你想要的excel的文件
        FileInputStream fis=new FileInputStream(new File(System.getProperty("user.dir")+"/src/excel.xls"));
        //解析excel
        POIFSFileSystem pSystem=new POIFSFileSystem(fis);
        //获取整个excel
        HSSFWorkbook hb=new HSSFWorkbook(pSystem);
        System.out.println(hb.getNumCellStyles());
        //获取第一个表单sheet
        HSSFSheet sheet=hb.getSheetAt(0);
        //获取第一行
    int firstrow=    sheet.getFirstRowNum();
    //获取最后一行
    int lastrow=    sheet.getLastRowNum();
    //循环行数依次获取列数
        for (int i = firstrow; i < lastrow+1; i++) {
            //获取哪一行i
            Row row=sheet.getRow(i);
            if (row!=null) {
                //获取这一行的第一列
            int firstcell=    row.getFirstCellNum();
            //获取这一行的最后一列
            int lastcell=    row.getLastCellNum();
            //创建一个集合,用处将每一行的每一列数据都存入集合中
            List<String> list=new ArrayList<>();
            for (int j = firstcell; j <lastcell; j++) {
                //获取第j列
                Cell cell=row.getCell(j);
                
                if (cell!=null) {
                    System.out.print(cell+"\t");
                    list.add(cell.toString());
                }
            }
            
            User user=new User();
            if (list.size()>0) {
                user.setUsername(list.get(1));
                user.setPassword(list.get(2));
            }
            BaseDAO dao=new BaseDAO();
            dao.save(user);
            System.out.println();
            }
        }
        fis.close();
    }

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SpringMVC 初级操作 下一篇数据库优化以及SQL优化小结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目