188 lines
8.7 KiB
HTML
188 lines
8.7 KiB
HTML
|
<!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>
|
|||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|||
|
<style>
|
|||
|
body {
|
|||
|
padding: 20px;
|
|||
|
}
|
|||
|
.progress-container {
|
|||
|
position: relative;
|
|||
|
width: 100%;
|
|||
|
height: 20px;
|
|||
|
background-color: #f3f3f3;
|
|||
|
border-radius: 10px;
|
|||
|
overflow: hidden;
|
|||
|
margin-top: 10px;
|
|||
|
}
|
|||
|
.progress-bar {
|
|||
|
height: 100%;
|
|||
|
width: 0;
|
|||
|
background-color: #4caf50;
|
|||
|
border-radius: 10px;
|
|||
|
transition: width 0.3s ease-in-out;
|
|||
|
}
|
|||
|
.progress-text {
|
|||
|
position: absolute;
|
|||
|
top: 0;
|
|||
|
left: 50%;
|
|||
|
transform: translateX(-50%);
|
|||
|
font-size: 14px;
|
|||
|
color: #333;
|
|||
|
line-height: 20px;
|
|||
|
font-weight: bold;
|
|||
|
white-space: nowrap;
|
|||
|
}
|
|||
|
</style>
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<div class="container">
|
|||
|
<h1 class="text-center">Excel 数据处理工具</h1>
|
|||
|
<div class="row">
|
|||
|
<div class="col-md-6 offset-md-3">
|
|||
|
<div class="form-group">
|
|||
|
<label for="fileInput">上传Excel或CSV文件</label>
|
|||
|
<input type="file" class="form-control-file" id="fileInput" accept=".xlsx, .xls, .csv">
|
|||
|
</div>
|
|||
|
<div class="form-group">
|
|||
|
<label for="sheetSelect">选择工作表</label>
|
|||
|
<select class="form-control" id="sheetSelect"></select>
|
|||
|
</div>
|
|||
|
<button class="btn btn-primary" id="processButton">处理数据</button>
|
|||
|
<div class="progress-container">
|
|||
|
<div class="progress-bar" id="progress-bar"></div>
|
|||
|
<span class="progress-text" id="progress-text">0%</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
|
|||
|
<script>
|
|||
|
document.addEventListener('DOMContentLoaded', function() {
|
|||
|
const fileInput = document.getElementById('fileInput');
|
|||
|
const sheetSelect = document.getElementById('sheetSelect');
|
|||
|
const processButton = document.getElementById('processButton');
|
|||
|
const progressBar = document.getElementById('progress-bar');
|
|||
|
const progressText = document.getElementById('progress-text');
|
|||
|
|
|||
|
fileInput.addEventListener('change', function(event) {
|
|||
|
const file = event.target.files[0];
|
|||
|
if (file) {
|
|||
|
const reader = new FileReader();
|
|||
|
reader.onload = function(e) {
|
|||
|
const data = e.target.result;
|
|||
|
const workbook = XLSX.read(data, { type: 'binary' });
|
|||
|
sheetSelect.innerHTML = '';
|
|||
|
workbook.SheetNames.forEach(sheetName => {
|
|||
|
const option = document.createElement('option');
|
|||
|
option.value = sheetName;
|
|||
|
option.text = sheetName;
|
|||
|
sheetSelect.appendChild(option);
|
|||
|
});
|
|||
|
processButton.disabled = false;
|
|||
|
};
|
|||
|
reader.readAsBinaryString(file);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
processButton.addEventListener('click', function() {
|
|||
|
const file = fileInput.files[0];
|
|||
|
const sheetName = sheetSelect.value;
|
|||
|
const reader = new FileReader();
|
|||
|
reader.onload = function(e) {
|
|||
|
const data = e.target.result;
|
|||
|
const workbook = XLSX.read(data, { type: 'binary' });
|
|||
|
const worksheet = workbook.Sheets[sheetName];
|
|||
|
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
|
|||
|
|
|||
|
// 定义需要导出的列号
|
|||
|
const colList = [2, 5, 6, 7, 8, 9, 11, 12, 13, 16, 18, 19, 20, 22, 27, 28, 29, 30, 31, 40, 51];
|
|||
|
const insertProvince = "陕西省";
|
|||
|
const insertCity = "宝鸡市";
|
|||
|
|
|||
|
// 处理数据
|
|||
|
const processedData = [];
|
|||
|
const headers = jsonData[0]; // 获取表头
|
|||
|
const newHeaders = [];
|
|||
|
|
|||
|
headers.forEach((header, colIndex) => {
|
|||
|
if (colList.includes(colIndex + 1)) {
|
|||
|
if (colIndex + 1 === 16) {
|
|||
|
// 拆分第16列标题
|
|||
|
newHeaders.push(header + "1");
|
|||
|
newHeaders.push(header + "2");
|
|||
|
newHeaders.push(header + "3");
|
|||
|
} else if (colIndex + 1 === 18) {
|
|||
|
// 在“问题区域”列前插入两列
|
|||
|
newHeaders.push("省");
|
|||
|
newHeaders.push("市");
|
|||
|
newHeaders.push(header);
|
|||
|
} else {
|
|||
|
newHeaders.push(header);
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
processedData.push(newHeaders); // 添加新的表头
|
|||
|
|
|||
|
const totalRows = jsonData.length - 1; // 总行数(不包括表头)
|
|||
|
let processedRows = 0;
|
|||
|
|
|||
|
jsonData.slice(1).forEach((row) => {
|
|||
|
const newRow = [];
|
|||
|
colList.forEach((colIndex) => {
|
|||
|
if (colIndex === 16) {
|
|||
|
// 拆分第16列数据
|
|||
|
const splitData = (row[colIndex - 1] || '').split('-');
|
|||
|
// 确保拆分后的单元格数量固定为3,不足部分用空字符串填充
|
|||
|
const paddedSplitData = splitData.concat(new Array(3 - splitData.length).fill(''));
|
|||
|
newRow.push(...paddedSplitData);
|
|||
|
} else if (colIndex === 18) {
|
|||
|
// 在“问题区域”列前插入两列
|
|||
|
newRow.push(insertProvince);
|
|||
|
newRow.push(insertCity);
|
|||
|
newRow.push(row[colIndex - 1]);
|
|||
|
} else if (colIndex === 9 || colIndex === 27) {
|
|||
|
// 处理第9列和第27列(日期时间列),确保导出时保持日期时间格式
|
|||
|
if (typeof row[colIndex - 1] === 'number') {
|
|||
|
const date = new Date(Math.round((row[colIndex - 1] - 25569) * 86400000)); // 转换为JavaScript日期
|
|||
|
newRow.push(date.toISOString().split('T')[0] + ' ' + date.toTimeString().split(' ')[0]);
|
|||
|
} else {
|
|||
|
newRow.push(row[colIndex - 1]);
|
|||
|
}
|
|||
|
} else {
|
|||
|
// 替换特定内容
|
|||
|
newRow.push(row[colIndex - 1] === "陕西省12313分中心" ? "12313" : row[colIndex - 1]);
|
|||
|
}
|
|||
|
});
|
|||
|
processedData.push(newRow);
|
|||
|
|
|||
|
// 更新进度条
|
|||
|
processedRows++;
|
|||
|
const percentage = Math.round((processedRows / totalRows) * 100);
|
|||
|
progressBar.style.width = `${percentage}%`;
|
|||
|
progressText.textContent = `${percentage}%`;
|
|||
|
});
|
|||
|
|
|||
|
// 创建新的工作簿并添加处理后的数据
|
|||
|
const newWorksheet = XLSX.utils.aoa_to_sheet(processedData);
|
|||
|
const newWorkbook = XLSX.utils.book_new();
|
|||
|
XLSX.utils.book_append_sheet(newWorkbook, newWorksheet, 'ProcessedData');
|
|||
|
|
|||
|
// 保存文件
|
|||
|
XLSX.writeFile(newWorkbook, 'ProcessedData.xlsx');
|
|||
|
|
|||
|
// 更新进度条为100%完成
|
|||
|
progressBar.style.width = '100%';
|
|||
|
progressText.textContent = '100% 完成';
|
|||
|
};
|
|||
|
reader.readAsBinaryString(file);
|
|||
|
});
|
|||
|
});
|
|||
|
</script>
|
|||
|
</body>
|
|||
|
</html>
|