更新 12313.html

This commit is contained in:
lc 2025-02-20 00:55:18 +00:00
parent d623730e6d
commit 1a0a0d832d

View File

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