| | |
| | | |
| | | 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文件 |
| | |
| | | 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(values[i][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=" + fileName); |
| | | 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"); |