2 Commits 988b5b51ec ... b449f55e5f

Author SHA1 Message Date
  大数据与最优化研究所 b449f55e5f Merge remote-tracking branch 'origin/dev' into dev 1 week ago
  大数据与最优化研究所 decd0f6b3e excel模板导出和数据导出 1 week ago

+ 160 - 45
src/main/java/com/xjrsoft/module/form/controller/FormExecuteController.java

@@ -16,18 +16,9 @@ import com.xjrsoft.common.model.result.RT;
 import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
 import com.xjrsoft.common.utils.DatasourceUtil;
 import com.xjrsoft.common.utils.LocalDateTimeUtil;
+import com.xjrsoft.module.attendance.entity.AttendanceStatistics;
 import com.xjrsoft.module.authority.dto.BatchSetDataAuthDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteAddOrUpdateDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteDeleteDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteInfoDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteListDto;
-import com.xjrsoft.module.form.dto.AppFormExecutePageDto;
-import com.xjrsoft.module.form.dto.FormExecuteAddOrUpdateDto;
-import com.xjrsoft.module.form.dto.FormExecuteDeleteDto;
-import com.xjrsoft.module.form.dto.FormExecuteInfoDto;
-import com.xjrsoft.module.form.dto.FormExecuteListDto;
-import com.xjrsoft.module.form.dto.FormExecutePageDto;
-import com.xjrsoft.module.form.dto.FormExecuteWorkflowInfoDto;
+import com.xjrsoft.module.form.dto.*;
 import com.xjrsoft.module.form.entity.FormDesignConfig;
 import com.xjrsoft.module.form.entity.FormRelease;
 import com.xjrsoft.module.form.entity.FormReleaseConfig;
@@ -37,6 +28,7 @@ import com.xjrsoft.module.form.service.IFormReleaseService;
 import com.xjrsoft.module.form.service.IFormTemplateService;
 import com.xjrsoft.module.generator.constant.ComponentTypeConstant;
 import com.xjrsoft.module.generator.entity.ColumnConfig;
+import com.xjrsoft.module.generator.entity.ImportConfig;
 import com.xjrsoft.module.generator.entity.TableConfig;
 import com.xjrsoft.module.student.service.IStudentConsumeDateService;
 import com.xjrsoft.module.system.entity.XjrTipsMessage;
@@ -45,13 +37,11 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import oracle.sql.INTERVALDS;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.Valid;
@@ -64,12 +54,14 @@ import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -243,42 +235,65 @@ public class FormExecuteController {
         return R.ok(formExecuteService.workFlowInfo(dto));
     }
 
+//    @GetMapping(value = "/export")
+//    @ApiOperation(value = "导出")
+//    @XjrLog(value = "导出表单数据")
+//    public ResponseEntity<byte[]> export(@Valid FormExecuteListDto dto) throws IOException {
     @PostMapping(value = "/export")
     @ApiOperation(value = "导出")
     @XjrLog(value = "导出表单数据")
