Dateien nach "backend/src/main/webapp" hochladen

This commit is contained in:
2026-10-13 15:14:18 +02:00
parent dbb1916544
commit 5c62bef387

View File

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