-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
107 lines (89 loc) · 3.37 KB
/
index.html
File metadata and controls
107 lines (89 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV to Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h2>CSV to Table</h2>
<!-- File input for CSV -->
<input type="file" id="csvFile" accept=".csv" />
<button onclick="generateTable()">Generate Table</button>
<!-- Table to display CSV data -->
<table id="csvTable">
<thead>
<!-- Column headers will be generated dynamically -->
</thead>
<tbody>
<!-- Data will be inserted here -->
</tbody>
</table>
<script>
// Function to handle the CSV file and display data
function generateTable() {
// Get the file input
const fileInput = document.getElementById("csvFile");
const file = fileInput.files[0];
if (!file) {
alert("Please upload a CSV file first!");
return;
}
// Create a FileReader to read the CSV file
const reader = new FileReader();
reader.onload = function(event) {
const csvData = event.target.result;
// Split the CSV data into rows
const rows = csvData.split("\n");
const tableBody = document.querySelector("#csvTable tbody");
const tableHead = document.querySelector("#csvTable thead");
// Clear the table before inserting new data
tableBody.innerHTML = "";
tableHead.innerHTML = "";
// Process the first row as headers
const headers = rows[0].split(",");
const headerRow = document.createElement("tr");
// Create table headers dynamically
headers.forEach(header => {
const th = document.createElement("th");
th.textContent = header.trim();
headerRow.appendChild(th);
});
// Append header row to the table
tableHead.appendChild(headerRow);
// Loop through the remaining rows and create table rows
rows.slice(1).forEach(row => {
const columns = row.split(",");
if (columns.length === headers.length) { // Ensure correct number of columns
const tr = document.createElement("tr");
// Create and append each cell (td) to the row (tr)
columns.forEach(col => {
const td = document.createElement("td");
td.textContent = col.trim();
tr.appendChild(td);
});
// Append the row to the table body
tableBody.appendChild(tr);
}
});
};
// Read the CSV file as text
reader.readAsText(file);
}
</script>
</body>
</html>