jinlin
2025-03-01 86f02fee03614fef275c6e0c355d73318ca3025e
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.example.server.ExportExcel.method;
 
import cn.hutool.core.date.DateUtil;
import com.example.server.ExportExcel.dto.ExprotExcelDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
public class ExcelExport {
    public static void getHSSFWorkbook(String filePath,
                                       List<ExprotExcelDto> dtos) throws IOException {
        // 创建一个SXSSFWorkbook,对应一个Excel文件
        SXSSFWorkbook wb = new SXSSFWorkbook();
 
        // 创建第一个工作表并填充数据
 
        for (ExprotExcelDto dto : dtos) {
            String[][] content = new String[dto.getDataList().size()][dto.getColumnDto().size()];
            if (dto.getDataList() != null) {
                for (int i = 0; i < dto.getDataList().size(); i++) {
                    Object object = dto.getDataList().get(i);
 
                    Class c1azz = object.getClass();
                    for (int j = 0; j < dto.getColumnDto().size(); j++) {
                        Method method = null;
                        try {
                            String fieldName = dto.getColumnDto().get(j).getColumnName();
                            method = c1azz.getMethod("get" + StringUtils.capitalize(fieldName));
                            Object value = method.invoke(object);
                            String valueStr = "";
                            if (value == null) {
 
                            } else if (value instanceof Date) {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                valueStr = sdf.format(value);
                            } else {
                                valueStr = value.toString();
                            }
                            content[i][j] = valueStr;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            createSheetAndFillData(wb, dto, content);
        }
 
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            wb.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    private static void createSheetAndFillData(SXSSFWorkbook wb, ExprotExcelDto data, String[][] content) {
        SXSSFSheet sheet = wb.createSheet(data.getSheetName());
        //sheet.setDefaultRowHeightInPoints(20);
        CellStyle titleStyle = wb.createCellStyle();
 
        //4.设置字体样式
        Font titleFont = wb.createFont();
 
        titleFont.setFontName("宋体");
        //titleFont.setFontHeight((short) 10);
        //titleFont.setItalic(true);
        //titleFont.setStrikeout(true);
        //titleFont.setColor((short) 111);
        //titleFont.setTypeOffset((short) 5);
        //titleFont.setUnderline((byte) 0000);
        titleFont.setBold(true);
        //titleFont.setFontHeightInPoints((short) 12);
 
        titleStyle.setBorderBottom(BorderStyle.THICK);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderRight(BorderStyle.THIN);
        titleStyle.setBorderTop(BorderStyle.THIN);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setWrapText(true);
 
        titleStyle.setFont(titleFont);
 
        SXSSFCell cell = null;
        SXSSFRow titleRow = sheet.createRow(0);
        //titleRow.setHeightInPoints(40);
        // 循环输出标题行
        for (int i = 0; i < data.getColumnDto().size(); i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(data.getColumnDto().get(i).getColumnDesc());
            cell.setCellStyle(titleStyle);
            sheet.setColumnWidth(i, 5000);
        }
 
        CellStyle contentStyle = wb.createCellStyle();
 
        //4.设置字体样式
        Font contentFont = wb.createFont();
 
        contentFont.setFontName("宋体");
        contentFont.setBold(false);
 
        contentStyle.setBorderBottom(BorderStyle.THICK);
        contentStyle.setBorderLeft(BorderStyle.THIN);
        contentStyle.setBorderRight(BorderStyle.THIN);
        contentStyle.setBorderTop(BorderStyle.THIN);
        contentStyle.setAlignment(HorizontalAlignment.CENTER);
        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentStyle.setWrapText(true);
 
        contentStyle.setFont(contentFont);
 
        for (int j = 0; j < data.getColumnDto().size(); j++) {
            if (data.getColumnDto().get(j).getColumnWidth() != null && data.getColumnDto().get(j).getColumnWidth() != 0)
                sheet.setColumnWidth(j, data.getColumnDto().get(j).getColumnWidth() * 256);
        }
 
        for (int i = 0; i < content.length; i++) {
            SXSSFRow row = sheet.createRow(1 + i);
            for (int j = 0; j < content[i].length; j++) {
                cell = row.createCell(j);
                if (data.getColumnDto().get(j).getHorizontalAlignment() != null) {
                    CellStyle contentStyle2 = wb.createCellStyle();
                    contentStyle2.cloneStyleFrom(contentStyle);
                    contentStyle2.setAlignment(data.getColumnDto().get(j).getHorizontalAlignment());
                    cell.setCellStyle(contentStyle2);
                } else {
                    cell.setCellStyle(contentStyle);
                }
                cell.setCellValue(content[i][j]);
            }
        }
    }
}