当前位置:   article > 正文

JAVA 集合数据EXCEL导出并上传OSS_java excel导出上传至oss文件服务器

java excel导出上传至oss文件服务器

前言:上文讲述如何上传EXCEL文件,从指定行读取数据,且支持行数据与java属性值相互转换,

entity对象定义方式与上文一致

EXCEL上传指定行读取数据_T浩浩的博客-CSDN博客

前提:需要导出的数据

List<ProductImportReq> exportList=Lists.newArrayList();

导出方式一:直接通过流返回给前端

  1. public void export(HttpServletResponse response) throws IOException {
  2. List<ProductExportExcelResp> exportList = Lists.newArrayList();
  3. response.setContentType("application/vnd.ms-excel");
  4. response.setCharacterEncoding("utf-8");
  5. // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
  6. String fileName = URLEncoder.encode("平台商品主档-"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")), "UTF-8");
  7. // String fileName = "失败文件-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
  8. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  9. EasyExcel.write(response.getOutputStream(), ProductExportExcelResp.class)
  10. .sheet("sheet1")
  11. // 设置字段宽度为自动调整,不太精确
  12. .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
  13. .doWrite(exportList);
  14. }

导出方式二:将List集合转成excel并上传到OSS

excel信息承载类

  1. @AllArgsConstructor
  2. @NoArgsConstructor
  3. @Data
  4. public class EasyExcelParams<T> {
  5. /**
  6. * 文件名称
  7. */
  8. private String fileName;
  9. /**
  10. * sheet名称
  11. */
  12. private String sheetName;
  13. /**
  14. * 数据
  15. */
  16. private List<T> data;
  17. /**
  18. * 数据模型类型
  19. */
  20. private Class<T> dataModelClazz;
  21. /**
  22. * cell handler
  23. */
  24. private List<CellWriteHandler> strategies;
  25. public EasyExcelParams(String fileName,String sheetName, List<T> data, Class<T> dataModelClazz) {
  26. this.fileName=fileName;
  27. this.sheetName = sheetName;
  28. this.data = data;
  29. this.dataModelClazz = dataModelClazz;
  30. }
  31. public EasyExcelParams(String fileName,String sheetName, List<T> data, Class<T> dataModelClazz, CellWriteHandler... strategies) {
  32. this(fileName,sheetName, data, dataModelClazz);
  33. this.strategies = Arrays.asList(strategies);
  34. }
  35. public boolean validate() {
  36. return StringUtils.isNotBlank(sheetName)
  37. && Objects.nonNull(dataModelClazz)
  38. && Objects.nonNull(data);
  39. }
  40. }

流对象处理工具类

  1. public class EasyExcelToStreamUtil {
  2. public static byte[] toInputStream(EasyExcelParams params){
  3. try {
  4. return YLZExcelUtils.getExcelBytes(params);
  5. } catch (ExcelException e) {
  6. throw new RuntimeException(e);
  7. }
  8. }
  9. }

文件上传处理类

  1. @RefreshScope
  2. @Component
  3. public class FileUploadUtil<T> {
  4. @Autowired
  5. private RemoteFileService remoteFileService;
  6. @Value(value = "${file.url.prefix}")
  7. private String prefix;
  8. public String uploadExcel(String fileName, String sheetName, List<T> data, Class<T> dataModelClazz){
  9. EasyExcelParams excelParams=new EasyExcelParams(fileName,sheetName,data,dataModelClazz);
  10. byte[] dateByte = EasyExcelToStreamUtil.toInputStream(excelParams);
  11. FileUploadDTO fileUploadDTO=new FileUploadDTO();
  12. fileUploadDTO.setFileName(fileName);
  13. fileUploadDTO.setData(dateByte);
  14. R result = remoteFileService.uploadExcel(fileUploadDTO);
  15. Map<String,String> fileResult= (Map<String, String>) result.getData();
  16. String fileUrl=null!=fileResult.get("url")? prefix+fileResult.get("url") :"";
  17. return fileUrl;
  18. }
  19. }

OSS文件上传实现 上传实现根据项目实际情况选择,实现步骤 1.设置参数 2.将byte[]转成InputStream, 3.上传 保存文件地址

  1. @Slf4j
  2. @Service
  3. @AllArgsConstructor
  4. public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
  5. //具体上传OSS方式根据项目实际情况选择
  6. private final FileTemplate fileTemplate;
  7. public R uploadExcel(FileUploadDTO fileUploadDTO) {
  8. Map<String, String> resultMap = new HashMap<>(4);
  9. resultMap.put("bucketName", properties.getBucketName());
  10. resultMap.put("fileName", fileUploadDTO.getFileName());
  11. if(null==fileUploadDTO.getData()){
  12. return R.ok("");
  13. }
  14. try (InputStream inputStream = new ByteArrayInputStream(fileUploadDTO.getData())) {
  15. fileTemplate.putObject(properties.getBucketName(),
  16. fileUploadDTO.getFileName(), inputStream, "vnd.ms-excel");
  17. }
  18. catch (Exception e) {
  19. log.error("上传失败", e);
  20. return R.failed(e.getLocalizedMessage());
  21. }
  22. return R.ok(resultMap);
  23. }
  24. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/599153
推荐阅读
相关标签
  

闽ICP备14008679号