index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Excel 自动填表工具</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  15. background: #f5f7fa;
  16. color: #333;
  17. padding: 20px;
  18. }
  19. .container {
  20. max-width: 1100px;
  21. margin: 0 auto;
  22. background: #fff;
  23. border-radius: 12px;
  24. box-shadow: 0 2px 12px rgba(0,0,0,0.08);
  25. padding: 30px;
  26. }
  27. h1 {
  28. font-size: 24px;
  29. color: #1a1a2e;
  30. margin-bottom: 8px;
  31. display: flex;
  32. align-items: center;
  33. gap: 10px;
  34. }
  35. .subtitle {
  36. color: #888;
  37. font-size: 14px;
  38. margin-bottom: 24px;
  39. border-bottom: 1px solid #eee;
  40. padding-bottom: 16px;
  41. }
  42. .form-group {
  43. margin-bottom: 18px;
  44. }
  45. .form-group label {
  46. display: block;
  47. font-weight: 600;
  48. font-size: 14px;
  49. margin-bottom: 5px;
  50. color: #444;
  51. }
  52. .form-group .hint {
  53. font-size: 12px;
  54. color: #999;
  55. font-weight: 400;
  56. }
  57. .form-row {
  58. display: flex;
  59. gap: 20px;
  60. flex-wrap: wrap;
  61. }
  62. .form-row .form-group {
  63. flex: 1;
  64. min-width: 180px;
  65. }
  66. select, input[type="file"], input[type="number"] {
  67. width: 100%;
  68. padding: 10px 12px;
  69. border: 1px solid #d1d5db;
  70. border-radius: 8px;
  71. font-size: 14px;
  72. background: #fafbfc;
  73. transition: border-color 0.2s;
  74. }
  75. select:focus, input:focus {
  76. border-color: #4f8cf7;
  77. outline: none;
  78. box-shadow: 0 0 0 3px rgba(79, 140, 247, 0.15);
  79. }
  80. .file-upload-wrapper {
  81. position: relative;
  82. display: flex;
  83. align-items: center;
  84. gap: 12px;
  85. }
  86. .file-upload-wrapper input[type="file"] {
  87. padding: 8px;
  88. }
  89. .file-name {
  90. font-size: 13px;
  91. color: #666;
  92. white-space: nowrap;
  93. overflow: hidden;
  94. text-overflow: ellipsis;
  95. max-width: 200px;
  96. }
  97. .btn {
  98. padding: 10px 28px;
  99. border: none;
  100. border-radius: 8px;
  101. font-size: 15px;
  102. font-weight: 600;
  103. cursor: pointer;
  104. transition: all 0.2s;
  105. display: inline-flex;
  106. align-items: center;
  107. gap: 8px;
  108. }
  109. .btn-primary {
  110. background: #4f8cf7;
  111. color: #fff;
  112. }
  113. .btn-primary:hover {
  114. background: #3a7ae8;
  115. transform: translateY(-1px);
  116. }
  117. .btn-primary:disabled {
  118. background: #b0c8f5;
  119. cursor: not-allowed;
  120. transform: none;
  121. }
  122. .btn-success {
  123. background: #34c759;
  124. color: #fff;
  125. }
  126. .btn-success:hover {
  127. background: #2db84e;
  128. }
  129. .btn-success:disabled {
  130. background: #9de0b2;
  131. cursor: not-allowed;
  132. }
  133. .btn-secondary {
  134. background: #e8ecf1;
  135. color: #444;
  136. }
  137. .btn-secondary:hover {
  138. background: #d5dbe3;
  139. }
  140. .btn-danger {
  141. background: #ef4444;
  142. color: #fff;
  143. }
  144. .btn-danger:hover {
  145. background: #dc2626;
  146. }
  147. .actions {
  148. display: flex;
  149. gap: 12px;
  150. flex-wrap: wrap;
  151. margin: 20px 0 16px;
  152. padding: 16px 0;
  153. border-top: 1px solid #eee;
  154. border-bottom: 1px solid #eee;
  155. }
  156. .log-area {
  157. background: #1a1a2e;
  158. color: #e4e4e4;
  159. border-radius: 8px;
  160. padding: 16px 20px;
  161. font-family: 'Courier New', monospace;
  162. font-size: 12px;
  163. max-height: 320px;
  164. overflow-y: auto;
  165. white-space: pre-wrap;
  166. word-break: break-all;
  167. line-height: 1.6;
  168. margin-top: 16px;
  169. }
  170. .log-area .log-success {
  171. color: #4ade80;
  172. }
  173. .log-area .log-error {
  174. color: #f87171;
  175. }
  176. .log-area .log-warn {
  177. color: #fbbf24;
  178. }
  179. .log-area .log-info {
  180. color: #60a5fa;
  181. }
  182. .preview-table-wrapper {
  183. overflow-x: auto;
  184. margin-top: 16px;
  185. border-radius: 8px;
  186. border: 1px solid #e5e7eb;
  187. }
  188. .preview-table {
  189. width: 100%;
  190. border-collapse: collapse;
  191. font-size: 13px;
  192. min-width: 600px;
  193. }
  194. .preview-table th {
  195. background: #f3f4f6;
  196. padding: 10px 12px;
  197. text-align: left;
  198. font-weight: 600;
  199. border-bottom: 2px solid #e5e7eb;
  200. white-space: nowrap;
  201. position: sticky;
  202. top: 0;
  203. z-index: 1;
  204. }
  205. .preview-table td {
  206. padding: 8px 12px;
  207. border-bottom: 1px solid #e5e7eb;
  208. max-width: 200px;
  209. overflow: hidden;
  210. text-overflow: ellipsis;
  211. white-space: nowrap;
  212. }
  213. .preview-table tr:hover td {
  214. background: #f9fafb;
  215. }
  216. .status-badge {
  217. display: inline-block;
  218. padding: 4px 14px;
  219. border-radius: 20px;
  220. font-size: 13px;
  221. font-weight: 500;
  222. }
  223. .status-badge.idle {
  224. background: #e5e7eb;
  225. color: #6b7280;
  226. }
  227. .status-badge.loading {
  228. background: #fef3c7;
  229. color: #d97706;
  230. }
  231. .status-badge.success {
  232. background: #d1fae5;
  233. color: #065f46;
  234. }
  235. .status-badge.error {
  236. background: #fee2e2;
  237. color: #991b1b;
  238. }
  239. .flex {
  240. display: flex;
  241. align-items: center;
  242. gap: 12px;
  243. }
  244. .flex-between {
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. flex-wrap: wrap;
  249. gap: 10px;
  250. }
  251. .mt-8 { margin-top: 8px; }
  252. .mt-12 { margin-top: 12px; }
  253. .text-center { text-align: center; }
  254. .text-sm { font-size: 13px; }
  255. .text-muted { color: #888; }
  256. .hidden { display: none !important; }
  257. .download-section {
  258. background: #ecfdf5;
  259. border: 1px solid #6ee7b7;
  260. border-radius: 8px;
  261. padding: 16px 20px;
  262. display: none;
  263. align-items: center;
  264. justify-content: space-between;
  265. flex-wrap: wrap;
  266. gap: 12px;
  267. margin-top: 12px;
  268. }
  269. .download-section.show {
  270. display: flex;
  271. }
  272. </style>
  273. </head>
  274. <body>
  275. <div class="container">
  276. <h1>📊 Excel 自动填表工具</h1>
  277. <div class="subtitle">
  278. 选择模板 → 上传来源数据 → 预览 → 生成并下载
  279. <span class="status-badge idle" id="statusBadge">就绪</span>
  280. </div>
  281. <!-- 配置区域 -->
  282. <div class="form-row">
  283. <div class="form-group">
  284. <label>📁 选择模板 <span class="hint">(模板文件需放在服务器 templates_excel/ 目录)</span></label>
  285. <select id="templateSelect">
  286. <option value="">-- 请选择模板 --</option>
  287. {% for t in templates %}
  288. <option value="{{ t }}">{{ t }}</option>
  289. {% endfor %}
  290. </select>
  291. </div>
  292. <div class="form-group">
  293. <label>📄 来源数据 Excel</label>
  294. <div class="file-upload-wrapper">
  295. <input type="file" id="sourceFile" accept=".xlsx,.xls">
  296. <span class="file-name" id="fileName">未选择文件</span>
  297. </div>
  298. </div>
  299. </div>
  300. <div class="form-row">
  301. <div class="form-group">
  302. <label>来源表头行号 <span class="hint">(第2行是字段名)</span></label>
  303. <input type="number" id="sourceHeader" value="2" min="1">
  304. </div>
  305. <div class="form-group">
  306. <label>模板表头行号 <span class="hint">(第4行是字段名)</span></label>
  307. <input type="number" id="templateHeader" value="4" min="1">
  308. </div>
  309. </div>
  310. <!-- 操作按钮 -->
  311. <div class="actions">
  312. <button class="btn btn-primary" id="previewBtn">🔍 预览数据</button>
  313. <button class="btn btn-success" id="generateBtn" disabled>🚀 生成并下载</button>
  314. <button class="btn btn-secondary" id="clearLogBtn">🗑️ 清空日志</button>
  315. </div>
  316. <!-- 状态信息 -->
  317. <div class="flex-between">
  318. <span id="statusText" class="text-muted text-sm">请选择模板和来源文件</span>
  319. <span id="rowCount" class="text-muted text-sm"></span>
  320. </div>
  321. <!-- 预览表格 -->
  322. <div id="previewContainer" class="hidden">
  323. <div class="flex-between mt-8">
  324. <span class="text-sm" style="font-weight:600;">📋 数据预览 (前10行)</span>
  325. </div>
  326. <div class="preview-table-wrapper" id="previewWrapper">
  327. <table class="preview-table" id="previewTable">
  328. <thead id="previewHead"></thead>
  329. <tbody id="previewBody"></tbody>
  330. </table>
  331. </div>
  332. </div>
  333. <!-- 下载区域 -->
  334. <div class="download-section" id="downloadSection">
  335. <span style="font-weight:600;color:#065f46;">✅ 生成完成!</span>
  336. <span id="downloadInfo"></span>
  337. <a href="#" id="downloadLink" class="btn btn-success" style="text-decoration:none;">📥 下载文件</a>
  338. </div>
  339. <!-- 日志 -->
  340. <div class="log-area" id="logArea">
  341. <span class="log-info">💡 系统就绪,等待操作...</span>
  342. </div>
  343. </div>
  344. <script>
  345. // DOM 元素
  346. const templateSelect = document.getElementById('templateSelect');
  347. const sourceFile = document.getElementById('sourceFile');
  348. const fileName = document.getElementById('fileName');
  349. const sourceHeader = document.getElementById('sourceHeader');
  350. const templateHeader = document.getElementById('templateHeader');
  351. const previewBtn = document.getElementById('previewBtn');
  352. const generateBtn = document.getElementById('generateBtn');
  353. const clearLogBtn = document.getElementById('clearLogBtn');
  354. const logArea = document.getElementById('logArea');
  355. const statusText = document.getElementById('statusText');
  356. const statusBadge = document.getElementById('statusBadge');
  357. const rowCount = document.getElementById('rowCount');
  358. const previewContainer = document.getElementById('previewContainer');
  359. const previewHead = document.getElementById('previewHead');
  360. const previewBody = document.getElementById('previewBody');
  361. const downloadSection = document.getElementById('downloadSection');
  362. const downloadLink = document.getElementById('downloadLink');
  363. const downloadInfo = document.getElementById('downloadInfo');
  364. let sourcePath = null;
  365. // 日志函数
  366. function log(msg, type = 'info') {
  367. const colors = {
  368. info: 'log-info',
  369. success: 'log-success',
  370. error: 'log-error',
  371. warn: 'log-warn'
  372. };
  373. const prefix = {
  374. info: 'ℹ️',
  375. success: '✅',
  376. error: '❌',
  377. warn: '⚠️'
  378. };
  379. const timestamp = new Date().toLocaleTimeString();
  380. const line = document.createElement('div');
  381. line.innerHTML = `<span style="color:#888;">[${timestamp}]</span> <span class="${colors[type] || 'log-info'}">${prefix[type] || 'ℹ️'} ${msg}</span>`;
  382. logArea.appendChild(line);
  383. logArea.scrollTop = logArea.scrollHeight;
  384. }
  385. function setStatus(text, type = 'idle') {
  386. statusText.textContent = text;
  387. statusBadge.textContent = {
  388. idle: '就绪',
  389. loading: '处理中...',
  390. success: '成功',
  391. error: '失败'
  392. }[type] || '就绪';
  393. statusBadge.className = `status-badge ${type}`;
  394. }
  395. function clearLog() {
  396. logArea.innerHTML = '';
  397. log('日志已清空', 'info');
  398. }
  399. // 文件选择
  400. sourceFile.addEventListener('change', function() {
  401. if (this.files.length > 0) {
  402. fileName.textContent = this.files[0].name;
  403. } else {
  404. fileName.textContent = '未选择文件';
  405. }
  406. checkReady();
  407. });
  408. templateSelect.addEventListener('change', checkReady);
  409. function checkReady() {
  410. const hasTemplate = templateSelect.value !== '';
  411. const hasFile = sourceFile.files.length > 0;
  412. previewBtn.disabled = !(hasTemplate && hasFile);
  413. generateBtn.disabled = true;
  414. if (hasTemplate && hasFile) {
  415. setStatus('已选择模板和来源文件,点击预览', 'idle');
  416. } else {
  417. setStatus('请选择模板和来源文件', 'idle');
  418. }
  419. }
  420. // 预览
  421. previewBtn.addEventListener('click', async function() {
  422. const template = templateSelect.value;
  423. const file = sourceFile.files[0];
  424. const sHeader = parseInt(sourceHeader.value) || 2;
  425. if (!template || !file) {
  426. log('请选择模板和来源文件', 'warn');
  427. return;
  428. }
  429. this.disabled = true;
  430. setStatus('正在预览...', 'loading');
  431. log(`📖 预览来源数据: ${file.name}`, 'info');
  432. const formData = new FormData();
  433. formData.append('source_file', file);
  434. formData.append('template_name', template);
  435. formData.append('source_header', sHeader);
  436. try {
  437. const response = await fetch('/preview', {
  438. method: 'POST',
  439. body: formData
  440. });
  441. const result = await response.json();
  442. if (result.error) {
  443. log(`❌ 预览失败: ${result.error}`, 'error');
  444. setStatus('预览失败', 'error');
  445. previewContainer.classList.add('hidden');
  446. return;
  447. }
  448. sourcePath = result.source_path;
  449. log(`✅ 成功读取 ${result.total_rows} 行数据`, 'success');
  450. setStatus(`预览成功,共 ${result.total_rows} 行`, 'success');
  451. rowCount.textContent = `共 ${result.total_rows} 行`;
  452. // 渲染表格
  453. renderPreview(result.columns, result.data);
  454. previewContainer.classList.remove('hidden');
  455. // 启用生成按钮
  456. generateBtn.disabled = false;
  457. } catch (err) {
  458. log(`❌ 请求失败: ${err.message}`, 'error');
  459. setStatus('预览失败', 'error');
  460. } finally {
  461. this.disabled = false;
  462. }
  463. });
  464. function renderPreview(columns, data) {
  465. // 表头
  466. previewHead.innerHTML = `<tr>${columns.map(c => `<th>${escapeHtml(c)}</th>`).join('')}</tr>`;
  467. // 数据行
  468. previewBody.innerHTML = data.map(row => {
  469. return `<tr>${columns.map(c => `<td title="${escapeHtml(String(row[c] || ''))}">${escapeHtml(String(row[c] || ''))}</td>`).join('')}</tr>`;
  470. }).join('');
  471. }
  472. function escapeHtml(text) {
  473. const div = document.createElement('div');
  474. div.textContent = text;
  475. return div.innerHTML;
  476. }
  477. // 生成
  478. generateBtn.addEventListener('click', async function() {
  479. if (!sourcePath) {
  480. log('请先预览数据', 'warn');
  481. return;
  482. }
  483. const template = templateSelect.value;
  484. const sHeader = parseInt(sourceHeader.value) || 2;
  485. const tHeader = parseInt(templateHeader.value) || 4;
  486. this.disabled = true;
  487. previewBtn.disabled = true;
  488. setStatus('正在生成...', 'loading');
  489. log('🚀 开始生成并追加到模板...', 'info');
  490. const formData = new FormData();
  491. formData.append('source_path', sourcePath);
  492. formData.append('template_name', template);
  493. formData.append('source_header', sHeader);
  494. formData.append('header_row', tHeader);
  495. try {
  496. const response = await fetch('/generate', {
  497. method: 'POST',
  498. body: formData
  499. });
  500. const result = await response.json();
  501. if (result.error) {
  502. log(`❌ 生成失败: ${result.error}`, 'error');
  503. setStatus('生成失败', 'error');
  504. return;
  505. }
  506. log(`✅ ${result.message}`, 'success');
  507. setStatus(`生成成功,追加 ${result.filled_count} 行`, 'success');
  508. // 显示下载
  509. downloadSection.classList.add('show');
  510. downloadLink.href = result.download_url;
  511. downloadInfo.textContent = `已追加 ${result.filled_count} 行数据`;
  512. } catch (err) {
  513. log(`❌ 请求失败: ${err.message}`, 'error');
  514. setStatus('生成失败', 'error');
  515. } finally {
  516. this.disabled = false;
  517. previewBtn.disabled = false;
  518. }
  519. });
  520. // 清空日志
  521. clearLogBtn.addEventListener('click', clearLog);
  522. // 初始检查
  523. checkReady();
  524. log('💡 请选择模板和来源Excel文件,然后点击"预览数据"', 'info');
  525. </script>
  526. </body>
  527. </html>