-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
248 lines (211 loc) · 6.29 KB
/
Copy pathindex.html
File metadata and controls
248 lines (211 loc) · 6.29 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!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>
<script src="https://cdn.sheetjs.com/xlsx-0.20.0/package/dist/xlsx.full.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.card {
background: white;
border-radius: 16px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 520px;
padding: 32px;
}
h1 {
text-align: center;
color: #2c3e50;
margin-bottom: 24px;
font-size: 24px;
font-weight: 600;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #34495e;
font-weight: 500;
}
input[type="file"] {
width: 100%;
padding: 10px;
border: 2px dashed #cbd5e0;
border-radius: 8px;
background: #f8fafc;
cursor: pointer;
transition: border-color 0.3s;
}
input[type="file"]:hover {
border-color: #a0aec0;
}
.options {
background: #f8fafc;
padding: 16px;
border-radius: 12px;
margin-bottom: 24px;
border: 1px solid #edf2f7;
}
.option-row {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.option-row:last-child {
margin-bottom: 0;
}
input[type="checkbox"] {
transform: scale(1.2);
cursor: pointer;
}
input[type="number"] {
width: 80px;
padding: 6px 10px;
border: 1px solid #cbd5e0;
border-radius: 6px;
font-size: 14px;
}
button {
width: 100%;
padding: 12px;
background: #4299e1;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: background 0.3s, transform 0.2s;
}
button:hover {
background: #3182ce;
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
#status {
margin-top: 16px;
min-height: 24px;
text-align: center;
font-size: 14px;
font-weight: 500;
}
.success { color: #38a169; }
.error { color: #e53e3e; }
footer {
text-align: center;
margin-top: 20px;
color: #718096;
font-size: 12px;
}
</style>
</head>
<body>
<div class="card">
<h1>Excel 行合并工具</h1>
<div class="input-group">
<label for="fileInput">📎 选择 Excel 文件 (.xlsx)</label>
<input type="file" id="fileInput" accept=".xlsx" />
</div>
<div class="options">
<div class="option-row">
<input type="checkbox" id="hasHeader" checked />
<label for="hasHeader" style="margin: 0;">包含表头</label>
</div>
<div class="option-row">
<label for="startRow" style="margin: 0;">从第</label>
<input type="number" id="startRow" value="2" min="1" />
<label for="startRow" style="margin: 0;">行开始处理</label>
</div>
</div>
<button id="processBtn">🚀 开始合并</button>
<div id="status"></div>
<footer>
所有文件在浏览器中处理,不会上传到任何服务器。
</footer>
</div>
<script>
document.getElementById('processBtn').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const hasHeader = document.getElementById('hasHeader').checked;
const startRow = parseInt(document.getElementById('startRow').value);
const statusEl = document.getElementById('status');
if (!fileInput.files[0]) {
statusEl.className = 'error';
statusEl.textContent = '⚠️ 请先选择一个 Excel 文件!';
return;
}
if (isNaN(startRow) || startRow < 1) {
statusEl.className = 'error';
statusEl.textContent = '⚠️ 起始行号必须 ≥ 1!';
return;
}
try {
statusEl.className = '';
statusEl.textContent = '⏳ 正在处理,请稍候...';
const file = fileInput.files[0];
const data = await file.arrayBuffer();
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const rawData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
if (rawData.length === 0) throw new Error('工作表为空');
let outputData = [];
if (hasHeader) {
outputData.push(rawData[0]);
}
let actualStart = hasHeader ? Math.max(1, startRow - 1) : startRow - 1;
const rows = rawData.slice(actualStart);
for (let i = 0; i < rows.length; i += 2) {
const row1 = rows[i] || [];
const row2 = rows[i + 1] || [];
const mergedRow = [];
const maxLen = Math.max(row1.length, row2.length);
for (let j = 0; j < maxLen; j++) {
const cell1 = row1[j];
const cell2 = row2[j];
if (cell1 !== undefined && cell1 !== null && cell1 !== '') {
mergedRow.push(cell1);
} else if (cell2 !== undefined && cell2 !== null && cell2 !== '') {
mergedRow.push(cell2);
} else {
mergedRow.push('');
}
}
outputData.push(mergedRow);
}
const newWorksheet = XLSX.utils.aoa_to_sheet(outputData);
const newWorkbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(newWorkbook, newWorksheet, '合并结果');
const outputName = file.name.replace(/\.xlsx?$/i, '_merged.xlsx');
XLSX.writeFile(newWorkbook, outputName);
statusEl.className = 'success';
statusEl.textContent = '✅ 合并完成!文件已下载。';
} catch (e) {
console.error(e);
statusEl.className = 'error';
statusEl.textContent = '❌ 处理失败:' + (e.message || '未知错误');
}
});
</script>
</body>
</html>