a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
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
package com.basic.security.utils;
 
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Visit;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
public class ExcelWriteUtil {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    public static void write(File file, List<ModelAdapter> visitList) {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("访问记录");
            CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
            Font font = sheet.getWorkbook().createFont();
//            font.setBold(true);
            font.setFontHeightInPoints((short) 16);
            cellStyle.setFont(font);
            cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            cellStyle.setFont(font);
            cellStyle.setWrapText(false);
            CellStyle cellStyle2 = sheet.getWorkbook().createCellStyle();
            Font font2 = sheet.getWorkbook().createFont();
            font2.setBold(true);
            font2.setFontHeightInPoints((short) 16);
            cellStyle2.setFont(font2);
            cellStyle2.setAlignment(CellStyle.ALIGN_CENTER);
            cellStyle2.setFont(font);
            cellStyle2.setWrapText(false);
            String heads[] = {"进出时间", "设备名称", "姓名", "身份证号", "手机号", "身份",
                    "被访人姓名", "被访人身份证号", "被访人手机号", "被访人身份", "被访人组织机构", "识别结果"};
            {
                sheet.setColumnWidth(0, 256 * 30);//进出时间
                sheet.setColumnWidth(1, 256 * 15);//设备名称
                sheet.setColumnWidth(2, 256 * 15);//姓名
                sheet.setColumnWidth(3, 256 * 30);//身份证号
                sheet.setColumnWidth(4, 256 * 20);//手机号
                sheet.setColumnWidth(5, 256 * 30);//身份
                sheet.setColumnWidth(6, 256 * 15);//被访人姓名
                sheet.setColumnWidth(7, 256 * 30);//被访人身份证号
                sheet.setColumnWidth(8, 256 * 20);//被访人手机号
                sheet.setColumnWidth(9, 256 * 30);//被访人身份
                sheet.setColumnWidth(10, 256 * 20);//被访人组织机构
                sheet.setColumnWidth(11, 256 * 15);//识别结果
            }
            int rowCount = 0;
            {
                Row row = sheet.createRow(rowCount);
                int columnCount = 0;
                for (Object field : heads) {
                    Cell cell = row.createCell(columnCount);
                    cell.setCellStyle(cellStyle2);
                    if (field instanceof String) {
                        cell.setCellValue((String) field);
                    } else if (field instanceof Integer) {
                        cell.setCellValue((Integer) field);
                    }
                    columnCount++;
                }
                rowCount++;
            }
            //String heads[] = {"进出时间","设备名称","姓名","身份证号","手机号","身份",
            //                    "被访人姓名","被访人身份证号","被访人手机号","被访人身份","被访人组织机构","识别结果"};
            for (ModelAdapter visit : visitList) {
                Row row = sheet.createRow(rowCount);
                Cell cell = null;
                int columnCount = 0;
                String visitTime = "";
                try {
                    visitTime = sdf.format(new Date(Long.parseLong(visit.getString(Visit.visit_time))));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                cell = row.createCell(columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visitTime);
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.device_name));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.person_name));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.person_id_number));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.person_phone));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.person_type));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.target_person_name));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.target_id_number));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.target_phone));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.target_identity));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(visit.getString(Visit.target_org_name));
                cell = row.createCell(++columnCount);
                cell.setCellStyle(cellStyle);
                cell.setCellValue("1".equals(visit.getString(Visit.verify_result)) ? "通行成功" : "通行失败");
                rowCount++;
            }
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                workbook.write(outputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}