导出班级学员成绩

This commit is contained in:
wuchunlei
2025-05-06 14:11:39 +08:00
parent f0202bbc47
commit dab1ae8202
2 changed files with 157 additions and 0 deletions

View File

@@ -17,6 +17,9 @@ import com.peanut.modules.medical.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
@@ -25,8 +28,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -395,6 +402,65 @@ public class ClassController {
return R.ok().put("result",classEntityService.userScoreList(params));
}
//导出学员成绩
@RequestMapping("/exportUserScore")
public void exportUserScore(HttpServletResponse response,@RequestBody Map<String,Object> params){
List<Map<String,Object>> userScoreList = classEntityService.userScoreList(params);
XSSFWorkbook wb = new XSSFWorkbook();
//创建一张表
Sheet sheet = wb.createSheet("Student");
//创建第一行起始为0
Row titleRow = sheet.createRow(0);
titleRow.createCell(0).setCellValue("姓名");
titleRow.createCell(1).setCellValue("电话");
titleRow.createCell(2).setCellValue("邮箱");
titleRow.createCell(3).setCellValue("任务");
titleRow.createCell(4).setCellValue("医案");
titleRow.createCell(5).setCellValue("心得");
titleRow.createCell(6).setCellValue("思考题");
titleRow.createCell(7).setCellValue("考试");
titleRow.createCell(8).setCellValue("总成绩");
//序号默认为1
int cell = 1;
//遍历
for (Map<String,Object> map : userScoreList) {
Row row = sheet.createRow(cell);
MyUserEntity user = (MyUserEntity)map.get("user");
row.createCell(0).setCellValue(user.getName());
row.createCell(1).setCellValue(user.getTel());
row.createCell(2).setCellValue(user.getEmail());
row.createCell(3).setCellValue(map.get("task0Score").toString());
row.createCell(4).setCellValue(map.get("task1Score").toString());
row.createCell(5).setCellValue(map.get("experienceScore").toString());
row.createCell(6).setCellValue(map.get("questionScore").toString());
row.createCell(7).setCellValue(map.get("examScore").toString());
row.createCell(8).setCellValue(map.get("userScore").toString());
//序号自增
cell++;
}
String fileName = "学生成绩.xlsx";
OutputStream outputStream =null;
try {
//文件名编码格式
fileName = URLEncoder.encode(fileName,"UTF-8");
//设置ContentType请求信息格式
response.setContentType("application/vnd.ms-excel");
//设置标头
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
wb.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//结班
@RequestMapping("/closeClass")
@Transactional

View File

@@ -6,9 +6,18 @@ import com.peanut.common.utils.HttpClientUtils;
import com.peanut.common.utils.R;
import com.peanut.modules.book.service.MyUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -24,6 +33,88 @@ public class WeixinApiController {
@Autowired
private MyUserService userInfoService;
public static void main(String[] args) {
String token = getAccessToken("ed1eb116f91a49a4ec1ec37c6f8f9495");
String tel = login(token,"0a3nul100iDgdU1T0J300E82Yk1nul1O");
System.out.println(tel);
}
public static String getAccessToken(String secret) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 拼接URL
String url = "https://api.weixin.qq.com/cgi-bin/token?" +
"appid=wxff97e3bb73cf57d2"+
"&secret="+secret+
"&grant_type=client_credential";
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json;charset=utf8");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
String token = jsonObject.getString("access_token");
return token;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
public static String login(String accessToken,String code) {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?" +
"access_token="+accessToken;
Map<String, Object> entity = new HashMap<>();
entity.put("code", code);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
httpPost.setEntity(new StringEntity(JSONObject.toJSONString(entity),"utf-8"));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
String tel = phoneInfo.getString("phoneNumber");
return tel;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
//微信扫描后回调的方法
// @GetMapping("callback")