Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
767 blopes 1
package br.com.sl.shared;
2
 
3
import java.math.BigDecimal;
4
import java.time.LocalDate;
5
import java.time.LocalDateTime;
6
import java.time.LocalTime;
7
import java.time.ZoneId;
8
import java.time.format.DateTimeFormatter;
9
import java.util.Date;
10
 
11
import org.apache.poi.ss.usermodel.Cell;
12
import org.apache.poi.ss.usermodel.CellType;
13
import org.apache.poi.ss.usermodel.DateUtil;
14
 
15
public class ExcelDataUtils {
16
 
17
        public static LocalDate lerData(Cell cell) {
18
        if (cell == null) return null;
19
 
20
        try {
21
            int t = cell.getCellType();
22
 
23
            // 1) Se for fórmula RTD
24
            if (t == CellType.FORMULA.getCode()) {
25
                int ct = cell.getCachedFormulaResultType();
26
 
27
                if (ct == CellType.NUMERIC.getCode()) {
28
                    double dv = cell.getNumericCellValue();
29
                    java.util.Date d = DateUtil.getJavaDate(dv);
30
                    return d.toInstant()
31
                            .atZone(java.time.ZoneId.systemDefault())
32
                            .toLocalDate();
33
                }
34
 
35
                if (ct == CellType.STRING.getCode()) {
36
                    String text = cell.getStringCellValue().trim();
37
                    if (text.isEmpty()) return null;
38
 
39
                    DateTimeFormatter[] patterns = {
40
                            DateTimeFormatter.ofPattern("dd/MM/yyyy"),
41
                            DateTimeFormatter.ofPattern("dd-MM-yyyy"),
42
                            DateTimeFormatter.ofPattern("yyyy-MM-dd"),
43
                            DateTimeFormatter.ofPattern("yyyy/MM/dd")
44
                    };
45
 
46
                    for (DateTimeFormatter p : patterns) {
47
                        try {
48
                            return LocalDate.parse(text, p);
49
                        } catch (Exception ignored) {}
50
                    }
51
                }
52
            }
53
 
54
            // 2) Se for numérico formatado como data
55
            if (t == CellType.NUMERIC.getCode() && DateUtil.isCellDateFormatted(cell)) {
56
                java.util.Date d = cell.getDateCellValue();
57
                return d.toInstant()
58
                        .atZone(java.time.ZoneId.systemDefault())
59
                        .toLocalDate();
60
            }
61
 
62
            // 3) Se vier como texto
63
            String text = cell.toString().trim();
64
            if (text.isEmpty()) return null;
65
 
66
            DateTimeFormatter[] patterns = {
67
                    DateTimeFormatter.ofPattern("dd/MM/yyyy"),
68
                    DateTimeFormatter.ofPattern("dd-MM-yyyy"),
69
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"),
70
                    DateTimeFormatter.ofPattern("yyyy/MM/dd")
71
            };
72
 
73
            for (DateTimeFormatter p : patterns) {
74
                try {
75
                    return LocalDate.parse(text, p);
76
                } catch (Exception ignored) {}
77
            }
78
 
79
        } catch (Exception e) {
80
            System.out.println("[lerData] erro: " + e.getMessage());
81
        }
82
 
83
        return null;
84
    }
85
 
86
    public static LocalTime lerHora(Cell cell) {
87
        if (cell == null) return null;
88
 
89
        try {
90
            // 1) Fórmula RTD
91
            if (cell.getCellType() == CellType.FORMULA.getCode()) {
92
 
93
                if (cell.getCachedFormulaResultType() == CellType.NUMERIC.getCode()) {
94
                    java.util.Date d = DateUtil.getJavaDate(cell.getNumericCellValue());
95
                    return d.toInstant()
96
                            .atZone(java.time.ZoneId.systemDefault())
97
                            .toLocalTime();
98
                }
99
 
100
                if (cell.getCachedFormulaResultType() == CellType.STRING.getCode()) {
101
                    String text = cell.getStringCellValue().trim();
102
                    if (text.isEmpty()) return null;
103
 
104
                    DateTimeFormatter[] patterns = {
105
                            DateTimeFormatter.ofPattern("HH:mm:ss"),
106
                            DateTimeFormatter.ofPattern("H:mm:ss"),
107
                            DateTimeFormatter.ofPattern("HH:mm"),
108
                            DateTimeFormatter.ofPattern("H:mm")
109
                    };
110
 
111
                    for (DateTimeFormatter p : patterns) {
112
                        try { return LocalTime.parse(text, p); }
113
                        catch (Exception ignored) {}
114
                    }
115
                }
116
            }
117
 
118
            // 2) Numérico com formato de data/hora
119
            if (cell.getCellType() == CellType.NUMERIC.getCode() && DateUtil.isCellDateFormatted(cell)) {
120
                java.util.Date d = cell.getDateCellValue();
121
                return d.toInstant()
122
                        .atZone(java.time.ZoneId.systemDefault())
123
                        .toLocalTime();
124
            }
125
 
126
            // 3) Texto simples
127
            String text = cell.toString().trim();
128
            if (text.isEmpty()) return null;
129
 
130
            DateTimeFormatter[] patterns = {
131
                    DateTimeFormatter.ofPattern("HH:mm:ss"),
132
                    DateTimeFormatter.ofPattern("H:mm:ss"),
133
                    DateTimeFormatter.ofPattern("HH:mm"),
134
                    DateTimeFormatter.ofPattern("H:mm")
135
            };
136
 
137
            for (DateTimeFormatter p : patterns) {
138
                try { return LocalTime.parse(text, p); }
139
                catch (Exception ignored) {}
140
            }
141
 
142
        } catch (Exception e) {
143
            System.out.println("[lerHora] erro: " + e.getMessage());
144
        }
145
 
146
        return null;
147
    }
148
 
149
 
150
    private BigDecimal lerPreco(Cell cell) {
151
        if (cell == null) return null;
152
 
153
        try {
154
            // 1) Fórmula RTD
155
            if (cell.getCellType() == CellType.FORMULA.getCode()) {
156
 
157
                if (cell.getCachedFormulaResultType() == CellType.NUMERIC.getCode()) {
158
                    return BigDecimal.valueOf(cell.getNumericCellValue());
159
                }
160
 
161
                if (cell.getCachedFormulaResultType() == CellType.STRING.getCode()) {
162
                    String text = cell.getStringCellValue().trim();
163
                    if (text.isEmpty()) return null;
164
                    text = text.replace(".", "").replace(",", ".");
165
                    return new BigDecimal(text);
166
                }
167
            }
168
 
169
            // 2) Numérico simples
170
            if (cell.getCellType() == CellType.NUMERIC.getCode()) {
171
                return BigDecimal.valueOf(cell.getNumericCellValue());
172
            }
173
 
174
            // 3) Texto
175
            String text = cell.toString().trim();
176
            if (text.isEmpty()) return null;
177
 
178
            text = text.replace(".", "").replace(",", ".");
179
            return new BigDecimal(text);
180
 
181
        } catch (Exception e) {
182
            System.out.println("[lerPreco] erro: " + e.getMessage());
183
        }
184
 
185
        return null;
186
    }
187
 
188
    /**
189
     * Converte a célula de data/hora do Excel para LocalDateTime.
190
     */
191
    private LocalDateTime getLocalDateTime(Cell cell, DateTimeFormatter formatacaoDataTime) {
192
        if (cell == null) {
193
            return null;
194
        }
195
 
196
        // Caso seja data/hora numérica do Excel
197
        if (cell.getCellType() == CellType.NUMERIC.getCode() && DateUtil.isCellDateFormatted(cell)) {
198
            Date date = cell.getDateCellValue(); // disponível em todas as versões
199
            if (date == null) {
200
                return null;
201
            }
202
            return date.toInstant()
203
                       .atZone(ZoneId.systemDefault())
204
                       .toLocalDateTime();
205
        }
206
 
207
        // Caso venha como TEXT (por exemplo num CSV importado)
208
        if (cell.getCellType() == CellType.STRING.getCode()) {
209
            String text = cell.getStringCellValue();
210
            if (text == null || text.trim().isEmpty()) {
211
                return null;
212
            }
213
            text = text.trim();
214
            try {
215
                // Ajuste o pattern se seu Excel estiver em outro formato
216
                return LocalDateTime.parse(text, formatacaoDataTime);
217
            } catch (Exception e) {
218
                return null;
219
            }
220
        }
221
        return null;
222
    }
223
 
224
    /**
225
     * Converte a célula de data/hora do Excel para LocalDateTime.
226
     */
227
    public static LocalDateTime getLocalDateTime(String cell, DateTimeFormatter formatacaoDataTime) {
228
        if (cell == null || cell.trim().isEmpty()) {
229
            return null;
230
        }
231
        cell = cell.trim();
232
        try {
233
            return LocalDateTime.parse(cell, formatacaoDataTime);
234
        } catch (Exception e) {
235
            return null;
236
        }
237
    }
238
 
239
    public static boolean isNumeric(Cell cell) {
240
        if (cell == null) return false;
241
 
242
        if (cell.getCellType() == CellType.NUMERIC.getCode()) {
243
            return true;
244
        }
245
 
246
        if (cell.getCellType() == CellType.STRING.getCode()) {
247
            try {
248
                Double.parseDouble(cell.getStringCellValue().replace(",", "."));
249
                return true;
250
            } catch (NumberFormatException e) {
251
                return false;
252
            }
253
        }
254
 
255
        return false;
256
    }
257
 
258
}