-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (31 loc) · 1.54 KB
/
script.js
File metadata and controls
39 lines (31 loc) · 1.54 KB
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
function handleFile() {
const fileInput = document.getElementById('fileInput');
const outputDiv = document.getElementById('output');
const file = fileInput.files[0];
if (!file) {
outputDiv.innerHTML = 'Nenhum arquivo selecionado.';
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0]; // Assumindo que a primeira planilha seja a desejada
const worksheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
const tableName = 'minha_tabela';
const columns = jsonData[0];
const insertSQL = jsonData.slice(1).map(row => {
const values = row.map(value => typeof value === 'string' ? `'${value}'` : value).join(', ');
return `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${values});`;
}).join('\n');
outputDiv.innerHTML = '<h2>Script SQL:</h2><textarea cols="80" rows="10">' + insertSQL + '</textarea>';
// Criar link para download
const downloadLink = document.createElement('a');
downloadLink.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(insertSQL);
downloadLink.download = 'script_insert.sql';
downloadLink.textContent = 'Download Script SQL';
outputDiv.appendChild(downloadLink);
};
reader.readAsArrayBuffer(file);
}