sgj
2026-02-26 6c955bced91e431cb6dbd255257d3dafd1dc6cf2
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.fzzy.igds.service;
 
import com.fzzy.igds.constant.FoodVariety;
import com.fzzy.igds.domain.InoutRecord;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Description
 * @Author CZT
 * @Date 2026/01/19 18:52
 */
@Service
public class ExportService {
 
    private static final String XLS = "xls";
    private static final String XLSX = "xlsx";
 
    @Resource
    private FileService fileService;
 
 
    /**
     * 读取Excel文件内容
     *
     * @param
     * @return
     */
    public List<InoutRecord> readExcel(String fileName) {
 
        //获取文件后缀名并判断
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
 
        //获取保存路径
        String path = fileService.getFileSavePath("TEMP");
        path = path + fileName;
 
        Workbook workbook = null;
        FileInputStream inputStream = null;
 
        try {
            // 获取Excel文件
            File excelFile = new File(path);
            if (!excelFile.exists()) {
                return null;
            }
            // 获取Excel工作簿
            inputStream = new FileInputStream(excelFile);
            workbook = getWorkbook(inputStream, fileType);
 
            // 读取excel中的数据
            return parseExcel(workbook);
        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 解析Excel数据
     *
     * @param workbook
     * @return
     */
    private static List<InoutRecord> parseExcel(Workbook workbook) throws Exception{
        List<InoutRecord> resultDataList = new ArrayList<>();
 
        // 解析sheet
        for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
            Sheet sheet = workbook.getSheetAt(sheetNum);
 
            // 校验sheet是否合法
            if (sheet == null) {
                continue;
            }
 
            // 获取第一行数据,不用处理
            int firstRowNum = sheet.getFirstRowNum();
 
            // 解析每一行的数据,构造数据对象
            int rowStart = firstRowNum + 2;
            int rowEnd = sheet.getPhysicalNumberOfRows();
 
            //定义变量,用于判断数据是否解析完
            boolean flag = false;
 
            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
                Row row = sheet.getRow(rowNum);
 
                if (null == row) {
                    //此行为空,则之后不再解析,判定为解析完成
                    flag = true;
                    break;
                }
 
                InoutRecord resultData = convertRowToData(row);
                resultDataList.add(resultData);
            }
            if(flag){
                break;
            }
        }
        return resultDataList;
    }
 
    /**
     * 根据文件后缀名类型获取对应的工作簿对象
     * @param inputStream 读取文件的输入流
     * @param fileType 文件后缀名类型(xls或xlsx)
     * @return 包含文件数据的工作簿对象
     * @throws IOException
     */
    public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException {
        Workbook workbook = null;
        if (fileType.equalsIgnoreCase(XLS)) {
            workbook = new HSSFWorkbook(inputStream);
        } else if (fileType.equalsIgnoreCase(XLSX)) {
            workbook = new XSSFWorkbook(inputStream);
        }
        return workbook;
    }
 
    /**
     * 提取每一行中需要的数据,构造成为一个结果数据对象
     *
     * @param row
     * @return
     */
    private static InoutRecord convertRowToData(Row row) throws Exception {
        InoutRecord resultData = new InoutRecord();
        
        Cell cell;
        int cellNum = 0;
 
        //第一列序号,不做处理
        cell = row.getCell(cellNum++);
 
        //日期
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setCustomerName("");
        }else {
            cell.setCellType(CellType.STRING);
            String time = cell.getStringCellValue().trim().replaceAll(" ","");
            if(StringUtils.isNotEmpty(time)){
                resultData.setRegisterTime(DateUtils.parseDate(time,"yyyy-MM-dd"));
            }
        }
 
        //类型
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setType("");
        }else {
            cell.setCellType(CellType.STRING);
            String type = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(type)){
                resultData.setType(type);
            }
        }
 
        //车牌号
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPlateNum("");
        }else {
            cell.setCellType(CellType.STRING);
            String plateNum = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(plateNum)){
                resultData.setPlateNum(plateNum);
            }
        }
 
        //承运人
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setUserName("");
        }else {
            cell.setCellType(CellType.STRING);
            String userName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(userName)){
                resultData.setUserName(userName);
            }
        }
 
        //往来单位
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setCustomerName("");
        }else {
            cell.setCellType(CellType.STRING);
            String customerName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(customerName)){
                resultData.setCustomerName(customerName);
            }
        }
 
        //通知单编码
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setNoticeId("");
        }else {
            cell.setCellType(CellType.STRING);
            String noticeId = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(noticeId)){
                resultData.setNoticeId(noticeId);
            }
        }
 
        //装卸仓库
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setDepotId("");
        }else {
            cell.setCellType(CellType.STRING);
            String depotName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(depotName)){
                resultData.setDepotId(depotName);
            }
        }
 
        //粮食品种
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFoodVariety("");
        }else {
            cell.setCellType(CellType.STRING);
            String foodVariety = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(foodVariety)){
                resultData.setFoodVariety(FoodVariety.getCode(foodVariety.trim()));
            }
        }
 
        //粮食年份
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFoodYear("");
        }else {
            cell.setCellType(CellType.STRING);
            String year = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(year)){
                resultData.setFoodYear(year);
            }
        }
 
        //满车重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFullWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String fullWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(fullWeight)){
                resultData.setFullWeight(Double.valueOf(fullWeight));
            }
        }
 
        //空车重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setEmptyWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String emptyWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(emptyWeight)){
                resultData.setEmptyWeight(Double.valueOf(emptyWeight));
            }
        }
 
        //扣重
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setDeOther(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String deOther = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(deOther)){
                resultData.setDeOther(Double.valueOf(deOther));
            }
        }
 
        //结算重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setSettleWeight(0.0);
            resultData.setRecordWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String recordWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(recordWeight)){
                resultData.setSettleWeight(Double.valueOf(recordWeight));
                resultData.setRecordWeight(Double.valueOf(recordWeight));
            }
        }
 
        //单价
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPrice(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String price = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(price)){
                resultData.setPrice(Double.valueOf(price));
            }
        }
 
        //水分
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPerWet(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String perWet = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(perWet)){
                resultData.setPerWet(Double.valueOf(perWet));
            }
        }
 
        //杂质
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPerImpurity(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String perImpurity = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(perImpurity)){
                resultData.setPerImpurity(Double.valueOf(perImpurity));
            }
        }
 
        //备注
        cell = row.getCell(cellNum++);
        if(null ==  cell || cell.getCellType() == CellType.BLANK){
            resultData.setRemarks("");
        }else {
            cell.setCellType(CellType.STRING);
            String remarks = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(remarks)){
                resultData.setRemarks(remarks);
            }
        }
 
        if(null != resultData.getFullWeight() && null != resultData.getEmptyWeight()){
            resultData.setNetWeight(resultData.getFullWeight() - resultData.getEmptyWeight());
        }
        return resultData;
    }
 
}