(低代码开发,用的是jar)
前端代码:
$.ajax({
url: './jd',
method: 'post',
async: false,
dataType: "json",
data: {
sid: $("#sid").val(), //不用管
cmd: "pa.exportI", //请求后端的路劲
id: id //传过去的参数
},
success: function (response) {
console.log("”)
}
})
后端代码:
@Mapping("pa.exportI") //这里的response一直接收的是null,里面的逻辑不用管 public int export(String id,HttpServletResponse response) throws IOException { // 根据逗号进行分割 String[] idArray = id.split(","); List<BO> boList = new ArrayList<>(); // 进行处理,例如遍历分割后的数组 for (String ids : idArray) { BO bo = SDK.getBOAPI().get("bo_eu_peo", ids);//根据ids查询一条数据 if(bo!=null){ BO newBO = new BO(); newBO.set("NAME",bo.getString("NAME")); newBO.set("ISDEPTHEAD",bo.getString("ISDEPTHEAD"));//是否是部门负责人 newBO.set("TWOVOTE",bo.getString("TWOVOTE"));//得票数 newBO.set("VOTE",bo.getString("VOTE"));//第二轮得票数 newBO.set("PLACEDEPT",bo.getString("PLACEDEPT"));//部门 boList.add(newBO); } } /*JSONObject object = new JSONObject(); object.put("a",boList);*/ // 创建 Excel 工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("BO Data"); int rowNum = 0; for (BO bo : boList) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(bo.getString("NAME")); row.createCell(1).setCellValue(bo.getString("ISDEPTHEAD")); row.createCell(2).setCellValue(bo.getString("TWOVOTE")); row.createCell(3).setCellValue(bo.getString("VOTE")); row.createCell(4).setCellValue(bo.getString("PLACEDEPT")); } // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=bo_data.xlsx"); // 将 Excel 数据写入响应流 OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.close(); return 1; }