mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.panzhihua.common.utlis;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.panzhihua.common.excel.CustomSheetWriteHandler;
import com.panzhihua.common.model.dtos.FtpConfig;
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
 
/**
 * @author lyq
 * excel工具类
 */
@Slf4j
public class ExcelUtils {
 
    public static String adminExport(FtpConfig config, String name, List<List<String>> headList
            , List<List<Object>> datalist, String sheetName, Object obj) {
        String ftpUrl = "/mnt/data/web/excel/";
        try {
            SFTPUtil sftp = new SFTPUtil(config.getUserName(), config.getPassword(), config.getHost(), config.getPort());
            sftp.login();
            boolean existDir = sftp.isExistDir(ftpUrl + name);
            if (!existDir) {
                String property = System.getProperty("user.dir");
                String fileName = property + File.separator + name;
                ExcelWriter excelWriter = null;
                InputStream inputStream = null;
                try {
                    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
                    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
 
                    contentWriteCellStyle.setWrapped(true);
                    HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                            new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
                    LongestMatchColumnWidthStyleStrategy longestMatchColumnWidthStyleStrategy = new LongestMatchColumnWidthStyleStrategy();
 
                    excelWriter = EasyExcel.write(fileName, Object.class)
                            .registerWriteHandler(horizontalCellStyleStrategy)
                            .registerWriteHandler(longestMatchColumnWidthStyleStrategy)
                            .registerWriteHandler(new CustomSheetWriteHandler()).build();
                    WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).head(headList).build();
                    excelWriter.write(datalist, writeSheet);
                    excelWriter.finish();
                    File file = new File(fileName);
                    inputStream = new FileInputStream(file);
                    sftp.uploadMore(ftpUrl, name, inputStream);
                    sftp.logout();
                    inputStream.close();
                    String absolutePath = file.getAbsolutePath();
                    boolean delete = file.delete();
                    log.info("删除excel【{}】结果【{}】", absolutePath, delete);
                } finally {
                    // 千万别忘记finish 会帮忙关闭流
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (excelWriter != null) {
                        excelWriter.finish();
                    }
                }
                return config.getExcelUrl() + name;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件传输失败【{}】", e.getMessage());
            return null;
        }
        return null;
    }
}