Files
Workbench/backend/src/main/webapp/Useradd.html

220 lines
8.5 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Excel Contact Import & Export</title>
<script src="/backend/xlsx/xlsx.full.min.js"></script>
<link rel="stylesheet" href="/backend/ios.css">
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
padding: 20px;
}
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
display: inline-block;
}
button {
margin: 10px;
padding: 10px 20px;
border: none;
background-color: red;
color: white;
border-radius: 5px;
cursor: pointer;
}
#import-table {
margin-top: 20px;
max-width: min(1200px, calc(100vw - 80px));
max-height: 420px;
overflow: auto;
border: 1px solid var(--ios-border, rgba(255,255,255,.14));
border-radius: 14px;
}
#import-table table {
width: max-content;
min-width: 100%;
border-collapse: separate;
border-spacing: 0;
}
#import-table th,
#import-table td {
min-width: 120px;
padding: 9px 12px;
border-right: 1px solid var(--ios-separator, rgba(255,255,255,.1));
border-bottom: 1px solid var(--ios-separator, rgba(255,255,255,.1));
text-align: left;
white-space: nowrap;
}
#import-table th {
position: sticky;
top: 0;
z-index: 2;
background: var(--ios-surface-strong, #151b2c);
}
#import-table td[contenteditable="true"]:focus {
outline: 2px solid var(--ios-blue, #4f7cff);
outline-offset: -2px;
background: rgba(79,124,255,.12);
}
.empty-preview {
padding: 18px;
color: var(--ios-muted, rgba(255,255,255,.6));
}
</style>
</head>
<body>
<div class="container">
<h2>Excel Export & Import</h2>
<button id="export-button">Kontakt Excel Export</button>
<input type="file" id="file-input" accept=".xlsx">
<button id="send-button">Daten Senden</button>
<div id="import-table"></div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
let importedData = [];
let importedHeaders = [];
const url = sessionStorage.getItem("url");
const stationNumber = sessionStorage.getItem("stationNumber");
function exportExcel() {
fetch(url + '/backend/api/getUsers', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ station: stationNumber, })
})
.then(response => response.json())
.then(data => {
if (!data || data.length === 0) {
alert("Keine Daten zum Exportieren.");
return;
}
const headers = Object.keys(data[0]);
const rows = data.map(obj => headers.map(header => obj[header]));
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows]);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Contacts");
XLSX.writeFile(workbook, "UsersExport.xlsx");
})
.catch(error => alert("Fehler beim Exportieren: " + error.message));
}
function renderImportTable() {
const container = document.getElementById("import-table");
container.replaceChildren();
if (!importedHeaders.length || !importedData.length) {
const empty = document.createElement("div");
empty.className = "empty-preview";
empty.textContent = "Keine Importdaten vorhanden.";
container.appendChild(empty);
return;
}
const table = document.createElement("table");
const thead = document.createElement("thead");
const headerRow = document.createElement("tr");
importedHeaders.forEach(header => {
const th = document.createElement("th");
th.textContent = header == null ? "" : String(header);
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement("tbody");
importedData.forEach((row, rowIndex) => {
const tr = document.createElement("tr");
importedHeaders.forEach((_, colIndex) => {
const td = document.createElement("td");
td.contentEditable = "true";
td.textContent = row[colIndex] == null ? "" : String(row[colIndex]);
td.addEventListener("input", () => {
if (!importedData[rowIndex]) importedData[rowIndex] = [];
importedData[rowIndex][colIndex] = td.textContent.trim();
});
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
container.appendChild(table);
}
function importExcelData() {
const fileInput = document.getElementById("file-input");
if (!fileInput.files.length) return;
const reader = new FileReader();
reader.onload = function(event) {
try {
const workbook = XLSX.read(event.target.result, { type: "array" });
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1, defval: "" });
if (jsonData.length < 2) {
importedHeaders = [];
importedData = [];
renderImportTable();
alert("Die Excel-Datei enthält keine Datenzeilen.");
return;
}
importedHeaders = jsonData[0].map(value => value == null ? "" : String(value).trim());
importedData = jsonData.slice(1).filter(row => row.some(value => String(value ?? "").trim() !== ""));
renderImportTable();
} catch (error) {
console.error("Excel-Import fehlgeschlagen:", error);
alert("Fehler beim Einlesen der Excel-Datei: " + error.message);
}
};
reader.readAsArrayBuffer(fileInput.files[0]);
}
function sendExcelData() {
if (!importedHeaders.length || !importedData.length) {
alert("Keine importierten Daten zum Senden vorhanden.");
return;
}
const jsonData = importedData.map(row => {
const obj = {};
importedHeaders.forEach((header, index) => {
obj[header] = row[index] ?? "";
});
return obj;
});
if (!url) {
alert("Backend URL is not defined.");
return;
}
fetch(url + '/backend/api/importUser', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ station: stationNumber, data: jsonData })
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(result => alert("Daten erfolgreich gesendet!"))
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
}
document.getElementById("export-button").addEventListener("click", exportExcel);
document.getElementById("file-input").addEventListener("change", importExcelData);
document.getElementById("send-button").addEventListener("click", sendExcelData);
});
</script>
</body>
</html>