-    public ResponseEntity<byte[]> export(@Valid @RequestBody FormExecuteListDto dto) {
+    public ResponseEntity<byte[]> export(@Valid @RequestBody FormExecuteListDto dto) throws IOException {
         FormRelease formRelease = formReleaseService.getById(dto.getReleaseId());
         FormReleaseConfig formReleaseConfig = JSONUtil.toBean(formRelease.getConfigJson(), FormReleaseConfig.class);
         // 配置excel第一行字段名
-        List<List<String>> head = new ArrayList<>();
         List<ColumnConfig> columnConfigs = formReleaseConfig.getListConfig().getColumnConfigs();
-        for (ColumnConfig config : columnConfigs) {
-            head.add(Collections.singletonList(config.getLabel()));
-        }
+        Map<String, ColumnConfig> columnConfigMap = columnConfigs.stream()
+                .collect(Collectors.toMap(ColumnConfig::getColumnName, c -> c));
+        List<ImportConfig> importConfigs = formReleaseConfig.getListConfig().getImportConfigs();
 
-        List<Map<Integer, Object>> exportList = new ArrayList<>();
-        if (!dto.getIsTemplate()) {
-            // 构建导出数据格式
-//            dto.setIsTrans(false);
-//            Boolean isPage = formReleaseConfig.getListConfig().getIsPage();
-            List<Entity> dataList = formExecuteService.list(dto);
-//            if (isPage) {
-//                dataList = formExecuteService.page(dto).getList();
-//            } else {
-//                dataList = formExecuteService.list(BeanUtil.toBean(dto, FormExecuteListDto.class));
+        // 开始写入
+        Workbook workbook = new XSSFWorkbook();
+        // 创建一个工作表(sheet)
+        String sheetName = "sheet1";
+        Sheet sheet = workbook.createSheet(sheetName);
+
+        // 设置第每列的列宽度
+//        for (ImportConfig importConfig : importConfigs) {
+//            if(importConfig.getWidth() == 0){
+//                sheet.autoSizeColumn(importConfig.getSortCode());
 //            }
-            // 时间日期格式转换
+//            sheet.setColumnWidth(importConfig.getSortCode(), 100);
+//        }
+
+        // 表头
+        createHead(workbook, sheet, importConfigs);
+        //生成数据
+        int dataRowNumber = 1;
+
+        if (!dto.getIsTemplate()) {
+            // 字体内容格式
+            Font font = workbook.createFont();
+            font.setBold(false);// 设置为粗体
+            font.setFontName("宋体");
+            font.setFontHeightInPoints((short)12);
+            font.setColor(Font.COLOR_RED);
+            CellStyle cellStyle = workbook.createCellStyle();
+            cellStyle.setFont(font); // 将字体应用到样式
+//            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
 
+            List<Entity> dataList = formExecuteService.list(dto);
             for (Entity entity : dataList) {
-                Map<Integer, Object> exportData = new HashMap<>(entity.size());
-                for (int i = 0; i < columnConfigs.size(); i++) {
-                    ColumnConfig config = columnConfigs.get(i);
-                    Object value = entity.get(config.getColumnName());
-                    String componentType = config.getComponentType();
+                Row dataRow = sheet.createRow(dataRowNumber);
+                for (int i = 0; i < importConfigs.size(); i++) {
+                    ImportConfig importConfig = importConfigs.get(i);
+                    sheet.autoSizeColumn(importConfig.getSortCode());
+                    ColumnConfig columnConfig = columnConfigMap.get(importConfig.getFieldName());
+                    Object value = entity.get(importConfig.getFieldName());
+                    String componentType = columnConfig.getComponentType();
                     // 日期时间字段格式化
                     if ((StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE, componentType)
                             || StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE_RANGE, componentType))) {
-                        String format = LocalDateTimeUtil.convertFontFormat(config.getFormat());
+                        String format = LocalDateTimeUtil.convertFontFormat(columnConfig.getFormat());
                         LocalDateTime dateTime = null;
                         if (value instanceof Date) {
                             dateTime = ((Date) value).toLocalDate().atStartOfDay();
@@ -298,22 +313,122 @@ public class FormExecuteController {
                         }
                         value = LocalDateTimeUtil.format(time);
                     }
-                    exportData.put(i, value);
+
+                    Cell cell = dataRow.createCell(i);
+                    cell.setCellValue((String)value);
+                    cell.setCellStyle(cellStyle);
                 }
-                exportList.add(exportData);
+                dataRowNumber ++;
             }
         }
+
+        //写入文件
         ByteArrayOutputStream bot = new ByteArrayOutputStream();
-        EasyExcel.write(bot).head(head).automaticMergeHead(false)
-                .registerWriteHandler(new FormContentStyleStrategy())
-                .excelType(ExcelTypeEnum.XLSX).sheet().doWrite(exportList);
+        workbook.write(bot);
         return R.fileStream(bot.toByteArray(), formReleaseConfig.getMenuConfig().getName() + ExcelTypeEnum.XLSX.getValue());
+//        FormRelease formRelease = formReleaseService.getById(dto.getReleaseId());
+//        FormReleaseConfig formReleaseConfig = JSONUtil.toBean(formRelease.getConfigJson(), FormReleaseConfig.class);
+//        // 配置excel第一行字段名
+//        List<List<String>> head = new ArrayList<>();
+//        List<ColumnConfig> columnConfigs = formReleaseConfig.getListConfig().getColumnConfigs();
+//        for (ColumnConfig config : columnConfigs) {
+//            head.add(Collections.singletonList(config.getLabel()));
+//        }
+//
+//        List<Map<Integer, Object>> exportList = new ArrayList<>();
+//        if (!dto.getIsTemplate()) {
+//            // 构建导出数据格式
+////            dto.setIsTrans(false);
+////            Boolean isPage = formReleaseConfig.getListConfig().getIsPage();
+//            List<Entity> dataList = formExecuteService.list(dto);
+////            if (isPage) {
+////                dataList = formExecuteService.page(dto).getList();
+////            } else {
+////                dataList = formExecuteService.list(BeanUtil.toBean(dto, FormExecuteListDto.class));
+////            }
+//            // 时间日期格式转换
+//
+//            for (Entity entity : dataList) {
+//                Map<Integer, Object> exportData = new HashMap<>(entity.size());
+//                for (int i = 0; i < columnConfigs.size(); i++) {
+//                    ColumnConfig config = columnConfigs.get(i);
+//                    Object value = entity.get(config.getColumnName());
+//                    String componentType = config.getComponentType();
+//                    // 日期时间字段格式化
+//                    if ((StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE, componentType)
+//                            || StrUtil.equalsIgnoreCase(ComponentTypeConstant.DATE_RANGE, componentType))) {
+//                        String format = LocalDateTimeUtil.convertFontFormat(config.getFormat());
+//                        LocalDateTime dateTime = null;
+//                        if (value instanceof Date) {
+//                            dateTime = ((Date) value).toLocalDate().atStartOfDay();
+//                        } else if (value instanceof Timestamp) {
+//                            dateTime = ((Timestamp)value).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+//                        }else if(value instanceof LocalDateTime){
+//                            dateTime = (LocalDateTime)value;
+//                        }
+//                        value = LocalDateTimeUtil.format(dateTime, format);
+//                    } else if ((StrUtil.equalsIgnoreCase(ComponentTypeConstant.TIME, componentType)
+//                            || StrUtil.equalsIgnoreCase(ComponentTypeConstant.TIME_RANGE, componentType))) {
+//                        LocalTime time = null;
+//                        if (value instanceof Time) {
+//                            time = ((Time) value).toLocalTime();
+//                        } else if (value instanceof INTERVALDS) {
+//                            time = LocalDateTimeUtil.convertIntervalToLocalTime((INTERVALDS)value);
+//                        }
+//                        value = LocalDateTimeUtil.format(time);
+//                    }
+//                    exportData.put(i, value);
+//                }
+//                exportList.add(exportData);
+//            }
+//        }
+//        ByteArrayOutputStream bot = new ByteArrayOutputStream();
+//        EasyExcel.write(bot).head(head).automaticMergeHead(false)
+//                .registerWriteHandler(new FormContentStyleStrategy())
+//                .excelType(ExcelTypeEnum.XLSX).sheet().doWrite(exportList);
+//        return R.fileStream(bot.toByteArray(), formReleaseConfig.getMenuConfig().getName() + ExcelTypeEnum.XLSX.getValue());
+    }
+
+    private void createHead(Workbook workbook, Sheet sheet, List<ImportConfig> importConfigs) {
+        int rowNumber = 0;
+        Font font = workbook.createFont();
+        font.setFontName("宋体");
+        font.setFontHeightInPoints((short)12);
+
+        // 正常样式
+        CellStyle normalCellStyle = workbook.createCellStyle();
+        normalCellStyle.setFont(font); // 将字体应用到样式
+        normalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+        normalCellStyle.setAlignment(HorizontalAlignment.CENTER);
+
+
+        // 必填样式
+        CellStyle requiredCellStyle = workbook.createCellStyle();
+        requiredCellStyle.setFont(font); // 将字体应用到样式
+        requiredCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+        requiredCellStyle.setAlignment(HorizontalAlignment.CENTER);
+        requiredCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
+        requiredCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+
+        // 所在行
+        Row row = sheet.createRow(rowNumber);
+
+        // 每一列
+        for (ImportConfig importConfig : importConfigs){
+            Cell cell = row.createCell(importConfig.getSortCode());
+            String content = importConfig.getLabel();
+            cell.setCellValue(content);
+            if(importConfig.getRequired()){
+                cell.setCellStyle(requiredCellStyle);
+            }else {
+                cell.setCellStyle(normalCellStyle);
+            }
+        }
     }
 
     @PostMapping("/import")
     @ApiOperation(value = "导入")
     public R importData(@RequestParam String releaseId, @RequestParam MultipartFile file) throws IOException {
-
         String sql = "SELECT id,code,value,remark FROM base_system_config WHERE code= 'studentConsumeDate' ";
         Map<String, Object> systemConfigMap = SqlRunnerAdapter.db().selectOne(sql);
         if (systemConfigMap != null && systemConfigMap.get("value") != null) {

+ 4 - 0
src/main/java/com/xjrsoft/module/form/service/IFormExecuteService.java

@@ -18,7 +18,11 @@ import com.xjrsoft.module.form.dto.FormExecuteWorkflowInfoDto;
 import com.xjrsoft.module.form.dto.FormExecuteWorkflowUpdateDto;
 import com.xjrsoft.module.form.vo.DeskTableInfoVo;
 import org.apache.commons.lang3.tuple.Triple;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestBody;
 
+import javax.validation.Valid;
+import java.io.ByteArrayOutputStream;
 import java.util.List;
 import java.util.Map;
 

+ 3 - 1
src/main/java/com/xjrsoft/module/form/service/impl/FormExecuteServiceImpl.java

@@ -40,6 +40,7 @@ import com.xjrsoft.module.form.entity.FormTemplate;
 import com.xjrsoft.module.form.mapper.FormReleaseMapper;
 import com.xjrsoft.module.form.mapper.FormTemplateMapper;
 import com.xjrsoft.module.form.service.IFormExecuteService;
+import com.xjrsoft.module.form.service.IFormReleaseService;
 import com.xjrsoft.module.form.utils.FormDataTransUtil;
 import com.xjrsoft.module.form.vo.DeskColumnsVo;
 import com.xjrsoft.module.form.vo.DeskTableInfoVo;
@@ -95,6 +96,8 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
 
     private final IAppFuncDesignService appFuncDesignService;
 
+    private final IFormReleaseService formReleaseService;
+
     /**
      * 需要模糊查询的
      */
@@ -2475,5 +2478,4 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         }
         return deskTableInfoVo;
     }
-
 }