feat: enhance Excel table header and register styling with Navy fill, white bold font, borders, and AutoFilter

This commit is contained in:
Atharv1705
2026-11-16 10:57:16 +05:30
parent 5d98aa57cb
commit 13fd84a53c
6 changed files with 226 additions and 12 deletions
+96
View File
@@ -34,6 +34,7 @@
"recharts": "^2.13.3",
"tailwind-merge": "^2.5.4",
"xlsx": "^0.18.5",
"xlsx-js-style": "^1.2.0",
"zod": "^3.23.8",
"zustand": "^4.5.5"
},
@@ -4700,6 +4701,15 @@
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==",
"license": "MIT"
},
"node_modules/exit-on-epipe": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/exit-on-epipe/-/exit-on-epipe-1.0.1.tgz",
"integrity": "sha512-h2z5mrROTxce56S+pnvAV890uu7ls7f1kEvVGJbw1OlFH3/mlJ5bkXu0KRyW94v37zzHPiUd55iLn3DA7TjWpw==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/expect-type": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",
@@ -4798,6 +4808,12 @@
}
}
},
"node_modules/fflate": {
"version": "0.3.11",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.3.11.tgz",
"integrity": "sha512-Rr5QlUeGN1mbOHlaqcSYMKVpPbgLy0AWT/W0EHxA6NGI12yO1jpoui2zBBvU2G824ltM6Ut8BFgfHSBGfkmS0A==",
"license": "MIT"
},
"node_modules/file-entry-cache": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
@@ -6070,6 +6086,18 @@
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/printj": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/printj/-/printj-1.1.2.tgz",
"integrity": "sha512-zA2SmoLaxZyArQTOPj5LXecR+RagfPSU5Kw1qP+jkWeNlrq+eJZyY2oS68SU1Z/7/myXM4lo9716laOFAVStCQ==",
"license": "Apache-2.0",
"bin": {
"printj": "bin/printj.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/prop-types": {
"version": "15.8.1",
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
@@ -7419,6 +7447,74 @@
"node": ">=0.8"
}
},
"node_modules/xlsx-js-style": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/xlsx-js-style/-/xlsx-js-style-1.2.0.tgz",
"integrity": "sha512-DDT4FXFSWfT4DXMSok/m3TvmP1gvO3dn0Eu/c+eXHW5Kzmp7IczNkxg/iEPnImbG9X0Vb8QhROda5eatSR/97Q==",
"license": "Apache-2.0",
"dependencies": {
"adler-32": "~1.2.0",
"cfb": "^1.1.4",
"codepage": "~1.14.0",
"commander": "~2.17.1",
"crc-32": "~1.2.0",
"exit-on-epipe": "~1.0.1",
"fflate": "^0.3.8",
"ssf": "~0.11.2",
"wmf": "~1.0.1",
"word": "~0.3.0"
},
"bin": {
"xlsx": "bin/xlsx.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/xlsx-js-style/node_modules/adler-32": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/adler-32/-/adler-32-1.2.0.tgz",
"integrity": "sha512-/vUqU/UY4MVeFsg+SsK6c+/05RZXIHZMGJA+PX5JyWI0ZRcBpupnRuPLU/NXXoFwMYCPCoxIfElM2eS+DUXCqQ==",
"license": "Apache-2.0",
"dependencies": {
"exit-on-epipe": "~1.0.1",
"printj": "~1.1.0"
},
"bin": {
"adler32": "bin/adler32.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/xlsx-js-style/node_modules/codepage": {
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/codepage/-/codepage-1.14.0.tgz",
"integrity": "sha512-iz3zJLhlrg37/gYRWgEPkaFTtzmnEv1h+r7NgZum2lFElYQPi0/5bnmuDfODHxfp0INEfnRqyfyeIJDbb7ahRw==",
"license": "Apache-2.0",
"dependencies": {
"commander": "~2.14.1",
"exit-on-epipe": "~1.0.1"
},
"bin": {
"codepage": "bin/codepage.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/xlsx-js-style/node_modules/codepage/node_modules/commander": {
"version": "2.14.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.14.1.tgz",
"integrity": "sha512-+YR16o3rK53SmWHU3rEM3tPAh2rwb1yPcQX5irVn7mb0gXbwuCCrnkbV5+PBfETdfg1vui07nM6PCG1zndcjQw==",
"license": "MIT"
},
"node_modules/xlsx-js-style/node_modules/commander": {
"version": "2.17.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.17.1.tgz",
"integrity": "sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg==",
"license": "MIT"
},
"node_modules/xml-name-validator": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-5.0.0.tgz",
+1
View File
@@ -37,6 +37,7 @@
"recharts": "^2.13.3",
"tailwind-merge": "^2.5.4",
"xlsx": "^0.18.5",
"xlsx-js-style": "^1.2.0",
"zod": "^3.23.8",
"zustand": "^4.5.5"
},
@@ -1,14 +1,20 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { ColumnDef } from "@tanstack/react-table";
import * as XLSX from "xlsx";
import XLSX from "xlsx-js-style";
import { DataTable } from "./DataTable";
vi.mock("xlsx", async () => {
const actual = await vi.importActual<typeof import("xlsx")>("xlsx");
vi.mock("xlsx-js-style", async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
const base = ((actual as { default?: Record<string, unknown> }).default ?? actual) as Record<string, unknown>;
const writeFileMock = vi.fn();
return {
...actual,
writeFile: vi.fn(),
...base,
writeFile: writeFileMock,
default: {
...base,
writeFile: writeFileMock,
},
};
});
@@ -14,7 +14,7 @@ import {
type Table as TanStackTable,
} from "@tanstack/react-table";
import { useState } from "react";
import * as XLSX from "xlsx";
import XLSX from "xlsx-js-style";
import {
ChevronUp,
ChevronDown,
@@ -69,6 +69,75 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
const ws = XLSX.utils.json_to_sheet(exportRows);
// Apply rich executive styling to table header row (Navy fill, bold white font, borders)
for (let c = 0; c < visibleCols.length; c++) {
const headerAddr = XLSX.utils.encode_cell({ r: 0, c });
if (ws[headerAddr]) {
ws[headerAddr].s = {
fill: { fgColor: { rgb: "1E3A8A" } }, // Poona Hospital Deep Navy
font: { name: "Calibri", sz: 11, bold: true, color: { rgb: "FFFFFF" } },
alignment: { horizontal: "center", vertical: "center", wrapText: true },
border: {
top: { style: "thin", color: { rgb: "334155" } },
bottom: { style: "medium", color: { rgb: "0F172A" } },
left: { style: "thin", color: { rgb: "334155" } },
right: { style: "thin", color: { rgb: "334155" } },
},
};
}
}
// Apply zebra striping, borders, and column-aware alignment to data rows
for (let r = 0; r < rows.length; r++) {
const isOdd = r % 2 === 1;
for (let c = 0; c < visibleCols.length; c++) {
const cellAddr = XLSX.utils.encode_cell({ r: r + 1, c });
if (!ws[cellAddr]) continue;
const col = visibleCols[c];
const headerLabel = (typeof col.columnDef.header === "string" ? col.columnDef.header : col.id).toLowerCase();
const val = rows[r].getValue(col.id);
let horizontalAlign: "left" | "center" | "right" = "left";
if (
typeof val === "number" ||
headerLabel.includes("cost") ||
headerLabel.includes("value") ||
headerLabel.includes("amount") ||
headerLabel.includes("price") ||
headerLabel.includes("minutes") ||
headerLabel.includes("downtime") ||
headerLabel.includes("total")
) {
horizontalAlign = "right";
} else if (
headerLabel.includes("code") ||
headerLabel.includes("date") ||
headerLabel.includes("status") ||
headerLabel.includes("priority") ||
headerLabel.includes("criticality") ||
headerLabel.includes("serial") ||
headerLabel.includes("mode") ||
headerLabel.includes("id") ||
typeof val === "boolean"
) {
horizontalAlign = "center";
}
ws[cellAddr].s = {
fill: { fgColor: { rgb: isOdd ? "F8FAFC" : "FFFFFF" } },
font: { name: "Calibri", sz: 10, color: { rgb: "0F172A" } },
alignment: { horizontal: horizontalAlign, vertical: "center" },
border: {
top: { style: "thin", color: { rgb: "E2E8F0" } },
bottom: { style: "thin", color: { rgb: "E2E8F0" } },
left: { style: "thin", color: { rgb: "E2E8F0" } },
right: { style: "thin", color: { rgb: "E2E8F0" } },
},
};
}
}
// Auto-fit column widths with generous padding so text/codes are never truncated
const colWidths = visibleCols.map((col) => {
const headerLabel =
@@ -85,9 +154,9 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
});
ws["!cols"] = colWidths;
// Add comfortable row heights: header row taller (26pt), data rows spacious (20pt)
// Add comfortable row heights: header row taller (28pt), data rows spacious (20pt)
ws["!rows"] = [
{ hpt: 26 },
{ hpt: 28 },
...rows.map(() => ({ hpt: 20 })),
];