通过Java代码实现对Excel文件内容写入并导出

本文介绍了如何通过Java和EasyExcel库处理Excel文件,包括引入相关依赖、读取和写入数据,以及处理单元格内容的逻辑。开发者可以学习如何将数据从模板Excel导入并动态填充内容后导出为新的Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在日常开发过程中,我们可能会遇到需要向Excel文件里面写入内容的情况,下面就给大家演示一下如何通过java实现对Excel文件内容写入和导出。
首先我们需要在pom文件中引入POI相关依赖:

            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>xmlbeans</artifactId>
                    <groupId>org.apache.xmlbeans</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>poi-ooxml</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>poi</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>poi-ooxml-schemas</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.12.0</version>
        </dependency>
在这里插入代码片

然后在代码中实现对Excel的读取和写入

        try {
            //根据类型获取不同的导出模板,读取本地Excel,转成文件写入流
            if ("2".equals(reqDto.getTimeAccuracy())) {
                inputStream = new FileInputStream(fileUrl + "2023年第四季度填报模板.xlsx");
            } else {
                inputStream = new FileInputStream(fileUrl + "2023年填报模板.xlsx");
            }
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            for (Row row : sheet) {
                if (row == null) {
                    continue;
                }
                String id = getCellStringValue(row.getCell(0));
                if (!"指标编号".equals(id.trim())) {
                    String name = getCellStringValue(row.getCell(1));
                    FillingTemplate fillingTemplate = compareList.get(id + "|" + name.trim());
                    if (ObjectUtil.isNotEmpty(fillingTemplate)) {
                        Cell cell = row.getCell(2);
                        //这里判断当前cell是否有值,没有值的话默认读取excel中cell是不存在的,需要手动创建
                        if (cell == null) {
                            cell = row.createCell(2);
                        }
                        //逻辑处理赋值,可忽略
                        if (!StringUtils.isEmpty(fillingTemplate.getValue())) {
                            cell.setCellValue(fillingTemplate.getValue());
                        } else {
                            if (!StringUtils.isEmpty(fillingTemplate.getIndicatorId())) {
                                IndicatorVo indicatorValue = getIndicatorValue(fillingTemplate.getIndicatorId(), data);
                                if (!ObjectUtil.isEmpty(indicatorValue.getOriginalValue()) && !ObjectUtil.isEmpty(indicatorValue.getValue())) {
                                    cell.setCellValue(indicatorValue.getValue().toString());
                                } else if (!ObjectUtil.isEmpty(indicatorValue.getOriginalValue()) && ObjectUtil.isEmpty(indicatorValue.getValue())) {
                                    cell.setCellValue(indicatorValue.getOriginalValue().toString());
                                }
                            }
                        }
                    }
                }
            }
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("123123", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setContentType("application/octet-stream;charset=utf-8");
            workbook.write(response.getOutputStream());
            workbook.close();
//            outputStream.close();

        } catch (Exception e) {
            log.error("修改文档异常:" + e.getMessage());
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
在这里插入代码片

以上就是读取excel,然后写入到excel流中的过程,然后就是在前端导出excel就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值