const fs = require('fs'); const readline = require('readline'); class CsvToHtmlConverter { /** * Erstellt eine HTML-Tabelle aus einer CSV-Datei * @param {string} csvFilePath Pfad zur CSV-Datei * @returns {Promise} HTML-Tabelle als String */ async convertToHtml(csvFilePath) { if (!fs.existsSync(csvFilePath)) { throw new Error(`CSV file not found: ${csvFilePath}`); } const fileStream = fs.createReadStream(csvFilePath); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity, }); let html = this.getHtmlHeader(); let isFirstLine = true; for await (const line of rl) { const cells = this.parseCsvLine(line); if (isFirstLine) { html += ''; cells.forEach(cell => { html += `${this.escapeHtml(cell)}`; }); html += ''; isFirstLine = false; } else { html += ''; cells.forEach(cell => { html += `${this.escapeHtml(cell)}`; }); html += ''; } } html += ''; return html; } /** * Gibt das HTML- und CSS-Headergerüst für die Tabelle zurück */ getHtmlHeader() { return ` `; } /** * Wandelt eine CSV-Zeile in Felder um * @param {string} line * @returns {string[]} */ parseCsvLine(line) { // Für einfache Fälle (Kommas, kein Quote-Parsing) return line.split(',').map(field => field.trim()); } /** * Wandelt Sonderzeichen in HTML-Entities um * @param {string} text * @returns {string} */ escapeHtml(text) { return String(text) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } } module.exports = CsvToHtmlConverter;