aix
2024-08-13 128183e176aab3003a04b517b56162ba2ef780b0
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
package com.dji.sample.patches.utils;
 
import com.dji.sample.patches.model.entity.LotInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
 
public class ExcelUtil {
 
        public static ByteArrayInputStream exportLotInfoToExcel(List<LotInfo> lotInfos) throws IOException {
            // 创建 Excel 工作簿和工作表
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("LotInfo");
 
            // 写入表头
            Row headerRow = sheet.createRow(0);
            String[] headers = {
                    "ID", "Workspace ID", "BSM", "DKLX", "XZQDM", "XMC", "DKBH", "DKMC", "DKMJ", "SFBHZDK",
                    "XZB", "YZB", "BZ", "KZXX", "DKFW", "SJLX", "Is Plan", "Task Name", "Task ID", "Investigate",
                    "Is Push", "User Name", "Create Time", "Update Time"
            };
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }
 
            // 日期格式化
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
            // 写入数据
            int rowCount = 1;
            for (LotInfo lotInfo : lotInfos) {
                Row row = sheet.createRow(rowCount++);
                row.createCell(0).setCellValue(lotInfo.getId());
                row.createCell(1).setCellValue(lotInfo.getWorkspaceId());
                row.createCell(2).setCellValue(lotInfo.getBsm());
                row.createCell(3).setCellValue(lotInfo.getDklx());
                row.createCell(4).setCellValue(lotInfo.getXzqdm());
                row.createCell(5).setCellValue(lotInfo.getXmc());
                row.createCell(6).setCellValue(lotInfo.getDkbh());
                row.createCell(7).setCellValue(lotInfo.getDkmc());
                row.createCell(8).setCellValue(lotInfo.getDkmj() != null ? lotInfo.getDkmj() : 0.0);
                row.createCell(9).setCellValue(lotInfo.getSfbhzdk());
                row.createCell(10).setCellValue(lotInfo.getXzb() != null ? lotInfo.getXzb() : 0.0);
                row.createCell(11).setCellValue(lotInfo.getYzb() != null ? lotInfo.getYzb() : 0.0);
                row.createCell(12).setCellValue(lotInfo.getBz());
                row.createCell(13).setCellValue(lotInfo.getKzxx());
                row.createCell(14).setCellValue(lotInfo.getDkfw());
                row.createCell(15).setCellValue(lotInfo.getSjlx());
                row.createCell(16).setCellValue(lotInfo.getIsPlan());
                row.createCell(17).setCellValue(lotInfo.getTaskName());
                row.createCell(18).setCellValue(lotInfo.getTaskId());
                row.createCell(19).setCellValue(lotInfo.getInvestigate());
                row.createCell(20).setCellValue(lotInfo.getIsPush());
                row.createCell(21).setCellValue(lotInfo.getUserName());
                row.createCell(22).setCellValue(lotInfo.getCreateTime() != null ? dateFormat.format(lotInfo.getCreateTime()) : "");
                row.createCell(23).setCellValue(lotInfo.getUpdateTime() != null ? dateFormat.format(lotInfo.getUpdateTime()) : "");
            }
 
            // 写入到 ByteArrayOutputStream
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
 
            return new ByteArrayInputStream(outputStream.toByteArray());
        }
 
}