刘帅
2026-09-22 bc68f4393cafe39c4fce614c8128936067ea25f7
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
package com.dsh.guns.modular.system.util;
 
 
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
 
public class ExcelUtil {
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
        // 第一步,创建一个webbook,对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        // 创建一个居中格式
        style.setAlignment((short) 2);
        HSSFCell cell = null;
        // 创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellType(1);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        if(values!=null){
            // 创建内容
            for (int i = 0; i < values.length; i++) {
                row = sheet.createRow(i + 1);
                for (int j = 0; j < values[i].length; j++) {
                    row.createCell(j).setCellValue(maskSensitiveValue(j < title.length ? title[j] : null, values[i][j]));
                }
            }
 
        }
        return wb;
    }
 
    /** Export files are an external disclosure channel and always remain masked. */
    private static String maskSensitiveValue(String columnName, String value) {
        if (value == null) return null;
        String column = columnName == null ? "" : columnName.toLowerCase();
        if (column.contains("身份证") || column.contains("idcard") || column.contains("identity")) {
            return value.length() <= 10 ? stars(value.length()) : value.substring(0, 6) + stars(value.length() - 10) + value.substring(value.length() - 4);
        }
        if (column.contains("电话") || column.contains("手机") || column.contains("phone") || column.contains("mobile")) {
            return value.length() <= 7 ? stars(value.length()) : value.substring(0, 3) + "****" + value.substring(7);
        }
        return value;
    }
 
    private static String stars(int size) {
        StringBuilder value = new StringBuilder();
        for (int i = 0; i < size; i++) value.append('*');
        return value.toString();
    }
 
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName));
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
}