xlsx test

This commit is contained in:
Tomas Dvorak
2025-06-11 23:10:35 +02:00
parent 57e0e6b570
commit 39d2ee9eca
+30 -35
View File
@@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - PP Kunovice</title> <title>Admin Dashboard - PP Kunovice</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://cdn.sheetjs.com/xlsx/0.18.5/package/dist/xlsx.full.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script> <script src="https://cdn.tailwindcss.com"></script>
<style> <style>
:root { :root {
@@ -4382,7 +4383,7 @@ function filterReservations() {
displayReservations(filtered); displayReservations(filtered);
} }
// Function to export reservations to CSV with proper column separation // Function to export reservations to XLSX
function exportReservations() { function exportReservations() {
if (!window.allReservations || !window.allReservations.length) { if (!window.allReservations || !window.allReservations.length) {
showNotification('Žádné rezervace k exportu', 'warning'); showNotification('Žádné rezervace k exportu', 'warning');
@@ -4437,47 +4438,41 @@ function exportReservations() {
} }
}; };
// Create CSV content with semicolon delimiter // Prepare data for XLSX
const headers = ['Řidič', 'Vozidlo', 'Od', 'Do', 'Účel']; const headers = ['Řidič', 'Vozidlo', 'Od', 'Do', 'Účel'];
// Create CSV rows // Convert data to worksheet
const csvRows = []; const wsData = [
headers,
...dataToExport.map(res => [
res.driverName || '',
res.vehicle || '',
formatDateTime(res.start) || '',
formatDateTime(res.end) || '',
res.purpose || ''
])
];
// Add header row // Create worksheet
csvRows.push(headers.join(';')); const ws = XLSX.utils.aoa_to_sheet(wsData);
// Add data rows // Set column widths
for (const res of dataToExport) { const colWidths = [
const row = [ { wch: 20 }, // Řidič
`"${String(res.driverName || '').replace(/"/g, '""')}"`, { wch: 25 }, // Vozidlo
`"${String(res.vehicle || '').replace(/"/g, '""')}"`, { wch: 20 }, // Od
`"${formatDateTime(res.start) || ''}"`, { wch: 20 }, // Do
`"${formatDateTime(res.end) || ''}"`, { wch: 40 } // Účel
`"${String(res.purpose || '').replace(/"/g, '""')}"` ];
]; ws['!cols'] = colWidths;
csvRows.push(row.join(';'));
}
// Create CSV string with BOM for Excel // Create workbook
const csvString = '\uFEFF' + csvRows.join('\r\n'); const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Rezervace');
// Create and trigger download // Generate XLSX file
const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8' });
const link = document.createElement('a');
const timestamp = new Date().toISOString().split('T')[0]; const timestamp = new Date().toISOString().split('T')[0];
XLSX.writeFile(wb, `rezervace_${timestamp}.xlsx`);
link.href = URL.createObjectURL(blob);
link.download = `rezervace_${timestamp}.csv`;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
// Clean up
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}, 100);
} }
// Helper function to format date and time // Helper function to format date and time