乐于分享
好东西不私藏

提取Excel中的身份证网页版工具

提取Excel中的身份证网页版工具
用人工智能生成的一个网页版小工具,用于提取Excel文件中的身份证号码。
下面是代码。
<!DOCTYPE html><htmllang="zh-CN"><head>    <metacharset="UTF-8">    <metaname="viewport"content="width=device-width, initial-scale=1.0">    <title>Excel身份证号码提取工具</title>    <style>        * {            margin0;            padding0;            box-sizing: border-box;            font-family"Microsoft Yahei", sans-serif;        }        body {            max-width800px;            margin50px auto;            padding20px;            background-color#f5f5f5;        }        .container {            background: white;            padding30px;            border-radius8px;            box-shadow0 2px 10px rgba(0,0,0,0.1);        }        h1 {            text-align: center;            color#333;            margin-bottom30px;        }        .upload-area {            border2px dashed #ccc;            padding40px;            text-align: center;            border-radius6px;            margin-bottom20px;            cursor: pointer;            transition: all 0.3s;        }        .upload-area:hover {            border-color#409eff;        }        #file-input {            display: none;        }        .btn {            display: inline-block;            padding10px 20px;            background#409eff;            color: white;            border: none;            border-radius4px;            cursor: pointer;            font-size16px;            margin10px 5px;        }        .btn:disabled {            background#ccc;            cursor: not-allowed;        }        .result-area {            margin-top20px;            padding20px;            background#f8f9fa;            border-radius4px;            display: none;        }        .status {            text-align: center;            margin20px 0;            color#666;        }        table {            width100%;            border-collapse: collapse;            margin10px 0;        }        thtd {            border1px solid #ddd;            padding8px 12px;            text-align: center;        }        th {            background-color#e9ecef;        }    </style></head><body>    <divclass="container">        <h1>Excel身份证号码提取工具</h1>        <!-- 文件上传区域 -->        <divclass="upload-area"id="upload-area">            <p>点击或拖拽Excel文件到此处上传</p>            <inputtype="file"id="file-input"accept=".xlsx,.xls">        </div>        <!-- 操作按钮 -->        <divstyle="text-align: center;">            <buttonclass="btn"id="extract-btn"disabled>提取身份证号码</button>            <buttonclass="btn"id="download-btn"disabled>下载提取结果</button>        </div>        <!-- 状态提示 -->        <divclass="status"id="status"></div>        <!-- 结果展示区域 -->        <divclass="result-area"id="result-area">            <h3>提取结果预览</h3>            <tableid="result-table">                <thead>                    <tr>                        <th>序号</th>                        <th>身份证号码</th>                    </tr>                </thead>                <tbody>                </tbody>            </table>        </div>    </div>    <!-- 引入SheetJS库 -->    <scriptsrc="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>    <script>        // 获取DOM元素        const uploadArea = document.getElementById('upload-area');        const fileInput = document.getElementById('file-input');        const extractBtn = document.getElementById('extract-btn');        const downloadBtn = document.getElementById('download-btn');        const status = document.getElementById('status');        const resultArea = document.getElementById('result-area');        const resultTable = document.getElementById('result-table').querySelector('tbody');        // 全局变量存储文件和提取结果        let workbook = null;        let idCardList = [];        // 点击上传区域触发文件选择        uploadArea.addEventListener('click'() => {            fileInput.click();        });        // 拖拽上传        uploadArea.addEventListener('dragover'(e) => {            e.preventDefault();            uploadArea.style.borderColor = '#409eff';        });        uploadArea.addEventListener('dragleave'() => {            uploadArea.style.borderColor = '#ccc';        });        uploadArea.addEventListener('drop'(e) => {            e.preventDefault();            uploadArea.style.borderColor = '#ccc';            if (e.dataTransfer.files.length > 0) {                fileInput.files = e.dataTransfer.files;                handleFileSelect(e.dataTransfer.files[0]);            }        });        // 文件选择事件        fileInput.addEventListener('change'(e) => {            if (e.target.files.length > 0) {                handleFileSelect(e.target.files[0]);            }        });        // 处理文件选择        function handleFileSelect(file) {            // 验证文件类型            const fileTypes = [                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',                'application/vnd.ms-excel'            ];            const fileName = file.name.toLowerCase();            if (!fileTypes.includes(file.type) && !fileName.endsWith('.xlsx') && !fileName.endsWith('.xls')) {                status.textContent = '❌ 请上传有效的Excel文件(.xlsx 或 .xls)';                status.style.color = 'red';                return;            }            status.textContent = '✅ 文件已上传:' + file.name;            status.style.color = 'green';            // 读取Excel文件            const reader = new FileReader();            reader.onload = function(e) {                try {                    const data = new Uint8Array(e.target.result);                    workbook = XLSX.read(data, { type'array' });                    extractBtn.disabled = false;                } catch (error) {                    status.textContent = '❌ 文件读取失败:' + error.message;                    status.style.color = 'red';                }            };            reader.readAsArrayBuffer(file);        }        // 提取身份证号码        extractBtn.addEventListener('click'() => {            if (!workbook) return;            status.textContent = '🔍 正在提取身份证号码...';            status.style.color = '#666';            // 清空之前的结果            idCardList = [];            resultTable.innerHTML = '';            // 遍历所有工作表            const sheetNames = workbook.SheetNames;            for (const sheetName of sheetNames) {                const worksheet = workbook.Sheets[sheetName];                // 将工作表转换为JSON                const jsonData = XLSX.utils.sheet_to_json(worksheet, { header1 });                // 遍历所有单元格内容                for (let row = 0; row < jsonData.length; row++) {                    const rowData = jsonData[row];                    for (let col = 0; col < rowData.length; col++) {                        const cellValue = rowData[col];                        if (cellValue && typeof cellValue === 'string') {                            // 正则匹配18位身份证号码(考虑可能的空格)                            const idCardRegex = /\b\d{17}[\dXx]\b/g;                            const matches = cellValue.match(idCardRegex);                            if (matches) {                                matches.forEach(idCard => {                                    // 去重                                    if (!idCardList.includes(idCard.toUpperCase())) {                                        idCardList.push(idCard.toUpperCase());                                    }                                });                            }                        }                    }                }            }            // 显示结果            if (idCardList.length > 0) {                status.textContent = `✅ 提取完成!共找到 ${idCardList.length} 个身份证号码`;                status.style.color = 'green';                // 填充结果表格                idCardList.forEach((idCard, index) => {                    const tr = document.createElement('tr');                    tr.innerHTML = `                        <td>${index + 1}</td>                        <td>${idCard}</td>                    `;                    resultTable.appendChild(tr);                });                resultArea.style.display = 'block';                downloadBtn.disabled = false;            } else {                status.textContent = '❌ 未找到任何身份证号码';                status.style.color = 'orange';                resultArea.style.display = 'none';                downloadBtn.disabled = true;            }        });        // 下载提取结果        downloadBtn.addEventListener('click'() => {            if (idCardList.length === 0return;            // 创建新的工作簿            const newWorkbook = XLSX.utils.book_new();            // 创建工作表数据            const sheetData = [['序号''身份证号码']];            idCardList.forEach((idCard, index) => {                sheetData.push([index + 1, idCard]);            });            // 创建工作表            const worksheet = XLSX.utils.aoa_to_sheet(sheetData);            XLSX.utils.book_append_sheet(newWorkbook, worksheet, '身份证号码提取结果');            // 下载文件            XLSX.writeFile(newWorkbook, '身份证号码提取结果.xlsx');            status.textContent = '📥 下载成功!文件已保存到本地';        });    </script></body></html>

使用方法

    1. 将代码保存为.html文件;
    2. 用浏览器打开该文件;
    3. 上传 Excel 文件(支持.xlsx/.xls;)
    4. 点击 "提取身份证号码" 按钮;
    5. 查看提取结果,点击 "下载提取结果" 保存为新的 Excel 文件。