生成导入数据模板
yaoye Lv5

生成导入数据模板

表格

系统表格模板,如图

系统表格模板

数据表格,如图

数据表格

数据表格的字段不确定,需要根据需要动态调整

工作

  • 需要将数据表格的数据导入到系统表格模板中的对应字段

  • 系统表格模板中字段是确定的,数据表格的字段非确定

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel Data Mapper</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
select, input {
width: 100%;
box-sizing: border-box;
}
button {
margin: 10px 0;
padding: 5px 10px;
}
</style>
</head>
<body>
<h1>Excel Data Mapper</h1>

<div>
<h2>上传数据表格</h2>
<input type="file" id="dataFile" accept=".xlsx, .xls">
</div>

<div id="mappingTable"></div>

<button onclick="mapData()">导入数据</button>

<div id="resultTable"></div>

<button id="downloadBtn" style="display:none;" onclick="downloadExcel()">下载 Excel</button>

<script>
let dataList = [];
let dataFields = [];
let resultData = [];
const templateFields = [
"条码", "自编码", "名称", "类别编码", "主供应商编码", "进价", "零售价", "批发价",
"会员价", "配送价", "联营扣率", "规格", "单位", "进货规格", "产地", "计价方式",
"是否积分", "前台议价", "前台折扣", "门店变价", "助记码", "经营方式", "品牌编码",
"保质期", "商品类型"
];

document.getElementById('dataFile').addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, {type: 'array'});
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const json = XLSX.utils.sheet_to_json(worksheet);
dataList = json;
dataFields = Object.keys(json[0]);
updateMappingTable();
};
reader.readAsArrayBuffer(file);
});

function updateMappingTable() {
if (dataFields.length > 0) {
let html = '<table><tr><th>字段</th>';
for (let field of templateFields) {
html += `<th>${field}</th>`;
}
html += '</tr><tr><td>模板字段</td>';
for (let field of templateFields) {
html += `<td>${field}</td>`;
}
html += '</tr><tr><td>数据字段</td>';
for (let field of templateFields) {
html += `<td><select id="select_${field}">`;
html += '<option value="">请选择</option>';
for (let dataField of dataFields) {
html += `<option value="${dataField}">${dataField}</option>`;
}
html += '</select></td>';
}
html += '</tr><tr><td>固定值</td>';
for (let field of templateFields) {
html += `<td><input type="text" id="input_${field}"></td>`;
}
html += '</tr></table>';
document.getElementById('mappingTable').innerHTML = html;
}
}

function mapData() {
const mapping = {};
for (let field of templateFields) {
const selectValue = document.getElementById(`select_${field}`).value;
const inputValue = document.getElementById(`input_${field}`).value;
mapping[field] = selectValue || inputValue;
}

resultData = dataList.map(row => {
const newRow = {};
for (let [templateField, mappedField] of Object.entries(mapping)) {
if (mappedField in row) {
newRow[templateField] = row[mappedField];
} else {
newRow[templateField] = mappedField || '';
}
}
return newRow;
});

let html = '<table><tr>';
for (let field of templateFields) {
html += `<th>${field}</th>`;
}
html += '</tr>';
for (let row of resultData.slice(0, 10)) { // 只显示前10行
html += '<tr>';
for (let field of templateFields) {
html += `<td>${row[field] || ''}</td>`;
}
html += '</tr>';
}
html += '</table>';
document.getElementById('resultTable').innerHTML = html;
document.getElementById('downloadBtn').style.display = 'block';
}

function downloadExcel() {
const ws = XLSX.utils.json_to_sheet(resultData);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "mapped_data.xls", { bookType: 'xls', type: 'binary' });
}
</script>
</body>
</html>