-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
65 lines (53 loc) · 2.8 KB
/
index.html
File metadata and controls
65 lines (53 loc) · 2.8 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manipulação de Planilhas Excel</title>
</head>
<body>
<input type="file" onchange="handleFiles(event)" multiple>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
<script>
function handleFiles(event) {
const files = event.target.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const reader = new FileReader();
reader.onload = function(event) {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, { type: 'array' });
workbook.SheetNames.forEach(sheetName => {
const sheet = workbook.Sheets[sheetName];
// Obter o nome da tabela (nome da aba da planilha)
const tableName = sheetName.replace(/\s+/g, '_'); // Remover espaços e substituir por underscores
// Converter a planilha para um objeto JSON
const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });
// Obter os nomes das colunas da planilha (cabeçalhos)
const columns = jsonData[0];
// Criar uma string de instrução SQL de INSERT
const insertSQL = jsonData.slice(1).map(row => {
const values = row.map(value => {
if (value === '' || typeof value === 'undefined') {
return 'NULL'; // Substituir por NULL se o valor for uma string vazia ou indefinido
} else {
return typeof value === 'string' ? `'${value}'` : value;
}
}).join(', ');
return `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${values});`;
}).join('\n');
// Criar um blob com o texto do SQL
const blob = new Blob([insertSQL], { type: 'text/plain' });
// Criar um link de download e atribuí-lo ao documento
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = `${tableName}.sql`;
link.click();
});
};
reader.readAsArrayBuffer(file);
}
}
</script>
</body>
</html>