Java用POI解析Excel、读取Excel、导出Excel

作者在这里用的是POI-5.0.0的jar包,但是只用这个包直接跑项目是跑不起来的,会报各种类找不到的错。结果最终需要8个jar包才可以正常运行,我把这8个jar包整理了一下,上传到了CSDN资源,点击这里下载
8个jar包截图如下:
在这里插入图片描述
除了截图里面的commons-lang3-3.12.0.jar这个包,我用到了这个工具类包里面的StringUtils,其余的8个jar包缺一不可,少一个项目都会报错跑不起来。
现在贴读取excel和导出excel的关键代码:

读取Excel数据的关键代码如下

 public static List<Bean01> Excel01() throws Exception {
        //定义一个list 集合保存从excel解析的用户
        List<Bean01> bean01List = new ArrayList<>();
        //1.读取要解析的excel文件
        InputStream inputStream = new FileInputStream(new File("03.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        //2.获取工作表对象
        XSSFSheet sheet = wb.getSheetAt(0);
        //3.得到行的迭代器
        Iterator<Row> iterator = sheet.iterator();
        int rowNum = 0;
        while (iterator.hasNext()) {
            Bean01 bean01 = new Bean01();
            Row row = iterator.next();
            //跳过标题行
            if (rowNum == 0) {
                rowNum++;
                continue;
            }
            for (int i = 0; i < 2; i++) {
                String result = getValue(row.getCell(i));//获取到单元格内的数据,方法见下
                result = StringUtils.deleteWhitespace(result);
//                System.out.println(result);
                if (i == 0) {
                    bean01.setCode(result);
                } else if (i == 1) {
                    bean01.setName(result);
                }
            }
            bean01List.add(bean01);
        }
        return bean01List;

    }
  /**
     * 获取单元格内的数据,并进行格式转换
     *
     * @param cell
     * @return
     */
    private static String getValue(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case NUMERIC:// 数值和日期均是此类型,需进一步判断
                if (DateUtil.isCellDateFormatted(cell)) {
                    //是日期类型
                    return String.valueOf(cell.getDateCellValue());
                } else {
                    //是数值类型
                    return String.valueOf(cell.getNumericCellValue());
                }
            default:
                return null;
        }
    }

将数据导出至Excel表格的关键代码如下

 public static void printExcel(List<Bean02> bean02List) {
        try {
            File file = new File("out.xlsx");

            //2.读取excel模板,创建excel对象
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet sheet = wb.getSheetAt(0);
            //4.定义一些可复用的对象
            int rowIndex = 0; //行的索引
            int cellIndex = 1; //单元格的索引
            XSSFRow nRow = null;
            Cell nCell = null;
            //5.读取大标题行
            nRow = sheet.createRow(rowIndex++); // 使用后 +1
            //6.读取大标题的单元格
            nCell = nRow.createCell(cellIndex);
            //7.设置大标题的内容
            String bigTitle = "月份新增用户表";
            nCell.setCellValue(bigTitle);
            //8.跳过第二行(模板的小标题,我们要用)
            rowIndex++;
            //9.读取第三行,获取它的样式
            nRow = sheet.createRow(rowIndex);
            //行高
            float lineHeight = 50.f;
            //12.遍历数据
            for (Bean02 bean02 : bean02List) {
                //13.创建数据行
                nRow = sheet.createRow(rowIndex++);
                //16.设置数据行高
                nRow.setHeightInPoints(lineHeight);
                //17.重置cellIndex,从第一列开始写数据
                cellIndex = 1;
                //18.创建数据单元格,设置单元格内容和样式
                //类型
                nCell = nRow.createCell(cellIndex++);
                if (StringUtils.isEmpty(bean02.getType())) {
                    nCell.setCellValue("");
                } else {
                    nCell.setCellValue(bean02.getType());
                }
                //名称
                nCell = nRow.createCell(cellIndex++);
                if (StringUtils.isEmpty(bean02.getName())) {
                    nCell.setCellValue("");
                } else {
                    nCell.setCellValue(bean02.getName());
                }
                //code
                nCell = nRow.createCell(cellIndex++);
                if (StringUtils.isEmpty(bean02.getCode())) {
                    nCell.setCellValue("");
                } else {
                    nCell.setCellValue(bean02.getCode());
                }

            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            wb.write(fileOutputStream); //将工作簿写到输出流中
            fileOutputStream.flush();
            fileOutputStream.close();
            wb.close();

        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

注意,这个地方的out.xlsx文件是提前打开Excel软件保存一个空白的表格文件到这里,也可以弄成通用的模板,这样才能把数据往这个已经存在的excel文件导入进去,大概的关键逻辑代码就这样,根据自己的需求,复制这些代码,改吧改吧,就能用了。
贴一个Java项目的目录结构:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄油奥特曼

如果可以,请我喝一杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值