excel导出有多个形式,该方法通过模板 平时导出 各种表格 和 复杂的定制化 excel导出需求。
具体jar我也不太清楚了,这是之前项目里面的excel导出,这是最近整理之后将他们封装成一个工具类,将之前的重复的操作全部封装起来,直接调用,我们需要做的就是配置数据库的配置表,和写入数据的操作。
org.apache.poi
poi
4.1.0
org.apache.poi
poi-ooxml
4.1.0
导出excel的一个流程:
获取数据库配置,获取模板,反射动态调用实现方法,写入数据,保存文件,返回数据。封装的excel工具类 :主要作用就是实现获取excel模板,保存文件,获取数据库配置,反射调用,返回给前端数据。
public FileUpload DownloadConfig(FileRequestDto fileRequestDto) throws Exception {
String fileItem = fileRequestDto.getFileItem();
Date date = fileRequestDto.getDate();
//获取数据库配置表
DownloadConfig downloadConfig = downloadConfigDao.selectList(new QueryWrapper().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 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;
}