CSV to Table Converter
`], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'exported-table.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Function to convert CSV to HTML table
function convertCSVToTable(csvData, delimiter, hasHeader, tableStyle) {
const rows = csvData.split('\n').filter(row => row.trim() !== '');
if (rows.length === 0) {
throw new Error('CSV data is empty');
}
let tableClass = '';
if (tableStyle === 'striped') {
tableClass = 'class="striped-table"';
} else if (tableStyle === 'bordered') {
tableClass = 'class="bordered-table"';
}
let tableHTML = `
`;
// Process header row if needed
if (hasHeader && rows.length > 0) {
const headerCells = rows[0].split(delimiter);
tableHTML += '';
for (const cell of headerCells) {
tableHTML += `${escapeHTML(cell.trim())} | `;
}
tableHTML += '
';
rows.shift(); // Remove header row from data
}
// Process data rows
tableHTML += '';
for (const row of rows) {
const cells = row.split(delimiter);
tableHTML += '';
for (const cell of cells) {
tableHTML += `${escapeHTML(cell.trim())} | `;
}
tableHTML += '
';
}
tableHTML += '
';
return tableHTML;
}
// Function to escape HTML special characters
function escapeHTML(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Function to show error message
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}
// Add some sample data on first load
if (!csvTextarea.value) {
csvTextarea.value = `Name,Age,City,Occupation
John Doe,28,New York,Software Engineer
Jane Smith,32,Los Angeles,Graphic Designer
Robert Johnson,45,Chicago,Project Manager
Emily Davis,29,Houston,Data Analyst
Michael Brown,36,Miami,Marketing Specialist`;
}
});