Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

导出面试人员地点安排Excel #42

Open
wants to merge 1 commit into
base: pre
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yundingshuyuan.recruit.dao;

import com.yundingshuyuan.recruit.domain.ReservationEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CustomReservationMapper {
List<ReservationEntity> getReservationDataForExport();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.yundingshuyuan.recruit.domain;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class ReservationEntity {
private Integer id;
private LocalDateTime interviewTime;
private Integer userId;
private Integer interviewId;
private String direction;
private String name;
private String qq;
private String gender;
private String location;

public ReservationEntity(Integer id, LocalDateTime interviewTime, Integer userId, Integer interviewId,
String direction, String name, String qq, String gender, String location) {
this.id = id;
this.interviewTime = interviewTime;
this.userId = userId;
this.interviewId = interviewId;
this.direction = direction;
this.name = name;
this.qq = qq;
this.gender = gender;
this.location = location;
}

// Getter and setter methods
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.yundingshuyuan.recruit.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class ExcelExporter {
public void exportToExcel(List<Map<String, Object>> dataList, OutputStream outputStream) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Reservation Data");

// 创建表头
Row headerRow = sheet.createRow(0);
int columnCount = 0;
for (String key : dataList.get(0).keySet()) {
Cell cell = headerRow.createCell(columnCount++);
cell.setCellValue(key);
}

// 填充数据
int rowCount = 1;
for (Map<String, Object> data : dataList) {
Row dataRow = sheet.createRow(rowCount++);
columnCount = 0;
for (Object value : data.values()) {
Cell cell = dataRow.createCell(columnCount++);
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else {
cell.setCellValue(value.toString());
}
}
}

workbook.write(outputStream);
workbook.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yundingshuyuan.recruit.dao.CustomReservationMapper">

<resultMap id="ReservationResultMap" type="com.yundingshuyuan.recruit.domain.ReservationEntity">
<id property="id" column="id"/>
<result property="interviewTime" column="interview_time"/>
<result property="userId" column="user_id"/>
<result property="interviewId" column="interview_id"/>
<result property="direction" column="direction"/>
<result property="name" column="name"/>
<result property="qq" column="qq"/>
<result property="gender" column="gender"/>
<result property="location" column="location"/>
</resultMap>

<select id="getReservationDataForExport" resultMap="ReservationResultMap">
SELECT r.id, r.interview_time, r.user_id, r.interview_id,
u.direction, u.name, u.qq, u.gender,
p.location
FROM reservation r
JOIN user_info u ON r.user_id = u.id
JOIN interview_position p ON r.interview_id = p.id
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.yundingshuyuan.recruit.web.controller;

import com.yundingshuyuan.recruit.dao.CustomReservationMapper;
import com.yundingshuyuan.recruit.domain.ReservationEntity;
import com.yundingshuyuan.recruit.utils.ExcelExporter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/export")
public class ExportController {
private final CustomReservationMapper customReservationMapper;

public ExportController(CustomReservationMapper customReservationMapper) {
this.customReservationMapper = customReservationMapper;
}

@GetMapping("/excel")
public void exportToExcel(HttpServletResponse response) {
List<ReservationEntity> reservationDataList = customReservationMapper.getReservationDataForExport();

// 将数据转换为 Map 对象列表
List<Map<String, Object>> dataList = convertToMapList(reservationDataList);

// 设置响应头信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=data.xlsx");

try {
// 获取输出流
OutputStream outputStream = response.getOutputStream();
// 使用 ExcelExporter 类导出数据到 Excel
ExcelExporter exporter = new ExcelExporter();
exporter.exportToExcel(dataList, outputStream);
outputStream.flush();
} catch (IOException e) {
log.error("Failed to export data to Excel: {}", e.getMessage());
}
}

private List<Map<String, Object>> convertToMapList(List<ReservationEntity> reservationDataList) {
List<Map<String, Object>> dataList = new ArrayList<>();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

for (ReservationEntity reservationEntity : reservationDataList) {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("编号", reservationEntity.getId());
dataMap.put("面试时间", reservationEntity.getInterviewTime().format(formatter));
dataMap.put("用户ID", reservationEntity.getUserId());
dataMap.put("面试ID", reservationEntity.getInterviewId());
dataMap.put("方向", reservationEntity.getDirection());
dataMap.put("姓名", reservationEntity.getName());
dataMap.put("QQ", reservationEntity.getQq());
dataMap.put("性别", reservationEntity.getGender());
dataMap.put("地点", reservationEntity.getLocation());

dataList.add(dataMap);
}

return dataList;
}
}