# 导入导出实现详解
> 基于 EmployeeController 和 importAndExport 示例的完整导入导出流程
> 源码分析时间:2026-04-01
---
## 一、功能概述
职员管理示例是最完整的导入导出参考,包含:
- Excel 导出(可选字段)
- Excel 导入(预览 + 确认)
- Word 导出
- PDF 导出
---
## 二、前端实现
### 2.1 API 定义(employee.ts)
```typescript
import { defHttp } from '#/api/request';
enum Api {
Prefix = '/api/extend/Employee',
}
// 获取列表
export function getEmployeeList(data) {
return defHttp.get({ url: Api.Prefix, data });
}
// 删除
export function delEmployee(id) {
return defHttp.delete({ url: `${Api.Prefix}/${id}` });
}
// 导出Excel(可选字段)
export function exportExcel(data) {
return defHttp.get({ url: `${Api.Prefix}/ExportData`, data });
}
// 模板下载
export function templateDownload() {
return defHttp.get({ url: `${Api.Prefix}/TemplateDownload` });
}
// 导入
export function importData(data) {
return defHttp.post({ url: `${Api.Prefix}/ImportData`, data });
}
// 导入预览
export function importPreview(data) {
return defHttp.get({ url: `${Api.Prefix}/ImportPreview`, data });
}
```
### 2.2 导出弹窗(ExportModal.vue)
```vue
当前页面数据
全部页面数据
{{ item.fullName }}
```
### 2.3 导入弹窗(ImportModal.vue)
```vue
成功: {{ result.snum }}条
失败: {{ result.fnum }}条
```
### 2.4 主页面(index.vue)
```vue
导出
导入
```
---
## 三、后端实现
### 3.1 导出 Excel(可选字段)
```java
@GetMapping("/ExportData")
public ActionResult exportExcelData(PaginationEmployee paginationEmployee) {
String dataType = paginationEmployee.getDataType();
String selectKey = paginationEmployee.getSelectKey();
// 根据类型获取数据
List entityList = "0".equals(dataType)
? employeeService.getList(paginationEmployee)
: employeeService.getList();
// 构建导出数据
List modeList = new ArrayList<>();
for (EmployeeEntity entity : entityList) {
EmployeeModel mode = new EmployeeModel();
mode.setEnCode(entity.getEnCode());
mode.setFullName(entity.getFullName());
// ... 映射字段
modeList.add(mode);
}
// 根据选中的字段构建导出列
List entitys = new ArrayList<>();
String[] splitData = selectKey.split(",");
for (String key : splitData) {
if ("enCode".equals(key)) {
entitys.add(new ExcelExportEntity("工号", "enCode"));
}
if ("fullName".equals(key)) {
entitys.add(new ExcelExportEntity("姓名", "fullName"));
}
// ... 其他字段
}
// 导出
ExportParams exportParams = new ExportParams(null, "职员信息");
exportParams.setType(ExcelType.XSSF);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entitys, modeList);
// 上传文件
String fileName = "职员信息_" + DateUtil.dateNow("yyyyMMddHHmmss") + ".xlsx";
MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
FileInfo fileInfo = FileUploadUtils.uploadFile(
new FileParameter(FileTypeConstant.TEMPORARY, fileName), multipartFile);
// 返回
DownloadVO vo = new DownloadVO();
vo.setName(fileName);
vo.setUrl(UploaderUtil.uploaderFile(fileName + "#" + FileTypeConstant.TEMPORARY));
return ActionResult.success(vo);
}
```
### 3.2 导入预览
```java
@GetMapping("/ImportPreview")
public ActionResult