前端页面:
一个简单的Vue导出按钮!
<!--导出按钮-->
<el-form-item>
<el-button size="mini" type="primary" @click="exportData">导出</el-button>
</el-form-item>
这个是前端给后台控制端传送值的地点!不知道写法对不对 但是可以给后端传递到需要的数据!
// 导出Excel表
const exportData = async () => {
const { data } = await _this.$API.system.get(
_this.$BASEDATA.exportData, {
//给后台传递需要的值
projectId: reactiveParams.infoForm.projectId,
}
);
};
后端代码
Gatway层
//导出Excel表
void exportInvoice(List<DataMyannotationsEntity> list, String path) throws IOException;
//导出Excel表
@Override
public void exportInvoice(List<DataMyannotationsEntity> list, String path) throws IOException {
dataProjectconterService.exportInvoice(list, path);
}
Server层
//导出Excel表
void exportInvoice(List<DataMyannotationsEntity> list, String path) throws IOException;
/**
* 导出Excel
* @param list
* @param path
* @throws IOException
*/
@Override
public void exportInvoice(List<DataMyannotationsEntity> list, String path) throws IOException {
//创建工作表
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet();
XSSFRow row=sheet.createRow(0);
//设置单元格内容
row.createCell(0).setCellValue("标注日期");
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("题包号");
row.createCell(3).setCellValue("计划标注量");
row.createCell(4).setCellValue("实际标注量");
//遍历导出数据
for(int i =0;i<list.size();i++){
XSSFRow row1 = sheet.createRow(i + 1);
row1.createCell(0).setCellValue(list.get(i).getProjectTime());
row1.createCell(1).setCellValue(list.get(i).getUpdatedBy());
row1.createCell(2).setCellValue(list.get(i).getProjectName());
row1.createCell(3).setCellValue(list.get(i).getPackageFrames()+"/"+list.get(i).getPackageCircles());
row1.createCell(4).setCellValue(list.get(i).getFrames()+"/"+list.get(i).getCircles());
}
FileOutputStream fileOutputStream=new FileOutputStream(path);
workbook.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
workbook.close();
}
Controller层
/**
* 导出Excel表,导出的excel表会直接存在于系统桌面上
* @param dataMyannotationsEntity :获取前端传入过来的数据
* @param projectId
*/
@GetMapping("/exportData")
public void exportData(@Validated DataMyannotationsEntity dataMyannotationsEntity,
@RequestParam(defaultValue = "", name = "projectId") String projectId) {
//构造器查询
QueryWrapper<DataMyannotationsEntity> queryWrapper=new QueryWrapper<>();
//输入查询条件,根据指定的字段排序
queryWrapper.eq("project_id", projectId)
.orderByAsc("project_time");
//查询符合条件的字段
List<DataMyannotationsEntity> list = dataMyannotationsService.getBaseMapper().selectList(queryWrapper);
try {
//获取系统桌面路径
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktoPath = desktopDir.getAbsolutePath();
//导出的文件加名
String path = desktoPath + "\\标注计划导出.xlsx";
dataProjectcenterService.exportInvoice(list, path);
} catch (Exception e) {
e.printStackTrace();
}
}
总结: 项目经验太少,逻辑思维不清楚,对于前端技术的迷惘!