| | |
| | | package com.yuanchu.mom.service.impl; |
| | | |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.context.AnalysisContext; |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.excel.metadata.data.ImageData; |
| | | import com.alibaba.excel.metadata.data.WriteCellData; |
| | | import com.alibaba.excel.util.FileUtils; |
| | | import com.alibaba.excel.util.ListUtils; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.yuanchu.mom.utils.QueryWrappers; |
| | | import com.yuanchu.mom.utils.UserUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | |
| | | /** |
| | | * 新增标准查新 |
| | | * |
| | | * @param processMethodSearchNewList |
| | | * @return |
| | | */ |
| | |
| | | index++; |
| | | |
| | | } |
| | | // 查询签名地址 |
| | | String writeUserUrl = UserUtils.getUserSignatureUrl(Integer.valueOf(writeUserId)); |
| | | String ratifyUserUrl = UserUtils.getUserSignatureUrl(Integer.valueOf(ratifyUserId)); |
| | | |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | //创建ExcelWriter 可以自动关流但还是手动关一次 |
| | | ExcelWriter excelWriter = null; |
| | | |
| | | try { |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("原材料检测信息导出", "UTF-8"); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); |
| | | // outputStream:要导出的文件的输出流 |
| | | OutputStream outputStream = response.getOutputStream(); |
| | | // 获取模版文件 |
| | | ClassPathResource classPathResource = new ClassPathResource("/static/excel/check-records.xlsx"); |
| | | // 使用模版文件的两种方式: |
| | | // 1、文件路径:.withTemplate(templateFileName) |
| | | // 2、输入流:.withTemplate(inputStream) |
| | | // String templateFileName = classPathResource.getFile().getPath(); |
| | | InputStream inputStream = classPathResource.getInputStream(); |
| | | // 创建ExcelWriter |
| | | excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build(); |
| | | // 获取第一个sheet页 |
| | | WriteSheet writeSheet = EasyExcel.writerSheet(0, "标准查新导出").build(); |
| | | //excelWriter.fill() 这地方就是填充属性。 |
| | | // excelWriter.fill(methodSearchNews, fillConfig, writeSheet); |
| | | excelWriter.fill(methodSearchNews, writeSheet); |
| | | |
| | | // 创建 ExcelWriter |
| | | ServletOutputStream outputStream = response.getOutputStream(); |
| | | ExcelWriter excelWriter = EasyExcel.write(outputStream).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build(); |
| | | // 这里easy excel模板导出问题 会删除后面内容再进行进行填充 |
| | | // 所以list 后面还有数据 想办法手动写入 |
| | | // 手动创建一个list 存放数据 然后再进行填充 |
| | | // 这里List<Object>可以用对象代替 偷懒用list |
| | | List<List<Object>> totalListList = ListUtils.newArrayList(); |
| | | List<Object> totalList = ListUtils.newArrayList(); |
| | | totalListList.add(totalList); |
| | | // 第一列 |
| | | totalList.add("查新人:"); |
| | | // 第二列 设置签名 |
| | | totalList.add(FileUtils.readFileToByteArray(new File(writeUserUrl))); |
| | | // 第三列 |
| | | totalList.add("日期:"); |
| | | // 第五列 |
| | | totalList.add("2024-12-23"); // 日期写死 |
| | | // 第六列 |
| | | totalList.add("审核:"); |
| | | // 第七列 |
| | | totalList.add(FileUtils.readFileToByteArray(new File(ratifyUserUrl))); |
| | | // 第八列 |
| | | totalList.add("日期:"); |
| | | // 第九列 |
| | | totalList.add("2024-12-23"); // 日期写死 |
| | | // 这里是write 别和fill 搞错了 |
| | | excelWriter.write(totalListList, writeSheet); |
| | | |
| | | // 获取 sheet0 对象 |
| | | WriteSheet mainSheet = EasyExcel.writerSheet(0, "标准查新导出").head(ProcessMethodSearchNewDto.class).build(); |
| | | // 设置输出流格式以及文件名: |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | String fileName = URLEncoder.encode( |
| | | "原材料检测信息导出", "UTF-8"); |
| | | response.setHeader("Content-disposition", |
| | | "attachment;filename=" + fileName + ".xlsx"); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | // 千万别忘记close关闭流 |
| | | if (excelWriter != null) { |
| | | excelWriter.close(); |
| | | |
| | | // 图片对象 |
| | | ImageData imageData = new ImageData(); |
| | | ImageData imageData1 = new ImageData(); |
| | | // 设置图片数据 |
| | | imageData.setImage(FileUtils.readFileToByteArray(new File(writeUserUrl))); // 设置编制人图片的文件路径 |
| | | imageData1.setImage(FileUtils.readFileToByteArray(new File(ratifyUserUrl))); // 设置批准人图片的文件路径 |
| | | |
| | | // 设置编制人图片位置 |
| | | imageData.setRelativeFirstRowIndex(1); |
| | | imageData.setRelativeFirstColumnIndex(2); |
| | | imageData.setRelativeLastRowIndex(1); |
| | | imageData.setRelativeLastColumnIndex(2); |
| | | // 设置批准人图片位置 |
| | | imageData1.setRelativeFirstRowIndex(1); |
| | | imageData1.setRelativeFirstColumnIndex(4); |
| | | imageData1.setRelativeLastRowIndex(1); |
| | | imageData1.setRelativeLastColumnIndex(4); |
| | | |
| | | |
| | | List<ImageData> list = new ArrayList<>(); |
| | | // 放入列表中 |
| | | list.add(imageData); |
| | | list.add(imageData1); |
| | | |
| | | // // 设置额外的文字(可选) |
| | | // WriteCellData<Void> writeCellData = new WriteCellData<>(); |
| | | //// imageDemoData.setWriteCellDataFile(writeCellData); |
| | | // // 这里可以设置为 EMPTY 则代表不需要其他数据了 |
| | | // writeCellData.setType(CellDataTypeEnum.STRING); |
| | | // writeCellData.setStringValue("额外的放一些文字"); |
| | | |
| | | |
| | | // 将图片数据设置到单元格中 |
| | | WriteCellData<ImageData> writeCellData = new WriteCellData<>(); |
| | | writeCellData.setImageDataList(list); |
| | | |
| | | // 创建数据列表 |
| | | List<List<WriteCellData>> dataList = new ArrayList<>(); |
| | | List<WriteCellData> rowData = new ArrayList<>(); |
| | | rowData.add(writeCellData); // 把图片放入一行 |
| | | dataList.add(rowData); |
| | | |
| | | |
| | | // 向 sheet0 写入数据(传入空list这样只导出表头) |
| | | excelWriter.write(methodSearchNews, mainSheet); |
| | | // 写入 图片 |
| | | excelWriter.write(dataList,mainSheet); |
| | | |
| | | //关闭流 |
| | | excelWriter.finish(); |
| | | |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | } |
| | | |