no message
							parent
							
								
									fc5f67d70f
								
							
						
					
					
						commit
						4aa8f25ae8
					
				| 
						 | 
				
			
			@ -124,7 +124,6 @@ public class AddressController {
 | 
			
		|||
    @GetMapping("/address/exportAddress/{taskId}")
 | 
			
		||||
    @SaCheckPermission("address:exportAddress")
 | 
			
		||||
    public void exportAddress(@PathVariable String taskId, HttpServletResponse response) {
 | 
			
		||||
        System.out.println(exportTaskService.isTaskExists(taskId));
 | 
			
		||||
        addressQueryService.exportAddress(taskId, response);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,7 +56,7 @@ public interface AddressQueryService {
 | 
			
		|||
 | 
			
		||||
    List<AddressExcelVO> queryAddressExcel();
 | 
			
		||||
 | 
			
		||||
    List<AddressExcelVO> queryAddressExcel2();
 | 
			
		||||
    List<AddressExcelVO> queryAddressExcel2(String taskId);
 | 
			
		||||
 | 
			
		||||
    List<AddressExcelVO> queryAddressExcel3();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,5 @@
 | 
			
		|||
package net.lab1024.sa.admin.module.business.wms.base.address.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.idev.excel.ExcelWriter;
 | 
			
		||||
import cn.idev.excel.FastExcel;
 | 
			
		||||
import cn.idev.excel.write.metadata.WriteSheet;
 | 
			
		||||
import cn.idev.excel.write.metadata.style.WriteCellStyle;
 | 
			
		||||
import cn.idev.excel.write.metadata.style.WriteFont;
 | 
			
		||||
import cn.idev.excel.write.style.HorizontalCellStyleStrategy;
 | 
			
		||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
			
		||||
import com.google.common.collect.Maps;
 | 
			
		||||
| 
						 | 
				
			
			@ -24,6 +18,7 @@ import net.lab1024.sa.admin.module.business.wms.base.item.domain.vo.ItemsExcelVO
 | 
			
		|||
import net.lab1024.sa.admin.module.business.wms.excel.ExportTaskService;
 | 
			
		||||
import net.lab1024.sa.base.common.domain.PageResult;
 | 
			
		||||
import net.lab1024.sa.base.common.exception.BusinessException;
 | 
			
		||||
import net.lab1024.sa.base.common.util.SmartExcelUtil;
 | 
			
		||||
import net.lab1024.sa.base.common.util.SmartPageUtil;
 | 
			
		||||
import net.lab1024.sa.base.common.util.SmartResponseUtil;
 | 
			
		||||
import net.lab1024.sa.base.module.support.dict.constant.DictConst;
 | 
			
		||||
| 
						 | 
				
			
			@ -158,12 +153,16 @@ public class AddressQueryServiceImpl implements AddressQueryService {
 | 
			
		|||
        return excelData;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<AddressExcelVO> queryAddressExcel2() {
 | 
			
		||||
    public List<AddressExcelVO> queryAddressExcel2(String taskId) {
 | 
			
		||||
        System.out.println("开始读取地址数据...");
 | 
			
		||||
        long startTime = System.currentTimeMillis();
 | 
			
		||||
        List<AddressExcelVO> excelData = new ArrayList<>();
 | 
			
		||||
        //总条数
 | 
			
		||||
        long total = addressManager.count();
 | 
			
		||||
        long lastId = 0;  // 初始化为最小ID-1
 | 
			
		||||
        int pageSize = 2000;  // 根据测试调整
 | 
			
		||||
        //进度条
 | 
			
		||||
        long processed = 0;
 | 
			
		||||
        while (true) {
 | 
			
		||||
            List<AddressVO> batch = addressDao.listByCursor(lastId, pageSize);
 | 
			
		||||
            if (batch.isEmpty()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -179,7 +178,14 @@ public class AddressQueryServiceImpl implements AddressQueryService {
 | 
			
		|||
                excelData.add(excelVO);
 | 
			
		||||
            }
 | 
			
		||||
            lastId = batch.stream().mapToLong(AddressVO::getAddressId).max().orElse(0);
 | 
			
		||||
            processed += batch.size();
 | 
			
		||||
 | 
			
		||||
            //计算进度条
 | 
			
		||||
            long progress = processed * 100 / total;
 | 
			
		||||
            exportTaskService.updateProgress(taskId, progress);
 | 
			
		||||
            System.out.println("已处理:" + processed + "条数据,进度:" + progress + "%");
 | 
			
		||||
        }
 | 
			
		||||
        exportTaskService.updateProgress(taskId, 100);
 | 
			
		||||
        long endTime = System.currentTimeMillis();
 | 
			
		||||
        System.out.println("查询,共" + excelData.size() + "条数据,耗时:" + (endTime - startTime) + "ms");
 | 
			
		||||
        return excelData;
 | 
			
		||||
| 
						 | 
				
			
			@ -209,66 +215,31 @@ public class AddressQueryServiceImpl implements AddressQueryService {
 | 
			
		|||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void exportAddress(String taskId, HttpServletResponse response) {
 | 
			
		||||
        System.out.println("开始读取地址数据...");
 | 
			
		||||
        long startTime = System.currentTimeMillis();
 | 
			
		||||
        SmartResponseUtil.setDownloadFileHeader(response, "收货地址信息.xlsx", null);
 | 
			
		||||
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
 | 
			
		||||
        headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
 | 
			
		||||
 | 
			
		||||
        WriteFont headWriteFont = new WriteFont();
 | 
			
		||||
        headWriteFont.setFontName("宋体");
 | 
			
		||||
        headWriteFont.setColor(IndexedColors.WHITE.getIndex());
 | 
			
		||||
        headWriteCellStyle.setWriteFont(headWriteFont);
 | 
			
		||||
 | 
			
		||||
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
 | 
			
		||||
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
 | 
			
		||||
 | 
			
		||||
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
 | 
			
		||||
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
 | 
			
		||||
 | 
			
		||||
        try (ExcelWriter excelWriter = FastExcel.write(response.getOutputStream(), AddressExcelVO.class).registerWriteHandler(horizontalCellStyleStrategy).build()) {
 | 
			
		||||
            WriteSheet writeSheet = FastExcel.writerSheet("收货地址信息").build();
 | 
			
		||||
            //总条数
 | 
			
		||||
            long total = addressManager.count();
 | 
			
		||||
            // 初始化为最小ID-1
 | 
			
		||||
            long lastId = 0;
 | 
			
		||||
            // 根据测试调整
 | 
			
		||||
            int pageSize = 2000;
 | 
			
		||||
            //进度条
 | 
			
		||||
            long processed = 0;
 | 
			
		||||
            while (true) {
 | 
			
		||||
                List<AddressVO> batch = addressDao.listByCursor(lastId, pageSize);
 | 
			
		||||
                if (batch.isEmpty()) {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                List<AddressExcelVO> excelData = new ArrayList<>();
 | 
			
		||||
                for (AddressVO address : batch) {
 | 
			
		||||
                    AddressExcelVO excelVO = AddressExcelVO.builder()
 | 
			
		||||
    public void exportAddress(String taskId, HttpServletResponse response){
 | 
			
		||||
        try {
 | 
			
		||||
            SmartExcelUtil.batchExportExcel(
 | 
			
		||||
                    response,
 | 
			
		||||
                    "收货地址信息.xlsx",
 | 
			
		||||
                    "收货地址信息",
 | 
			
		||||
                    AddressExcelVO.class,
 | 
			
		||||
                    taskId,
 | 
			
		||||
                    addressManager.count(),
 | 
			
		||||
                    (lastId, pageSize) -> addressDao.listByCursor(lastId, pageSize),
 | 
			
		||||
                    address -> AddressExcelVO.builder()
 | 
			
		||||
                            .name(address.getName())
 | 
			
		||||
                            .person(address.getPerson())
 | 
			
		||||
                            .telephone(address.getTelephone())
 | 
			
		||||
                            .address(address.getAddress())
 | 
			
		||||
                            .build();
 | 
			
		||||
                    excelData.add(excelVO);
 | 
			
		||||
                }
 | 
			
		||||
                excelWriter.write(excelData, writeSheet);
 | 
			
		||||
                lastId = batch.stream().mapToLong(AddressVO::getAddressId).max().orElse(0);
 | 
			
		||||
                processed += batch.size();
 | 
			
		||||
 | 
			
		||||
                //计算进度条
 | 
			
		||||
                long progress = processed * 100 / total;
 | 
			
		||||
                exportTaskService.updateProgress(taskId, progress);
 | 
			
		||||
                System.out.println("已处理:" + processed + "条数据,进度:" + progress + "%");
 | 
			
		||||
            }
 | 
			
		||||
            System.out.println("导出耗时:" + (System.currentTimeMillis() - startTime) + "ms");
 | 
			
		||||
            excelWriter.finish();
 | 
			
		||||
                            .build(),
 | 
			
		||||
                    AddressVO::getAddressId,
 | 
			
		||||
                    2000,
 | 
			
		||||
                    (processed, progress) -> exportTaskService.updateProgress(taskId, progress)
 | 
			
		||||
            );
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            exportTaskService.updateProgress(taskId, -1);
 | 
			
		||||
        } finally {
 | 
			
		||||
            exportTaskService.updateProgress(taskId, 100);
 | 
			
		||||
            exportTaskService.cleanupTask(taskId);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new BusinessException("导出失败");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,10 +20,6 @@ public class ExportTaskService {
 | 
			
		|||
        return taskId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean  isTaskExists(String taskId) {
 | 
			
		||||
        return progressMap.containsKey(taskId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateProgress(String taskId, long progress) {
 | 
			
		||||
        progressMap.put(taskId, progress);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,18 @@
 | 
			
		|||
package net.lab1024.sa.base.common.util;
 | 
			
		||||
 | 
			
		||||
import cn.idev.excel.ExcelWriter;
 | 
			
		||||
import cn.idev.excel.FastExcel;
 | 
			
		||||
import cn.idev.excel.write.handler.SheetWriteHandler;
 | 
			
		||||
import cn.idev.excel.write.metadata.WriteSheet;
 | 
			
		||||
import cn.idev.excel.write.metadata.holder.WriteSheetHolder;
 | 
			
		||||
import cn.idev.excel.write.metadata.holder.WriteWorkbookHolder;
 | 
			
		||||
import cn.idev.excel.write.metadata.style.WriteCellStyle;
 | 
			
		||||
import cn.idev.excel.write.metadata.style.WriteFont;
 | 
			
		||||
import cn.idev.excel.write.style.HorizontalCellStyleStrategy;
 | 
			
		||||
import jakarta.servlet.http.HttpServletResponse;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import net.lab1024.sa.base.common.exception.BusinessException;
 | 
			
		||||
import org.apache.poi.openxml4j.opc.PackagePartName;
 | 
			
		||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
 | 
			
		||||
import org.apache.poi.openxml4j.opc.TargetMode;
 | 
			
		||||
| 
						 | 
				
			
			@ -26,9 +30,14 @@ import java.awt.image.BufferedImage;
 | 
			
		|||
import java.io.ByteArrayOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.function.BiConsumer;
 | 
			
		||||
import java.util.function.BiFunction;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
import java.util.function.ToLongFunction;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * excel 工具类
 | 
			
		||||
 *
 | 
			
		||||
 * @Author 1024创新实验室-主任:卓大
 | 
			
		||||
| 
						 | 
				
			
			@ -42,27 +51,75 @@ public final class SmartExcelUtil {
 | 
			
		|||
    /**
 | 
			
		||||
     * 通用单sheet导出
 | 
			
		||||
     */
 | 
			
		||||
    public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data) throws IOException {
 | 
			
		||||
    public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, Class head, Collection<?> data) throws IOException {
 | 
			
		||||
        // 设置下载消息头
 | 
			
		||||
        SmartResponseUtil.setDownloadFileHeader(response, fileName, null);
 | 
			
		||||
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
 | 
			
		||||
        headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
 | 
			
		||||
        WriteFont headWriteFont = new WriteFont();
 | 
			
		||||
        headWriteFont.setFontName("宋体");
 | 
			
		||||
        headWriteFont.setColor(IndexedColors.WHITE.getIndex());
 | 
			
		||||
        headWriteCellStyle.setWriteFont(headWriteFont);
 | 
			
		||||
        //样式
 | 
			
		||||
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = getHorizontalCellStyleStrategy();
 | 
			
		||||
        // 下载
 | 
			
		||||
        FastExcel.write(response.getOutputStream(), head)
 | 
			
		||||
                .autoCloseStream(Boolean.FALSE)
 | 
			
		||||
                .sheet(sheetName)
 | 
			
		||||
                .registerWriteHandler(horizontalCellStyleStrategy)
 | 
			
		||||
                .doWrite(data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
 | 
			
		||||
 | 
			
		||||
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
 | 
			
		||||
        headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
 | 
			
		||||
 | 
			
		||||
        WriteFont headWriteFont = new WriteFont();
 | 
			
		||||
        headWriteFont.setFontName("宋体");
 | 
			
		||||
        headWriteFont.setColor(IndexedColors.WHITE.getIndex());
 | 
			
		||||
        headWriteCellStyle.setWriteFont(headWriteFont);
 | 
			
		||||
 | 
			
		||||
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
 | 
			
		||||
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
 | 
			
		||||
 | 
			
		||||
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static <T, E> void batchExportExcel(HttpServletResponse response, String fileName, String sheetName, Class head, String taskId, long total, BiFunction<Long, Integer, List<T>> pageQueryFunction, Function<T, E> convertFunction, ToLongFunction<T> idExtractor, int pageSize, BiConsumer<String, Long> progressUpdater) throws IOException {
 | 
			
		||||
        SmartResponseUtil.setDownloadFileHeader(response, fileName, null);
 | 
			
		||||
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = getHorizontalCellStyleStrategy();
 | 
			
		||||
        try (ExcelWriter excelWriter = FastExcel.write(response.getOutputStream(), head).registerWriteHandler(horizontalCellStyleStrategy).build()) {
 | 
			
		||||
            WriteSheet writeSheet = FastExcel.writerSheet(sheetName).build();
 | 
			
		||||
            long lastId = 0;//最后一个id
 | 
			
		||||
            long processed = 0;//进度条
 | 
			
		||||
            while (true) {
 | 
			
		||||
                List<T> batch = pageQueryFunction.apply(lastId, pageSize);
 | 
			
		||||
                if (batch.isEmpty()) {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                List<E> excelData = batch.stream()
 | 
			
		||||
                        .map(convertFunction)
 | 
			
		||||
                        .collect(Collectors.toList());
 | 
			
		||||
                excelWriter.write(excelData, writeSheet);
 | 
			
		||||
 | 
			
		||||
                lastId = batch.stream()
 | 
			
		||||
                        .mapToLong(idExtractor)
 | 
			
		||||
                        .max()
 | 
			
		||||
                        .orElse(lastId);
 | 
			
		||||
 | 
			
		||||
                processed += batch.size();
 | 
			
		||||
 | 
			
		||||
                long progress = processed * 100 / total;
 | 
			
		||||
                progressUpdater.accept(taskId, progress);
 | 
			
		||||
            }
 | 
			
		||||
            excelWriter.finish();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new BusinessException("导出失败");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 通用单 sheet水印 导出
 | 
			
		||||
     */
 | 
			
		||||
    public static void exportExcelWithWatermark(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data, String watermarkString) throws IOException {
 | 
			
		||||
    public static void exportExcelWithWatermark(HttpServletResponse response, String fileName, String sheetName, Class head, Collection<?> data, String watermarkString) throws IOException {
 | 
			
		||||
        // 设置下载消息头
 | 
			
		||||
        SmartResponseUtil.setDownloadFileHeader(response, fileName, null);
 | 
			
		||||
        // 水印
 | 
			
		||||
| 
						 | 
				
			
			@ -191,7 +248,7 @@ public final class SmartExcelUtil {
 | 
			
		|||
        /**
 | 
			
		||||
         * 画笔颜色
 | 
			
		||||
         */
 | 
			
		||||
        private Color color = new Color(239,239,239);
 | 
			
		||||
        private Color color = new Color(239, 239, 239);
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * 字体样式
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue