前端代码:
exportDepData(){
const aa = this.getBaseUrl()+`${ANASALARY}${EXPORTDATA+'dep'}?deptLevel=${this.depValue}`;
window.location.href = aa;
},
后端controller层的代码:
//导出数据
@GetMapping("/exportDatadep")
public void exportData(HttpServletResponse resp, String deptLevel
) throws IOException {
//获取表格数据
List<AnaSalaryDepTable> tableParam = anaSalaryService.getDepParam(deptLevel);
XSSFWorkbook wb = new XSSFWorkbook();
String sheetName = "工资分析";
XSSFSheet sheet = wb.createSheet(sheetName);
//合并单元格
CellRangeAddress rangeAddress = new CellRangeAddress(2, 2, 0, 3);
sheet.addMergedRegion(rangeAddress);
sheet.setColumnWidth(0,5000);
CellStyle border = wb.createCellStyle();
border.setBorderBottom(BorderStyle.THIN); //下边框
border.setBorderLeft(BorderStyle.THIN);//左边框
border.setBorderTop(BorderStyle.THIN);//上边框
border.setBorderRight(BorderStyle.THIN);//右边框
XSSFRow row = sheet.createRow(2);
//创建单元格,指定起始列号,从0开始
XSSFCell cell = row.createCell(0);
//设置单元格内容
cell.setCellValue("员工部门分析");
CellStyle style = wb.createCellStyle();
//设置背景色
style.setFillForegroundColor(IndexColor.GREY_50_PERCENT);
//字体居中
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);
CellStyle coll1Style = wb.createCellStyle();
//字体居右
coll1Style.setAlignment(HorizontalAlignment.CENTER);
coll1Style.setBorderBottom(BorderStyle.THIN); //下边框
coll1Style.setBorderLeft(BorderStyle.THIN);//左边框
coll1Style.setBorderTop(BorderStyle.THIN);//上边框
coll1Style.setBorderRight(BorderStyle.THIN);//右边框
try {
row = null;
for (int i = 0; i < tableParam.size() + 1; i++) {
if (row == null) {
row = sheet.createRow(3);
XSSFCell cell1 = row.createCell(0);
XSSFCell cell2 = row.createCell(1);
XSSFCell cell3 = row.createCell(2);
XSSFCell cell4 = row.createCell(3);
//设置标头
cell1.setCellValue("部门");
cell2.setCellValue("工资总额");
cell3.setCellValue("平均工资");
cell4.setCellValue("人数");
//设置边框
cell1.setCellStyle(border);
cell3.setCellStyle(coll1Style);
cell2.setCellStyle(coll1Style);
cell1.setCellStyle(coll1Style);
cell4.setCellStyle(coll1Style);
} else {
int rowNum = row.getRowNum();
row = sheet.createRow(rowNum + 1);
AnaSalaryDepTable anaWorkAgeTable = tableParam.get(i - 1);
BigDecimal salarySum = anaWorkAgeTable.getSalarySum();
BigDecimal averageSalary = anaWorkAgeTable.getAverageSalary();
String salarysum = String.valueOf(salarySum);
String averagesalary = String.valueOf(averageSalary);
Double salary=0.0;
Double average=0.0;
if(salarysum!=null && !"null".equals(salarysum)){
salary = Double.valueOf(salarysum);
}
if(averagesalary!=null && !"null".equals(averagesalary)){
average = Double.valueOf(averagesalary);
}
XSSFCell cell1 = row.createCell(0);
cell1.setCellValue(anaWorkAgeTable.getDeptName());
XSSFCell cell2 = row.createCell(1);
cell2.setCellValue(anaWorkAgeTable.getCount());
XSSFCell cell3 = row.createCell(2);
cell3.setCellValue(salary);
XSSFCell cell4 = row.createCell(3);
cell4.setCellValue(average);
cell1.setCellStyle(border);
cell2.setCellStyle(border);
cell3.setCellStyle(border);
cell4.setCellStyle(border);
}
}
//动态控制图表的大小
int oneRowNum = sheet.getLastRowNum();
int row2=0;
int col2=0;
if(oneRowNum<16){
row2=17;
col2=14;
}else{
row2=oneRowNum+1;
col2=20;
}
//创建柱图
ExcelUtil.createBarChart(sheet, 4, oneRowNum,
0, 0, 2, 2, 5, 2, col2, row2, "部门", "工资总额", "员工工资分析");
ExcelUtil.downLoadExcel("工资分析", resp, wb);
} catch (Exception e) {
e.printStackTrace();
}
}

该博客介绍了如何通过前端与后端接口交互来实现实现Excel工资分析报表的导出。前端部分使用了URL拼接方式调用后端API,而后端Controller利用Java处理HTTP响应,生成并格式化Excel文件,包括设置单元格样式、合并单元格、创建图表等操作,最后将文件下载到客户端。

2773

被折叠的 条评论
为什么被折叠?



