-
Notifications
You must be signed in to change notification settings - Fork 919
Expand file tree
/
Copy pathexportExcel_excelJSHTML.html
More file actions
65 lines (58 loc) · 1.66 KB
/
exportExcel_excelJSHTML.html
File metadata and controls
65 lines (58 loc) · 1.66 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
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<script src="https://unpkg.com/exceljs/dist/exceljs.min.js"></script>
<button onclick="generateExcel()">Generate Excel</button>
<script>
function generateExcel() {
var workbook = new ExcelJS.Workbook();
var worksheet = workbook.addWorksheet('My Sheet');
// Sample JSON data
var jsonData = {
"employees": [
{
"name": "John Doe",
"age": 30,
"position": "Manager"
},
{
"name": "Jane Smith",
"age": 25,
"position": "Developer"
},
{
"name": "Bob Johnson",
"age": 45,
"position": "Salesperson"
}
]
};
worksheet.addRow(['Name', 'Age', 'Position']);
jsonData.employees.forEach(function(employee) {
worksheet.addRow([employee.name, employee.age, employee.position]);
});
// Set cell colors
worksheet.getCell('A1').font = {
color:{argb: 'FFFF0000'} // red
};
worksheet.getCell('B1').fill = {
type: 'pattern',
pattern: 'solid',
fgColor: {argb: 'FF00FF00'} // green
};
worksheet.getCell('C1').fill = {
type: 'pattern',
pattern: 'solid',
fgColor: {argb: 'FF0000FF'} // blue
};
// Generate Excel file and download it
workbook.xlsx.writeBuffer().then(function(buffer) {
var blob = new Blob([buffer], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
var url = URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = url;
link.download = 'my_file.xlsx';
link.click();
});
}
</script>
</j:jelly>