<!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>
效果如图