-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-to-xslx.html
330 lines (299 loc) · 10.5 KB
/
csv-to-xslx.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV to Excel Converter</title>
<style>
/* Dark Theme Styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #121212;
color: #e0e0e0;
height: 100vh;
display: flex;
flex-direction: column;
}
.container {
flex: 1;
max-width: 800px;
margin: auto;
padding: 30px;
background-color: #1e1e1e;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.7);
position: relative;
}
h1 {
text-align: center;
color: #ffffff;
}
.description {
margin: 20px 0;
color: #b0b0b0;
line-height: 1.6;
}
.file-info {
text-align: center;
margin: 20px 0;
color: #cfcfcf;
font-size: 1.1em;
}
#dropOverlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(40, 167, 69, 0.2);
display: none;
align-items: center;
justify-content: center;
z-index: 1000;
color: #28a745;
font-size: 2em;
pointer-events: none; /* to prevent event bleed through causing dragleave events and subsequent flicker of dropOverlay */
}
#message {
text-align: center;
color: #b0b0b0;
margin-top: 10px;
}
footer {
padding: 20px;
text-align: center;
color: #888;
font-size: 14px;
background-color: #1e1e1e;
}
a {
color: #28a745;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
#loadButtonContainer {
display: flex;
justify-content: center;
}
.load-button {
padding: 12px 25px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s;
margin-top: 20px;
text-align: center;
}
.load-button:hover {
background-color: #218838;
}
/* Hidden File Input */
#csvFileInput {
display: none;
}
/* Responsive Adjustments */
@media (max-width: 600px) {
.container {
padding: 20px;
}
h1 {
font-size: 1.8em;
}
.load-button {
width: 100%;
text-align: center;
}
}
</style>
</head>
<body>
<!-- Drop Overlay -->
<div id="dropOverlay">Drop here to load a CSV file</div>
<div class="container">
<h1>CSV to Excel Converter</h1>
<!-- Description Section -->
<div class="description">
<p>Convert your comma-separated values (CSV) files into Excel spreadsheets (.xlsx) effortlessly.</p>
<p>Drag and drop a CSV file, paste CSV data directly, or use the load button below. Your Excel file will be ready instantly!</p>
<p><strong>Privacy Guaranteed:</strong> All processing happens locally in your browser. Your data is never sent to any server, ensuring complete privacy and security.</p>
</div>
<!-- File Information Display -->
<div class="file-info" id="fileInfo">
No file or data loaded.
</div>
<!-- Load Button -->
<div id="loadButtonContainer">
<button class="load-button">Load CSV File</button>
<input type="file" id="csvFileInput" accept=".csv" style="display: none;" />
</div>
<!-- Message Display -->
<div id="message"></div>
</div>
<!-- Footer with Explanation -->
<footer>
<p>
This application runs entirely in your browser using JavaScript and the <a href="https://sheetjs.com/" target="_blank" rel="noopener noreferrer">SheetJS</a> library. No data is transmitted or stored externally.
</p>
</footer>
<!-- Include the Latest SheetJS Library -->
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.mini.min.js"></script>
<script>
// Get DOM Elements
const csvFileInput = document.getElementById('csvFileInput');
const fileInfo = document.getElementById('fileInfo');
const messageDiv = document.getElementById('message');
const dropOverlay = document.getElementById('dropOverlay');
const loadButton = document.querySelector('.load-button');
let csvData = '';
/**
* Function to handle CSV data input
* @param {string} data - The CSV data as a string
* @param {string} [filename] - Optional filename for saved Excel
*/
function handleCSVData(data, filename = 'converted.xlsx') {
if (data.trim() !== '') {
csvData = data;
messageDiv.textContent = 'Converting to Excel...';
convertCSVToExcel(csvData, filename);
} else {
messageDiv.textContent = 'Invalid CSV data provided.';
}
}
/**
* Function to read file as text
* @param {File} file - The file to read
*/
function readFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
handleCSVData(content, file.name.replace(/\.csv$/i, '.xlsx'));
fileInfo.textContent = `Loaded file: ${file.name}`;
};
reader.onerror = function() {
messageDiv.textContent = 'Error reading file.';
fileInfo.textContent = 'No file or data loaded.';
};
reader.readAsText(file);
}
/**
* Function to read pasted text or file
* @param {ClipboardEvent} e - The paste event
*/
function handlePaste(e) {
const items = (e.clipboardData || window.clipboardData).items;
let hasFile = false;
for (const element of items) {
if (element.kind === 'file') {
const file = element.getAsFile();
if (file && file.type === 'text/csv') {
hasFile = true;
readFile(file);
break;
}
}
}
if (!hasFile) {
const pastedData = (e.clipboardData || window.clipboardData).getData('text');
if (pastedData.trim() !== '') {
handleCSVData(pastedData);
fileInfo.textContent = 'CSV data pasted.';
} else {
messageDiv.textContent = 'No valid data found in paste.';
}
}
}
/**
* Function to Convert CSV to Excel
* @param {string} csv - The CSV data as a string
* @param {string} filename - The desired filename for the Excel file
*/
function convertCSVToExcel(csv, filename) {
try {
// Parse CSV data
const workbook = XLSX.read(csv, { type: 'string' });
// Generate Excel file
XLSX.writeFile(workbook, filename);
messageDiv.textContent = `Conversion successful! "${filename}" has been saved.`;
} catch (error) {
console.error(error);
messageDiv.textContent = 'An error occurred during conversion.';
}
}
/**
* Function to show the drop overlay
*/
function showDropOverlay() {
dropOverlay.style.display = 'flex';
}
/**
* Function to hide the drop overlay
*/
function hideDropOverlay() {
dropOverlay.style.display = 'none';
}
/**
* Handle Drag Events for Full Page Drop
*/
['dragenter', 'dragover'].forEach(eventName => {
window.addEventListener(eventName, (e) => {
e.preventDefault();
e.stopPropagation();
showDropOverlay();
}, false);
});
['dragleave', 'drop'].forEach(eventName => {
window.addEventListener(eventName, (e) => {
e.preventDefault();
e.stopPropagation();
hideDropOverlay();
}, false);
});
/**
* Handle Drop Event
*/
window.addEventListener('drop', (e) => {
const files = e.dataTransfer.files;
if (files.length > 0) {
const file = files[0];
if (file.type === 'text/csv') {
readFile(file);
} else {
messageDiv.textContent = 'Please drop a valid CSV file.';
fileInfo.textContent = 'No file or data loaded.';
}
}
});
/**
* Event Listener for Load Button
*/
loadButton.addEventListener('click', () => {
csvFileInput.click();
});
/**
* Event Listener for File Input
*/
csvFileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file && file.type === 'text/csv') {
readFile(file);
} else {
messageDiv.textContent = 'Please select a valid CSV file.';
fileInfo.textContent = 'No file or data loaded.';
}
});
/**
* Event Listener for Paste
*/
window.addEventListener('paste', handlePaste);
</script>
</body>
</html>