POI工具操作excel(四)读取excel

1、创建要读取的excel

在电脑的D盘下面创建一个excel,内容大致如下:

2、读取excel数据

package com.zdw.poi;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;

/**
 * 导入excel,读取数据
 */
public class ExcelImput {
    public static void main(String[] args) throws Exception {
        //获取文件输入流
        FileInputStream inputStream = new FileInputStream("D:\\2345Downloads\\test.xlsx");
        //通过文件流创建 工作簿
        Workbook workbook = WorkbookFactory.create(inputStream);
        //通过工作簿得到第一个sheet,参数是从0开始
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();//得到行索引,从0开始
        System.out.println("这个excel文件共有:"+lastRowNum+"行");
        Row row = null;
        Cell cell = null;
        for(int rowNull = 1; rowNull<=lastRowNum; rowNull++){
            row = sheet.getRow(rowNull);//通过行号得到行信息
            //得到最大的列号,从1开始
            short lastCellNum = row.getLastCellNum();
            //System.out.println("最大的列号:"+lastCellNum);
            StringBuilder stringBuilder = new StringBuilder();
            for(short cellNum=1;cellNum<lastCellNum;cellNum++){
                cell = row.getCell(cellNum);
                //因为每个cell单元格的格式不同,所以要根据不同的格式,以不同的方式获取数据
                Object cellValue = getCellValue(cell);
                stringBuilder.append(cellValue).append("-");
            }
            System.out.println(stringBuilder.toString());
        }

    }

    public static Object getCellValue(Cell cell){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Object result = null;
        CellType cellType = cell.getCellType();//得到单元格的数据类型
        switch (cellType){
            case STRING: //字符串类型
                result = cell.getStringCellValue();
                break;
            case BOOLEAN: //boolean类型
                result = cell.getBooleanCellValue();
                break;
            case NUMERIC: //数字类型和日期类型都会走这里
                if(DateUtil.isCellDateFormatted(cell)){//表示的是日期
                    result = cell.getDateCellValue();
                    result = simpleDateFormat.format(result);
                }else{//表示的普通数字
                    result = cell.getNumericCellValue();
                }
                break;
            case FORMULA: //表示的公式
                result = cell.getCellFormula();
                break;
            default:
                break;
        }
        return result;
    }
}

3、打印控制台效果

这个excel文件共有:4行
1.0-张三-23.0-17773806476-男-2018/11/08-
2.0-李四-24.0-17773806471-男-2018/11/09-
3.0-王五-25.0-17773806472-男-2018/11/10-
4.0-赵六-26.0-17773806473-男-2018/11/11-

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己封装的excel导出/导入,可以根据注解来导出excel.本项目一共有13个类,里面还包含了一个反射工具,一个编码工具,10分值了。下面是测试代码 public class Test { public static void main(String[] arg) throws FileNotFoundException, IOException{ testBean(); testMap(); } public static void testBean() throws FileNotFoundException, IOException{ List l = new ArrayList(); for(int i=0;i<100;i++){ l.add(new MyBean()); } //很轻松,只需要二句话就能导出excel BeanExport be = ExportExcel.BeanExport(MyBean.class); be.createBeanSheet("1月份", "1月份人员信息").addData(l); be.createBeanSheet("2月份","2月份人员信息").addData(l); be.writeFile("E:/test/bean人员信息8.xlsx"); } //如果不想用注解,还能根据MAP导出. public static void testMap () throws FileNotFoundException, IOException{ List l = new ArrayList(); l.add(new MapHeader("姓名","name",5000)); l.add(new MapHeader("年龄","age",4000)); l.add(new MapHeader("生日","birthdate",3000)); l.add(new MapHeader("地址","address",5000)); l.add(new MapHeader("双精度","d",4000)); l.add(new MapHeader("float","f",6000)); List<Map> lm = new ArrayList<Map>(); for(int i=0;i<100;i++){ Map map = new HashMap(); map.put("name","闪电球"); map.put("age",100); map.put("birthdate",new Date()); map.put("address","北京市广东省AAA号123楼!"); map.put("d",22.222d); map.put("f",295.22f); lm.add(map); } MapExport me = ExportExcel.mapExport(l); me.createMapSheel("1月份","广东省人员信息").addData(lm); me.createMapSheel("2月份", "北京市人员信息").addData(lm); me.writeFile("E:/test/map人员信息9.xlsx"); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值