Compare commits

...

2 Commits

  1. 42
      yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java
  2. 11
      yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/controller/admin/exa/EXAController.java
  3. 7
      yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/controller/admin/instant/vo/InstantPageReqVO.java
  4. 3
      yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/dal/mysql/instant/InstantMapper.java

42
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/excel/core/util/ExcelUtils.java

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.framework.excel.core.handler.SelectSheetWriteHandler; import cn.iocoder.yudao.framework.excel.core.handler.SelectSheetWriteHandler;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.converters.longconverter.LongStringConverter; import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -54,4 +55,45 @@ public class ExcelUtils {
.doReadAllSync(); .doReadAllSync();
} }
/**
* 将列表以 CSV 响应给前端
*
* @param response 响应
* @param filename 文件名
* @param sheetName Excel sheet
* @param head Excel head
* @param data 数据列表哦
* @param <T> 泛型保证 head data 类型的一致性
* @throws IOException 写入失败的情况
*/
public static <T> void writeCSV(HttpServletResponse response, String filename, String sheetName,
Class<T> head, List<T> data) throws IOException {
response.setHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(filename));
// response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setContentType("application/vnd.opencmlformats-officedocument.spreadsheetml.sheet");
// 输出 Excel
EasyExcel.write(response.getOutputStream(), head)
.excelType(ExcelTypeEnum.CSV)
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
.registerWriteHandler(new SelectSheetWriteHandler(head)) // 基于固定 sheet 实现下拉框
.registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
.sheet(sheetName).doWrite(data);
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
response.setHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(filename));
// response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setContentType("application/vnd.opencmlformats-officedocument.spreadsheetml.sheet");
}
public static <T> List<T> readCSV(MultipartFile file, Class<T> head) throws IOException {
return EasyExcel.read(file.getInputStream(), head, null)
.excelType(ExcelTypeEnum.CSV)
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
.doReadAllSync();
}
} }

11
yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/controller/admin/exa/EXAController.java

@ -105,8 +105,13 @@ public class EXAController {
Point.builder().ItemName("gbqegj").GroupName("test").ItemType(5).Descriptor("fqnewfej") Point.builder().ItemName("gbqegj").GroupName("test").ItemType(5).Descriptor("fqnewfej")
.EngUnits("ge").Source("批量录入").AutoSave(true).build() .EngUnits("ge").Source("批量录入").AutoSave(true).build()
); );
// 输出
ExcelUtils.write(response, "用户导入模板.xls", "测点列表", Point.class, list);
// 输出excel
// ExcelUtils.write(response, "用户导入模板.xls", "测点列表", Point.class, list);
//输出csv
ExcelUtils.writeCSV(response, "用户导入模板.csv", "测点列表", Point.class, list);
} }
@PostMapping("/import") @PostMapping("/import")
@ -119,7 +124,7 @@ public class EXAController {
public CommonResult<EXAPointImportRespVO> importExcel(@RequestParam("file") MultipartFile file, public CommonResult<EXAPointImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
System.out.println(file); System.out.println(file);
List<Point> list = ExcelUtils.read(file, Point.class); List<Point> list = ExcelUtils.readCSV(file, Point.class);
return success(exaService.importPointList(list, updateSupport)); return success(exaService.importPointList(list, updateSupport));
} }

7
yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/controller/admin/instant/vo/InstantPageReqVO.java

@ -10,8 +10,13 @@ import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class InstantPageReqVO extends PageParam { public class InstantPageReqVO extends PageParam {
@Schema(description = "机组id", example = "id") @Schema(description = "机组id", example = "id")
private Integer unitId; private Integer unit;
@Schema(description = "系统id", example = "id")
private Integer type;
@Schema(description = "子系统id", example = "id")
private Integer system;
@Schema(description = "系统id", example = "yudao") @Schema(description = "系统id", example = "yudao")
private Integer modelId; private Integer modelId;

3
yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/dal/mysql/instant/InstantMapper.java

@ -17,6 +17,9 @@ public interface InstantMapper extends BaseMapperX<InstantDO> {
.eqIfPresent(InstantDO::getIsUpdate,reqVO.getIsUpdate()) .eqIfPresent(InstantDO::getIsUpdate,reqVO.getIsUpdate())
.eqIfPresent(InstantDO::getRunning,reqVO.getRunning()) .eqIfPresent(InstantDO::getRunning,reqVO.getRunning())
.eqIfPresent(InstantDO::getRunningLog,reqVO.getRunningLog()) .eqIfPresent(InstantDO::getRunningLog,reqVO.getRunningLog())
.eqIfPresent(InstantDO::getUnitId,reqVO.getUnit())
.eqIfPresent(InstantDO::getSystemTypeId,reqVO.getType())
.eqIfPresent(InstantDO::getSystemId,reqVO.getSystem())
.orderByAsc(InstantDO::getId)); .orderByAsc(InstantDO::getId));
} }

Loading…
Cancel
Save