1、常规情况下前端请求后端,后端直接写入表格导出输入流即可
后端实现类(使用EasyExcel实现下载功能):
@Override
public void exportExcel(HttpServletResponse response) {
String realPath = "D://home/";
File folder = new File(realPath);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 导出文件名称
String fileName = realPath + System.currentTimeMillis() + "明细表.xlsx";
List<ProjectItemDTO> list = new ArrayList<>();
ProjectItemDTO projectItemDTO = new ProjectItemDTO();
projectItemDTO.setNumber("1");
projectItemDTO.setName("项目名称1");
projectItemDTO.setResponsibleDepartment("处室1");
projectItemDTO.setManagers("测试1");
projectItemDTO.setMembers("测试2");
projectItemDTO.setLevel("一般");
projectItemDTO.setBudgetAmount("450");
projectItemDTO.setPaidThisYear("360");
projectItemDTO.setItemNumber("230003188001074");
projectItemDTO.setBudgetType("新增预算");
list.add(projectItemDTO);
// 设置表格样式 加边框、设置自动换行
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setWrapped(true);
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.index);
EasyExcel.write(fileName, ProjectItemDTO.class).registerWriteHandler(
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle)).sheet("数据").doWrite(
data());
}
data() 获取导出数据信息
2、后端导出,前端响应输出流
vue前端代码
导出触发的事件方法
// 点击导出
clickToExport() {
exportExcel().then(res=>{
const fileName="计划表.xlsx"
const blob = new Blob([res],{
type: 'application/vnd.ms-excel'
})
const reader = new FileReader()
reader.readAsDataURL(blob)
reader.onload = function () {
const a = document.createElement('a')
a.download = fileName
a.href = URL.createObjectURL(blob)
document.body.appendChild(a)
a.click()
URL.revokeObjectURL(a.href)
document.body.removeChild(a)
}
})
},
路由配置
export function exportExcel() {
return request({
url: '/pc/exportExcel',
method: 'post',
responseType: 'blob'
})
}
后端代码
添加导入导出工具类文件
后端实现类:
@Override
public void exportExcel(){
// 获取输出流
HttpServletResponse response = RequestContextUtil.getResponse();
// int year = LocalDateTime.now().getYear();
List<List<Object>> sheet = new ArrayList<>();
List<Object> sheetHead = new ArrayList<>();
// sheetHead.add("序号");
sheetHead.add("项目名称");
sheetHead.add("主责部门");
sheetHead.add("项目经理");
sheetHead.add("项目成员");
sheetHead.add("项目分级");
sheetHead.add("预算金额(万元)");
sheetHead.add("今年支付");
sheetHead.add("事项编号");
sheetHead.add("预算部门");
sheetHead.add("预算名称");
sheetHead.add("预算类型");
sheet.add(sheetHead);
ArrayList<ProjectItemDTO> exportList = new ArrayList<>();
List<ProjectItemDTO> projectItemList = data();
for(ProjectItemDTO projectItemDTO:projectItemList){
exportList.add(projectItemDTO);
}
if(exportList.size()>0){
int i = 0;
for(ProjectItemDTO projectItemDTO:exportList){
i += 1;
List<Object> sheetrow = new ArrayList<>();
// sheetrow.add(i);
String number = StringUtils.isNotEmpty(projectItemDTO.getNumber())?projectItemDTO.getNumber():" ";
String name = StringUtils.isNotEmpty(projectItemDTO.getName())?projectItemDTO.getName():" ";
sheetrow.add(number + name);
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getResponsibleDepartment())?projectItemDTO.getResponsibleDepartment():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getManagers())?projectItemDTO.getManagers():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getMembers())?projectItemDTO.getMembers():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getLevel())?projectItemDTO.getLevel():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getBudgetAmount())?projectItemDTO.getBudgetAmount():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getPaidThisYear())?projectItemDTO.getPaidThisYear():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getItemNumber())?projectItemDTO.getItemNumber():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getBudgetDepartment())?projectItemDTO.getBudgetDepartment():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getBudgetName())?projectItemDTO.getBudgetName():" ");
sheetrow.add(StringUtils.isNotEmpty(projectItemDTO.getBudgetType())?projectItemDTO.getBudgetType():" ");
sheet.add(sheetrow);
}
}
// 导出执行
ExcelUtils.export(response, "计划明细表", sheet);
}
工具类代码及更多导入导出功能可参考该博主的文章:https://zyqok.blog.csdn.net/article/details/121994504
网友评论