Java poi Excel导出

        excel导出有多个形式,该方法通过模板  平时导出 各种表格 和 复杂的定制化 excel导出需求。

        具体jar我也不太清楚了,这是之前项目里面的excel导出,这是最近整理之后将他们封装成一个工具类,将之前的重复的操作全部封装起来,直接调用,我们需要做的就是配置数据库的配置表,和写入数据的操作。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>


<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>

导出excel的一个流程:

        获取数据库配置,获取模板,反射动态调用实现方法,写入数据,保存文件,返回数据。​​​​​​​封装的excel工具类 :主要作用就是实现获取excel模板,保存文件,获取数据库配置,反射调用,返回给前端数据。

    public FileUpload DownloadConfig(FileRequestDto fileRequestDto) throws Exception {

        String fileItem = fileRequestDto.getFileItem();
        Date date = fileRequestDto.getDate();
        //获取数据库配置表
        DownloadConfig downloadConfig = downloadConfigDao.selectList(new QueryWrapper<DownloadConfig>().lambda().eq(DownloadConfig::getFileItem, fileItem)).stream().findFirst().orElse(null);
        if (downloadConfig != null) {
            //获取模板创建模板对象
            String templateUrl = downloadConfig.getTemplateUrl();
            File file = new File(templateUrl);
            FileInputStream fileInputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            fileRequestDto.setSheet(sheet);
            //反射调用
            Class<?> clazz = Class.forName(downloadConfig.getPackagePath() + downloadConfig.getClassName());
            DownloadExcelImpl downloadExcel = (DownloadExcelImpl) applicationContext.getBean(clazz);
            downloadExcel.handleDownload(fileRequestDto);
            String fileName = downloadConfig.getFileName();
            File file1 = new File(savePath + "\\" + fileItem + "\\" + date);
            if (!file1.exists()) {
                file1.mkdirs();//创建文件夹
                file1.createNewFile();
            }
            String path = file1.getPath() + "\\" + fileName + ".xlsx";
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            workbook.write(fileOutputStream);
            workbook.close();
            String relativePath = returnPath + "\\" + fileItem + "\\" + date + "\\" + fileName + ".xlsx";
            FileUpload fileUpload = new FileUpload();
            fileUpload.setRelativePath(relativePath.replace("\\", "/"));//文件下载的路径
            fileUpload.setFileName(fileName);//文件名称
            return fileUpload;
        }
        return null;
    }

      以上操作我们已经实现excel 导出的 一个公共的流程部分,接下来就是我们写入数据的部分,其实写入数据很简单,就是往单元格里面写入数据,需要注意的一点就是excel 的单元格 起始行和起始列 像我们的数组一样,下标都是从0开始。

写入数据:一段简单的测试代码。       

@Service
public class TestExcelService implements DownloadExcelImpl {
    @Override
    public void handleDownload(FileRequestDto fileRequestDto) throws Exception {
        Sheet sheet = fileRequestDto.getSheet();
        getimportExcel(sheet);
    }

    @Autowired
    private FileDownloadConfigDao fileDownloadConfigDao;

    public void getimportExcel(Sheet sheet) {
        List<FileDownloadConfig> fileDownloadConfigs = fileDownloadConfigDao.selectList(null);
        int intRow = 0;
        for (FileDownloadConfig downloadConfig : fileDownloadConfigs) {
            int intline = 0;
            CellUtils.writeCellString(sheet, intRow, intline, downloadConfig.getId());//id
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getUrl());//url
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getFileItem());//类型
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getFileTitle());//表头
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getFileName());//名称
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getTemplateUrl());//模板地址
            CellUtils.writeCellString(sheet, intRow, ++intline, downloadConfig.getStatus());//状态
            intRow++;
        }
    }
}

 数据库表。

package com.ee.mcods.file.entity.excel;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ee.mcods.core.entity.EntityBase;
import lombok.Data;

@Data
@TableName(value = "download_config")
public class DownloadConfig extends EntityBase {
    /**
     * 包名
     */
    private String packagePath;

    /**
     * 实现类名
     */
    private String className;

    /**
     * 访问地址
     */
    private String url;

    /**
     * 类型
     */
    private String fileItem;

    /**
     * 文件下载名称
     */
    private String fileTitle;

    /**
     * 访问模块名称
     */
    private String fileName;

    /**
     * 模板地址
     */
    private String templateUrl;

    /**
     * 排序字段
     */
    private Integer orderId;

    /**
     * 返回的json数据
     */
    @TableField(exist = false)
    private String jsonString;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值