<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>在線表格</title><style>table {border-collapse: separate;border-spacing: 0;table-layout: fixed; /* 固定列寬 */}td, th {border: 1px solid #ddd; /* 設置默認邊框顏色為 #ddd */padding: 8px;text-align: center;white-space: nowrap; /* 防止文字換行 */overflow: visible; /* 讓文字溢出仍然顯示 */text-overflow: unset; /* 不使用省略號 */}.border-black {border: 1px solid black; /* 邊框設置為黑色 */}</style>
</head>
<body><table id="data-table"></table><script>// JSON 數據,包含表格數據以及單元格合并信息和邊框顯示信息 merge中第一個值為列,第二個值為行const tableData = {"columWidth": [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],"rowWidth": [30, 30, 30, 30, 30, 30, 100, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],"rows": [{"cells": ["表格"],"merge": [[24, 0]],"borderColor": [0],"fontSize": [40]},{"cells": ["", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},{"cells": ["內容:很長的內容", "", "", "", "名稱:abcdefg", "", "", "", "", "測試", "", "", "", "日期", "", "", "", "用戶", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},{"cells": ["", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},{"cells": ["", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},{"cells": ["01", "", "", "", "", "", "", "", "02", "", "", "", "", "", "", "", "03", "", "", "", "", "", "", ""],"merge": [[6, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [6, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [6, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["號碼", "數量", "數量", "號碼", "數量", "數量", "", "", "號碼", "數量", "數量", "號碼", "數量", "數量", "", "", "號碼", "數量", "數量", "端子號", "數量", "數量", "", ""],"merge": [[0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 0], [0, 0], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 0], [0, 0], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4], [0, 4]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],"verticalText": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],},{"cells": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["1", "1", "", "17", "1", "", "", "", "1", "1", "", "17", "1", "", "", "", "1", "1", "", "17", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["2", "1", "", "18", "1", "", "", "", "2", "1", "", "18", "1", "", "", "", "2", "1", "", "18", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["3", "1", "", "19", "1", "", "", "", "3", "1", "", "19", "1", "", "", "", "3", "1", "", "19", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["4", "1", "", "20", "1", "", "", "", "4", "1", "", "20", "1", "", "", "", "4", "1", "", "20", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["5", "1", "", "21", "1", "", "", "", "5", "1", "", "21", "1", "", "", "", "5", "1", "", "21", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["6", "1", "", "22", "1", "", "", "", "6", "1", "", "22", "1", "", "", "", "6", "1", "", "22", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["7", "1", "", "23", "1", "", "", "", "7", "1", "", "23", "1", "", "", "", "7", "1", "", "23", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["8", "1", "", "24", "1", "", "", "", "8", "1", "", "24", "1", "", "", "", "8", "1", "", "24", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["9", "1", "", "25", "1", "", "", "", "9", "1", "", "25", "1", "", "", "", "9", "1", "", "25", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["10", "1", "", "26", "1", "", "", "", "10", "1", "", "26", "1", "", "", "", "10", "1", "", "26", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["11", "1", "", "27", "1", "", "", "", "11", "1", "", "27", "1", "", "", "", "11", "1", "", "27", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["12", "1", "", "28", "1", "", "", "", "12", "1", "", "28", "1", "", "", "", "12", "1", "", "28", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["13", "1", "", "29", "1", "", "", "", "13", "1", "", "29", "1", "", "", "", "13", "1", "", "29", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["14", "1", "", "30", "1", "", "", "", "14", "1", "", "30", "1", "", "", "", "14", "1", "", "30", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["15", "1", "", "31", "1", "", "", "" ,"15", "1", "", "31", "1", "", "", "" ,"15", "1", "", "31", "1", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]},{"cells": ["", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},{"cells": ["", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "", ""],"merge": [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],"borderColor": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}]};// 動態繪制表格function drawTable(data) {const table = document.getElementById('data-table');// 標記哪些單元格已經合并,防止重復渲染const mergedCells = [];// 繪制數據行data.rows.forEach((rowData, rowIndex) => {const tr = document.createElement('tr');rowData.cells.forEach((cellData, index) => {const td = document.createElement('td');// 跳過已經合并過的單元格if (mergedCells[rowIndex] && mergedCells[rowIndex][index]) {return; // 跳過合并的單元格}td.textContent = cellData;//設置列寬if (data.columWidth[index]){td.style.maxWidth = data.columWidth[index] + 'px';td.style.minWidth = data.columWidth[index] + 'px';}//設置行高if (data.rowWidth[rowIndex]){td.style.maxHeight = data.rowWidth[rowIndex] + 'px';td.style.minHeight = data.rowWidth[rowIndex] + 'px';}// 設置豎排if (rowData.verticalText && rowData.verticalText[index] === 1) {td.style.writingMode = 'vertical-rl';}// 設置文字大小if (rowData.fontSize && rowData.fontSize[index]) {td.style.fontSize = rowData.fontSize[index] + 'px';}// 合并列if (!rowData.merge[index]) {rowData.merge[index] = [0, 0];}if (rowData.merge[index][0] > 0) {td.setAttribute('colspan', rowData.merge[index][0]);// 標記合并的單元格for (let i = 0; i < rowData.merge[index][0]; i++) {mergedCells[rowIndex] = mergedCells[rowIndex] || [];mergedCells[rowIndex][index + i] = true;}}// 合并行if (rowData.merge[index][1] > 0) {td.setAttribute('rowspan', rowData.merge[index][1]);// 標記合并的單元格for (let i = 0; i < rowData.merge[index][1]; i++) {mergedCells[rowIndex + i] = mergedCells[rowIndex + i] || [];mergedCells[rowIndex + i][index] = true;}}// 設置邊框if (rowData.borderColor[index] === 1) {td.classList.add('border-black');}tr.appendChild(td);});table.appendChild(tr);});editTable(table);}// 調用函數繪制表格drawTable(tableData);//編輯單元格的方法function editTable(table){let currentCell = null; // 當前編輯的單元格let oldValue = ""; // 原始內容// 雙擊進入編輯模式table.addEventListener("dblclick", function(event) {if (event.target.tagName === "TD") {currentCell = event.target; // 當前單元格oldValue = currentCell.innerText; // 保存原始內容const input = document.createElement("input");input.type = "text";input.value = oldValue;currentCell.innerHTML = "";currentCell.appendChild(input);input.focus();// 點擊其他地方退出編輯input.addEventListener("blur", function() {exitEdit();});// 按下回車退出編輯input.addEventListener("keydown", function(e) {if (e.key === "Enter") {exitEdit();}});}});// 退出編輯并顯示信息function exitEdit() {if (currentCell) {const newValue = currentCell.querySelector("input").value;currentCell.innerHTML = newValue; // 更新內容// 獲取行列索引const rowIndex = currentCell.parentElement.rowIndex;const cellIndex = currentCell.cellIndex;// 顯示行列及編輯前后的內容alert(`行: ${rowIndex + 1}, 列: ${cellIndex + 1}\n編輯前: ${oldValue}\n編輯后: ${newValue}`);currentCell = null; // 清空當前編輯單元格}}// 點擊其他地方退出編輯document.addEventListener("click", function(event) {if (currentCell && !currentCell.contains(event.target)) {exitEdit();}});}
</script></body>
</html>
效果如圖