刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.limsController.export;
 
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.limsEntity.LimsWendingxingFenxiRowVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
 
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
 
public class LimsWendingxingFenxiWorkbookBuilder {
 
    private static final String[] HEADERS = {
            "序号", "品名", "稳定性考察编号", "批号", "录入日期", "考察周期",
            "周期单位", "项目名称", "数据", "上限", "下限", "单位"
    };
 
    public Workbook build(List<LimsWendingxingFenxiRowVo> rows,
                          List<DictionaryDataEntity> periodUnits) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("Worksheet");
        sheet.createFreezePane(0, 1);
        CellStyle headerStyle = createHeaderStyle(workbook);
        CellStyle dateStyle = workbook.createCellStyle();
        dateStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
        CellStyle decimalStyle = workbook.createCellStyle();
        decimalStyle.setDataFormat(workbook.createDataFormat().getFormat("0.################"));
 
        Row header = sheet.createRow(0);
        for (int column = 0; column < HEADERS.length; column++) {
            Cell cell = header.createCell(column);
            cell.setCellValue(HEADERS[column]);
            cell.setCellStyle(headerStyle);
        }
 
        Map<String, String> unitLabels = unitLabels(periodUnits);
        List<LimsWendingxingFenxiRowVo> safeRows = rows == null
                ? Collections.<LimsWendingxingFenxiRowVo>emptyList() : rows;
        for (int index = 0; index < safeRows.size(); index++) {
            LimsWendingxingFenxiRowVo source = safeRows.get(index);
            Row row = sheet.createRow(index + 1);
            row.createCell(0).setCellValue(index);
            setText(row, 1, source.getYangpinMingcheng());
            setText(row, 2, source.getJihuaBianhao());
            setText(row, 3, source.getPihao());
            if (source.getJieguoLuruRiqi() != null) {
                Cell dateCell = row.createCell(4);
                dateCell.setCellValue(source.getJieguoLuruRiqi());
                dateCell.setCellStyle(dateStyle);
            } else {
                setText(row, 4, null);
            }
            BigDecimal period = decimal(source.getZhouqi());
            if (period != null) {
                Cell periodCell = row.createCell(5);
                periodCell.setCellValue(period.doubleValue());
                periodCell.setCellStyle(decimalStyle);
            } else {
                setText(row, 5, source.getZhouqi());
            }
            setText(row, 6, periodUnit(period, source.getZhouqiDanwei(), unitLabels));
            setText(row, 7, source.getXiangmuMingcheng());
            setText(row, 8, source.getRawResult());
            setText(row, 9, source.getShangxian());
            setText(row, 10, source.getXiaxian());
            setText(row, 11, source.getUnit());
        }
 
        int[] widths = {8, 24, 28, 18, 16, 12, 12, 24, 18, 14, 14, 12};
        for (int column = 0; column < widths.length; column++) {
            sheet.setColumnWidth(column, widths[column] * 256);
        }
        return workbook;
    }
 
    private CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
 
    private Map<String, String> unitLabels(List<DictionaryDataEntity> units) {
        Map<String, String> labels = new HashMap<>();
        if (units == null) {
            return labels;
        }
        for (DictionaryDataEntity unit : units) {
            if (unit == null || blank(unit.getEnCode()) || blank(unit.getFullName())) {
                continue;
            }
            labels.put(unit.getEnCode().trim().toLowerCase(Locale.ROOT), unit.getFullName().trim());
        }
        return labels;
    }
 
    private String periodUnit(BigDecimal period, String code, Map<String, String> labels) {
        if (period != null && period.compareTo(BigDecimal.ZERO) == 0) {
            return "/";
        }
        if (blank(code)) {
            return "";
        }
        String normalized = code.trim();
        String label = labels.get(normalized.toLowerCase(Locale.ROOT));
        return label == null ? normalized : label;
    }
 
    private BigDecimal decimal(String value) {
        if (blank(value)) {
            return null;
        }
        try {
            return new BigDecimal(value.trim());
        } catch (NumberFormatException ignored) {
            return null;
        }
    }
 
    private void setText(Row row, int column, String value) {
        row.createCell(column).setCellValue(value == null ? "" : value);
    }
 
    private boolean blank(String value) {
        return value == null || value.trim().isEmpty();
    }
}