| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Excel 自动填表工具</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
- background: #f5f7fa;
- color: #333;
- padding: 20px;
- }
- .container {
- max-width: 1100px;
- margin: 0 auto;
- background: #fff;
- border-radius: 12px;
- box-shadow: 0 2px 12px rgba(0,0,0,0.08);
- padding: 30px;
- }
- h1 {
- font-size: 24px;
- color: #1a1a2e;
- margin-bottom: 8px;
- display: flex;
- align-items: center;
- gap: 10px;
- }
- .subtitle {
- color: #888;
- font-size: 14px;
- margin-bottom: 24px;
- border-bottom: 1px solid #eee;
- padding-bottom: 16px;
- }
- .form-group {
- margin-bottom: 18px;
- }
- .form-group label {
- display: block;
- font-weight: 600;
- font-size: 14px;
- margin-bottom: 5px;
- color: #444;
- }
- .form-group .hint {
- font-size: 12px;
- color: #999;
- font-weight: 400;
- }
- .form-row {
- display: flex;
- gap: 20px;
- flex-wrap: wrap;
- }
- .form-row .form-group {
- flex: 1;
- min-width: 180px;
- }
- select, input[type="file"], input[type="number"] {
- width: 100%;
- padding: 10px 12px;
- border: 1px solid #d1d5db;
- border-radius: 8px;
- font-size: 14px;
- background: #fafbfc;
- transition: border-color 0.2s;
- }
- select:focus, input:focus {
- border-color: #4f8cf7;
- outline: none;
- box-shadow: 0 0 0 3px rgba(79, 140, 247, 0.15);
- }
- .file-upload-wrapper {
- position: relative;
- display: flex;
- align-items: center;
- gap: 12px;
- }
- .file-upload-wrapper input[type="file"] {
- padding: 8px;
- }
- .file-name {
- font-size: 13px;
- color: #666;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- max-width: 200px;
- }
- .btn {
- padding: 10px 28px;
- border: none;
- border-radius: 8px;
- font-size: 15px;
- font-weight: 600;
- cursor: pointer;
- transition: all 0.2s;
- display: inline-flex;
- align-items: center;
- gap: 8px;
- }
- .btn-primary {
- background: #4f8cf7;
- color: #fff;
- }
- .btn-primary:hover {
- background: #3a7ae8;
- transform: translateY(-1px);
- }
- .btn-primary:disabled {
- background: #b0c8f5;
- cursor: not-allowed;
- transform: none;
- }
- .btn-success {
- background: #34c759;
- color: #fff;
- }
- .btn-success:hover {
- background: #2db84e;
- }
- .btn-success:disabled {
- background: #9de0b2;
- cursor: not-allowed;
- }
- .btn-secondary {
- background: #e8ecf1;
- color: #444;
- }
- .btn-secondary:hover {
- background: #d5dbe3;
- }
- .btn-danger {
- background: #ef4444;
- color: #fff;
- }
- .btn-danger:hover {
- background: #dc2626;
- }
- .actions {
- display: flex;
- gap: 12px;
- flex-wrap: wrap;
- margin: 20px 0 16px;
- padding: 16px 0;
- border-top: 1px solid #eee;
- border-bottom: 1px solid #eee;
- }
- .log-area {
- background: #1a1a2e;
- color: #e4e4e4;
- border-radius: 8px;
- padding: 16px 20px;
- font-family: 'Courier New', monospace;
- font-size: 12px;
- max-height: 320px;
- overflow-y: auto;
- white-space: pre-wrap;
- word-break: break-all;
- line-height: 1.6;
- margin-top: 16px;
- }
- .log-area .log-success {
- color: #4ade80;
- }
- .log-area .log-error {
- color: #f87171;
- }
- .log-area .log-warn {
- color: #fbbf24;
- }
- .log-area .log-info {
- color: #60a5fa;
- }
- .preview-table-wrapper {
- overflow-x: auto;
- margin-top: 16px;
- border-radius: 8px;
- border: 1px solid #e5e7eb;
- }
- .preview-table {
- width: 100%;
- border-collapse: collapse;
- font-size: 13px;
- min-width: 600px;
- }
- .preview-table th {
- background: #f3f4f6;
- padding: 10px 12px;
- text-align: left;
- font-weight: 600;
- border-bottom: 2px solid #e5e7eb;
- white-space: nowrap;
- position: sticky;
- top: 0;
- z-index: 1;
- }
- .preview-table td {
- padding: 8px 12px;
- border-bottom: 1px solid #e5e7eb;
- max-width: 200px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .preview-table tr:hover td {
- background: #f9fafb;
- }
- .status-badge {
- display: inline-block;
- padding: 4px 14px;
- border-radius: 20px;
- font-size: 13px;
- font-weight: 500;
- }
- .status-badge.idle {
- background: #e5e7eb;
- color: #6b7280;
- }
- .status-badge.loading {
- background: #fef3c7;
- color: #d97706;
- }
- .status-badge.success {
- background: #d1fae5;
- color: #065f46;
- }
- .status-badge.error {
- background: #fee2e2;
- color: #991b1b;
- }
- .flex {
- display: flex;
- align-items: center;
- gap: 12px;
- }
- .flex-between {
- display: flex;
- justify-content: space-between;
- align-items: center;
- flex-wrap: wrap;
- gap: 10px;
- }
- .mt-8 { margin-top: 8px; }
- .mt-12 { margin-top: 12px; }
- .text-center { text-align: center; }
- .text-sm { font-size: 13px; }
- .text-muted { color: #888; }
- .hidden { display: none !important; }
-
- .download-section {
- background: #ecfdf5;
- border: 1px solid #6ee7b7;
- border-radius: 8px;
- padding: 16px 20px;
- display: none;
- align-items: center;
- justify-content: space-between;
- flex-wrap: wrap;
- gap: 12px;
- margin-top: 12px;
- }
- .download-section.show {
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>📊 Excel 自动填表工具</h1>
- <div class="subtitle">
- 选择模板 → 上传来源数据 → 预览 → 生成并下载
- <span class="status-badge idle" id="statusBadge">就绪</span>
- </div>
- <!-- 配置区域 -->
- <div class="form-row">
- <div class="form-group">
- <label>📁 选择模板 <span class="hint">(模板文件需放在服务器 templates_excel/ 目录)</span></label>
- <select id="templateSelect">
- <option value="">-- 请选择模板 --</option>
- {% for t in templates %}
- <option value="{{ t }}">{{ t }}</option>
- {% endfor %}
- </select>
- </div>
- <div class="form-group">
- <label>📄 来源数据 Excel</label>
- <div class="file-upload-wrapper">
- <input type="file" id="sourceFile" accept=".xlsx,.xls">
- <span class="file-name" id="fileName">未选择文件</span>
- </div>
- </div>
- </div>
- <div class="form-row">
- <div class="form-group">
- <label>来源表头行号 <span class="hint">(第2行是字段名)</span></label>
- <input type="number" id="sourceHeader" value="2" min="1">
- </div>
- <div class="form-group">
- <label>模板表头行号 <span class="hint">(第4行是字段名)</span></label>
- <input type="number" id="templateHeader" value="4" min="1">
- </div>
- </div>
- <!-- 操作按钮 -->
- <div class="actions">
- <button class="btn btn-primary" id="previewBtn">🔍 预览数据</button>
- <button class="btn btn-success" id="generateBtn" disabled>🚀 生成并下载</button>
- <button class="btn btn-secondary" id="clearLogBtn">🗑️ 清空日志</button>
- </div>
- <!-- 状态信息 -->
- <div class="flex-between">
- <span id="statusText" class="text-muted text-sm">请选择模板和来源文件</span>
- <span id="rowCount" class="text-muted text-sm"></span>
- </div>
- <!-- 预览表格 -->
- <div id="previewContainer" class="hidden">
- <div class="flex-between mt-8">
- <span class="text-sm" style="font-weight:600;">📋 数据预览 (前10行)</span>
- </div>
- <div class="preview-table-wrapper" id="previewWrapper">
- <table class="preview-table" id="previewTable">
- <thead id="previewHead"></thead>
- <tbody id="previewBody"></tbody>
- </table>
- </div>
- </div>
- <!-- 下载区域 -->
- <div class="download-section" id="downloadSection">
- <span style="font-weight:600;color:#065f46;">✅ 生成完成!</span>
- <span id="downloadInfo"></span>
- <a href="#" id="downloadLink" class="btn btn-success" style="text-decoration:none;">📥 下载文件</a>
- </div>
- <!-- 日志 -->
- <div class="log-area" id="logArea">
- <span class="log-info">💡 系统就绪,等待操作...</span>
- </div>
- </div>
- <script>
- // DOM 元素
- const templateSelect = document.getElementById('templateSelect');
- const sourceFile = document.getElementById('sourceFile');
- const fileName = document.getElementById('fileName');
- const sourceHeader = document.getElementById('sourceHeader');
- const templateHeader = document.getElementById('templateHeader');
- const previewBtn = document.getElementById('previewBtn');
- const generateBtn = document.getElementById('generateBtn');
- const clearLogBtn = document.getElementById('clearLogBtn');
- const logArea = document.getElementById('logArea');
- const statusText = document.getElementById('statusText');
- const statusBadge = document.getElementById('statusBadge');
- const rowCount = document.getElementById('rowCount');
- const previewContainer = document.getElementById('previewContainer');
- const previewHead = document.getElementById('previewHead');
- const previewBody = document.getElementById('previewBody');
- const downloadSection = document.getElementById('downloadSection');
- const downloadLink = document.getElementById('downloadLink');
- const downloadInfo = document.getElementById('downloadInfo');
- let sourcePath = null;
- // 日志函数
- function log(msg, type = 'info') {
- const colors = {
- info: 'log-info',
- success: 'log-success',
- error: 'log-error',
- warn: 'log-warn'
- };
- const prefix = {
- info: 'ℹ️',
- success: '✅',
- error: '❌',
- warn: '⚠️'
- };
- const timestamp = new Date().toLocaleTimeString();
- const line = document.createElement('div');
- line.innerHTML = `<span style="color:#888;">[${timestamp}]</span> <span class="${colors[type] || 'log-info'}">${prefix[type] || 'ℹ️'} ${msg}</span>`;
- logArea.appendChild(line);
- logArea.scrollTop = logArea.scrollHeight;
- }
- function setStatus(text, type = 'idle') {
- statusText.textContent = text;
- statusBadge.textContent = {
- idle: '就绪',
- loading: '处理中...',
- success: '成功',
- error: '失败'
- }[type] || '就绪';
- statusBadge.className = `status-badge ${type}`;
- }
- function clearLog() {
- logArea.innerHTML = '';
- log('日志已清空', 'info');
- }
- // 文件选择
- sourceFile.addEventListener('change', function() {
- if (this.files.length > 0) {
- fileName.textContent = this.files[0].name;
- } else {
- fileName.textContent = '未选择文件';
- }
- checkReady();
- });
- templateSelect.addEventListener('change', checkReady);
- function checkReady() {
- const hasTemplate = templateSelect.value !== '';
- const hasFile = sourceFile.files.length > 0;
- previewBtn.disabled = !(hasTemplate && hasFile);
- generateBtn.disabled = true;
- if (hasTemplate && hasFile) {
- setStatus('已选择模板和来源文件,点击预览', 'idle');
- } else {
- setStatus('请选择模板和来源文件', 'idle');
- }
- }
- // 预览
- previewBtn.addEventListener('click', async function() {
- const template = templateSelect.value;
- const file = sourceFile.files[0];
- const sHeader = parseInt(sourceHeader.value) || 2;
- if (!template || !file) {
- log('请选择模板和来源文件', 'warn');
- return;
- }
- this.disabled = true;
- setStatus('正在预览...', 'loading');
- log(`📖 预览来源数据: ${file.name}`, 'info');
- const formData = new FormData();
- formData.append('source_file', file);
- formData.append('template_name', template);
- formData.append('source_header', sHeader);
- try {
- const response = await fetch('/preview', {
- method: 'POST',
- body: formData
- });
- const result = await response.json();
- if (result.error) {
- log(`❌ 预览失败: ${result.error}`, 'error');
- setStatus('预览失败', 'error');
- previewContainer.classList.add('hidden');
- return;
- }
- sourcePath = result.source_path;
- log(`✅ 成功读取 ${result.total_rows} 行数据`, 'success');
- setStatus(`预览成功,共 ${result.total_rows} 行`, 'success');
- rowCount.textContent = `共 ${result.total_rows} 行`;
- // 渲染表格
- renderPreview(result.columns, result.data);
- previewContainer.classList.remove('hidden');
- // 启用生成按钮
- generateBtn.disabled = false;
- } catch (err) {
- log(`❌ 请求失败: ${err.message}`, 'error');
- setStatus('预览失败', 'error');
- } finally {
- this.disabled = false;
- }
- });
- function renderPreview(columns, data) {
- // 表头
- previewHead.innerHTML = `<tr>${columns.map(c => `<th>${escapeHtml(c)}</th>`).join('')}</tr>`;
-
- // 数据行
- previewBody.innerHTML = data.map(row => {
- return `<tr>${columns.map(c => `<td title="${escapeHtml(String(row[c] || ''))}">${escapeHtml(String(row[c] || ''))}</td>`).join('')}</tr>`;
- }).join('');
- }
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- // 生成
- generateBtn.addEventListener('click', async function() {
- if (!sourcePath) {
- log('请先预览数据', 'warn');
- return;
- }
- const template = templateSelect.value;
- const sHeader = parseInt(sourceHeader.value) || 2;
- const tHeader = parseInt(templateHeader.value) || 4;
- this.disabled = true;
- previewBtn.disabled = true;
- setStatus('正在生成...', 'loading');
- log('🚀 开始生成并追加到模板...', 'info');
- const formData = new FormData();
- formData.append('source_path', sourcePath);
- formData.append('template_name', template);
- formData.append('source_header', sHeader);
- formData.append('header_row', tHeader);
- try {
- const response = await fetch('/generate', {
- method: 'POST',
- body: formData
- });
- const result = await response.json();
- if (result.error) {
- log(`❌ 生成失败: ${result.error}`, 'error');
- setStatus('生成失败', 'error');
- return;
- }
- log(`✅ ${result.message}`, 'success');
- setStatus(`生成成功,追加 ${result.filled_count} 行`, 'success');
- // 显示下载
- downloadSection.classList.add('show');
- downloadLink.href = result.download_url;
- downloadInfo.textContent = `已追加 ${result.filled_count} 行数据`;
- } catch (err) {
- log(`❌ 请求失败: ${err.message}`, 'error');
- setStatus('生成失败', 'error');
- } finally {
- this.disabled = false;
- previewBtn.disabled = false;
- }
- });
- // 清空日志
- clearLogBtn.addEventListener('click', clearLog);
- // 初始检查
- checkReady();
- log('💡 请选择模板和来源Excel文件,然后点击"预览数据"', 'info');
- </script>
- </body>
- </html>
|