|
|
@@ -3,11 +3,7 @@ package com.xjrsoft.module.dataexpert.controller;
|
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.alibaba.excel.EasyExcel;
|
|
|
-import com.alibaba.excel.ExcelWriter;
|
|
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
-import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
|
@@ -31,12 +27,14 @@ import com.xjrsoft.module.dataexpert.service.IDataExpertTemplateService;
|
|
|
import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
|
|
|
import com.xjrsoft.module.dataexpert.vo.DataExpertTemplatePageVo;
|
|
|
import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateVo;
|
|
|
-import com.xjrsoft.module.student.entity.BaseStudent;
|
|
|
-import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
-import org.apache.commons.lang3.function.FailablePredicate;
|
|
|
+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 org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
@@ -49,6 +47,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
@@ -144,9 +144,9 @@ public class DataExpertTemplateController {
|
|
|
|
|
|
@PostMapping("/export-query")
|
|
|
@ApiOperation(value = "导出")
|
|
|
- public ResponseEntity<byte[]> exportData(@Valid @RequestBody DataExpertDto dto) {
|
|
|
+ public ResponseEntity<byte[]> exportData(@Valid @RequestBody DataExpertDto dto) throws SQLException, IOException {
|
|
|
//拼接字段
|
|
|
- String fields = "";
|
|
|
+ List<String> fields = new ArrayList<>();
|
|
|
Map<Integer, String> showNameMap = new HashMap<>();
|
|
|
List<String> titleList = new ArrayList<>();
|
|
|
List<DataExpertTemplateFieldVo> fieldList = dto.getFieldList();
|
|
|
@@ -156,10 +156,7 @@ public class DataExpertTemplateController {
|
|
|
for (int i = 0; i < fieldList.size(); i ++){
|
|
|
DataExpertTemplateFieldVo fieldVo = fieldList.get(i);
|
|
|
showNameMap.put(i, fieldVo.getShowName());
|
|
|
- if(i > 0){
|
|
|
- fields += ",";
|
|
|
- }
|
|
|
- fields += fieldVo.getFieldName();
|
|
|
+ fields.add("ifnull(" + fieldVo.getFieldName() + ", '')");
|
|
|
titleList.add(fieldVo.getShowName());
|
|
|
}
|
|
|
}else{
|
|
|
@@ -173,33 +170,34 @@ public class DataExpertTemplateController {
|
|
|
for (JsonElement jsonElement : fieldJson) {
|
|
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
showNameMap.put(i, jsonObject.get("name").getAsString());
|
|
|
- if(i > 0){
|
|
|
- fields += ",";
|
|
|
- }
|
|
|
- fields += jsonObject.get("field").getAsString();
|
|
|
+ fields.add("ifnull(" + jsonObject.get("field").getAsString() + ", '')");
|
|
|
titleList.add(jsonObject.get("name").getAsString());
|
|
|
i ++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//查出导出的数据并进行组装
|
|
|
- List<String[]> dataList = dataExpertTemplateService.getDataList(fields, expertSource.getViewName());
|
|
|
+ List<String[]> dataList = dataExpertTemplateService.getDataList(fields.toString().substring(1, fields.toString().length() - 1), expertSource.getViewName());
|
|
|
|
|
|
List<String[]> allDataList = new ArrayList<>();
|
|
|
allDataList.add(titleList.toArray(new String[titleList.size()]));
|
|
|
allDataList.addAll(dataList);
|
|
|
|
|
|
String sheetName = "数据";
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ Sheet sheet = workbook.createSheet(sheetName);
|
|
|
+ for (int i = 0; i < allDataList.size(); i ++){
|
|
|
+ Row row = sheet.createRow(i);
|
|
|
+ String[] data = allDataList.get(i);
|
|
|
+ for (int j = 0; j < data.length; j ++ ){
|
|
|
+ Cell cell = row.createCell(j);
|
|
|
+ cell.setCellValue(data[j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
String fileName = "导出结果" + ExcelTypeEnum.XLSX.getValue();
|
|
|
ByteArrayOutputStream bot = new ByteArrayOutputStream();
|
|
|
-
|
|
|
- ExcelWriter excelWriter = EasyExcel.write(bot).excelType(ExcelTypeEnum.XLSX).build();
|
|
|
- WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
|
|
|
-
|
|
|
- excelWriter.write(allDataList, writeSheet);
|
|
|
-
|
|
|
- excelWriter.finish();
|
|
|
-
|
|
|
+ workbook.write(bot);
|
|
|
return RT.fileStream(bot.toByteArray(), fileName);
|
|
|
}
|
|
|
|