xlsx test

This commit is contained in:
Tomas Dvorak
2025-06-11 23:10:35 +02:00
parent 57e0e6b570
commit 39d2ee9eca
+32 -37
View File
@@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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">
<script src="https://cdn.sheetjs.com/xlsx/0.18.5/package/dist/xlsx.full.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style>
:root {
@@ -4382,7 +4383,7 @@ function filterReservations() {
displayReservations(filtered);
}
// Function to export reservations to CSV with proper column separation
// Function to export reservations to XLSX
function exportReservations() {
if (!window.allReservations || !window.allReservations.length) {
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'];
// Create CSV rows
const csvRows = [];
// Add header row
csvRows.push(headers.join(';'));
// Add data rows
for (const res of dataToExport) {
const row = [
`"${String(res.driverName || '').replace(/"/g, '""')}"`,
`"${String(res.vehicle || '').replace(/"/g, '""')}"`,
`"${formatDateTime(res.start) || ''}"`,
`"${formatDateTime(res.end) || ''}"`,
`"${String(res.purpose || '').replace(/"/g, '""')}"`
];
csvRows.push(row.join(';'));
}
// Convert data to worksheet
const wsData = [
headers,
...dataToExport.map(res => [
res.driverName || '',
res.vehicle || '',
formatDateTime(res.start) || '',
formatDateTime(res.end) || '',
res.purpose || ''
])
];
// Create CSV string with BOM for Excel
const csvString = '\uFEFF' + csvRows.join('\r\n');
// Create worksheet
const ws = XLSX.utils.aoa_to_sheet(wsData);
// Create and trigger download
const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8' });
const link = document.createElement('a');
// Set column widths
const colWidths = [
{ wch: 20 }, // Řidič
{ wch: 25 }, // Vozidlo
{ wch: 20 }, // Od
{ wch: 20 }, // Do
{ wch: 40 } // Účel
];
ws['!cols'] = colWidths;
// Create workbook
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Rezervace');
// Generate XLSX file
const timestamp = new Date().toISOString().split('T')[0];
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);
XLSX.writeFile(wb, `rezervace_${timestamp}.xlsx`);
}
// Helper function to format date and time