<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Excel 批注提取工具</title>
<script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script>
<style>
body {
font-family: "Segoe UI", "Microsoft Yahei", sans-serif;
background: linear-gradient(135deg, #74ABE2, #5563DE);
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: #fff;
padding: 30px;
border-radius: 16px;
box-shadow: 0 6px 20px rgba(0,0,0,0.2);
text-align: center;
width: 450px;
animation: fadeIn 0.8s ease-in-out;
}
h1 {
font-size: 22px;
margin-bottom: 20px;
color: #444;
}
input[type="file"] {
display: none;
}
label {
display: inline-block;
background: #5563DE;
color: #fff;
padding: 10px 18px;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
}
label:hover {
background: #3f4bb3;
}
button {
margin-top: 20px;
background: #74ABE2;
color: #fff;
border: none;
padding: 10px 18px;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #4e8bd1;
}
.filename {
margin-top: 10px;
font-size: 14px;
color: #666;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="container">
<h1>Excel 批注提取工具</h1>
<form id="uploadForm">
<input type="file" id="fileInput" accept=".xlsx" />
<label for="fileInput">选择 Excel 文件</label>
<div class="filename" id="filename"></div>
<button type="button" onclick="extractComments()">提取批注并下载</button>
</form>
</div>
<script>
let selectedFile;
document.getElementById("fileInput").addEventListener("change", (e) => {
selectedFile = e.target.files[0];
document.getElementById("filename").innerText = selectedFile ? `已选择: ${selectedFile.name}` : "";
});
function extractComments() {
if (!selectedFile) {
alert("请先选择一个 Excel 文件!");
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: "array", cellComments: true });
// 创建新工作簿保存结果
const outWB = XLSX.utils.book_new();
const rows = [["工作表", "行", "列", "单元格内容", "批注内容"]];
workbook.SheetNames.forEach(sheetName => {
const ws = workbook.Sheets[sheetName];
const range = XLSX.utils.decode_range(ws["!ref"]);
for (let R = range.s.r; R <= range.e.r; ++R) {
for (let C = range.s.c; C <= range.e.c; ++C) {
const cellAddress = XLSX.utils.encode_cell({ r: R, c: C });
const cell = ws[cellAddress];
if (cell && cell.c) {
cell.c.forEach(comment => {
let text = comment.t || "";
let author = comment.a || "";
// 去掉批注人(如果文本以 "作者:" 开头)
if (text.startsWith(author)) {
text = text.slice(author.length).replace(/^[::]/, "").trim();
}
rows.push([
sheetName,
R + 1,
C + 1,
cell.v || "",
text
]);
});
}
}
}
});
// 转换为 sheet 并导出
const outWS = XLSX.utils.aoa_to_sheet(rows);
XLSX.utils.book_append_sheet(outWB, outWS, "批注汇总");
XLSX.writeFile(outWB, "批注提取结果.xlsx");
};
reader.readAsArrayBuffer(selectedFile);
}
</script>
</body>
</html>
