JSFiddle - React, Tailwind, and code Playground
Export and print
by Flexmonster Pivot Table
HTML
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<link rel="stylesheet" href="https://cdn.flexmonster.com/assets/jsfiddle-styles.css" />
<!-- xlsx library -->
<!-- For downloading Excel files -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.min.js" integrity="sha512-W/mRQs9ZSFpF14X/4aRgQss7+HRsVXsph+Y6DGLeqIqK8IpO+rQz0ISUEXkTeeKF7tivoGv+Ru7SpocS/1qahg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<button onclick="excel()">Export to Excel</button>
<div id="pivot-container"></div>
CSS
.link {
text-decoration: underline !important;
cursor: pointer;
color: #00a45a !important;
}
JavaScript
const pivot = new Flexmonster({
container: "pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
customizeCell: customizeCellFunction,
report: {
dataSource: {
filename: "data/data.csv",
},
slice: {
rows: [
{
uniqueName: "Category",
},
],
columns: [
{
uniqueName: "Country",
},
{
uniqueName: "[Measures]",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
]
},
},
});
function customizeCellFunction(cell, data) {
if (!(data.hierarchy && data.hierarchy.uniqueName == "Country")) return;
let newText =
`<a href='https://en.wikipedia.org/wiki/${data.escapedLabel}' target='_blank' class='link' onclick='preventExpand(event)'>${data.escapedLabel}</a>`;
if (data.type == "header" && data.member) {
let parts = cell.text.split(data.escapedLabel);
if (parts.length == 2) {
newText = parts[0] + newText + parts[1];
}
cell.text = newText;
} else if (data.type == "value" && (data.member || data.isDrillThrough)) {
cell.style['z-index'] = 1;
cell.text = newText;
}
}
function excel() {
const params = {
destinationType: "plain",
filename: "flexmonster",
header: "First header row",
excelSheetName: "Report",
showFilters: true,
};
pivot.exportTo("excel", params, exportHandler);
}
function exportHandler(result) {
const workbook = XLSX.read(result.data.buffer, {
type: "buffer",
});
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Range of used cells
const range = XLSX.utils.decode_range(worksheet["!ref"]);
for (let R = range.s.r; R <= range.e.r; ++R) {
for (let C = range.s.c; C <= range.e.c; ++C) {
const cellAddress = XLSX.utils.encode_cell({ r: R, c: C });
const...