以下是修正後的程式碼,主要修正日期格式處理及編號欄位引用方式:
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>稽核計劃轉換工具</title>
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.input-group {
margin: 15px 0;
}
input[type="text"], input[type="file"] {
padding: 8px;
width: 100%;
margin: 5px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #0056b3;
}
#downloadLink {
display: none;
margin-top: 15px;
padding: 10px;
background: #28a745;
color: white;
text-decoration: none;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>稽核計劃轉換工具</h1>
<div class="input-group">
<input type="text" id="stockCode" placeholder="輸入公司股票代碼(例如:2644)">
</div>
<div class="input-group">
<input type="file" id="excelFile" accept=".xlsx,.xls">
</div>
<button onclick="processFile()">轉換並下載</button>
<a id="downloadLink">下載結果</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script>
function processFile() {
const stockCode = document.getElementById('stockCode').value;
const fileInput = document.getElementById('excelFile');
if (!stockCode || !fileInput.files[0]) {
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' });
const firstSheet = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheet];
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
// 跳過前兩行表頭
const content = jsonData.slice(2)
.map(row => {
const [start, end] = row[2].split('-');
return `{[(表頭)]}${stockCode}${start.replace(/\//g, '')}${end.replace(/\//g, '')}${row[0]}\n稽核項目:${row[1]}`;
})
.join('\n\n');
const blob = new Blob([content], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.getElementById('downloadLink');
link.href = url;
link.download = `稽核計劃_${stockCode}.txt`;
link.style.display = 'block';
link.textContent = '點擊下載結果';
};
reader.readAsArrayBuffer(fileInput.files[0]);
}
</script>
</body>
</html>
主要改進點:
-
日期格式處理
使用 replace(/\//g, '') 來移除所有斜杠,正確轉換成 YYYYMMDD 格式
-
編號欄位引用
直接使用 row[0] 取得原始編號,避免重複添加前綴
-
格式優化
- 增加換行格式
"\n\n" 提升可讀性
- 使用更現代的 CSS 樣式
- 增加下載連結的互動反饋
-
使用者體驗
- 整合成單一 HTML 文件
- 更清晰的輸入提示
- 更友好的下載提示
使用方式:
- 將上述程式碼保存為
.html 檔案
- 使用支援 ES6 的現代瀏覽器開啟
- 輸入股票代碼(例如:2644)
- 拖曳 Excel 檔案到上傳區域
- 點擊「轉換並下載」按鈕
輸出格式範例:
{[(表頭)]}264411401011140131IA-114001
稽核項目:電腦化資訊系統循環及其法令遵循
(每個稽核項目間會有兩個換行符號分隔)