This commit is contained in:
rizrmd 2024-06-22 07:54:47 +00:00
parent 40857be95c
commit 7e30a03672
3 changed files with 90 additions and 90 deletions

View File

@ -1,23 +1,21 @@
import { useLocal } from "@/utils/use-local";
import { FC, MouseEvent } from "react";
import ExcelJS from "exceljs";
// import ExcelJS from "exceljs";
export const ExportExcel: FC<{
data: any[],
fileName?: string
}> = ({
data, fileName = "exported_data.xlsx"
}): JSX.Element => {
data: any[];
fileName?: string;
}> = ({ data, fileName = "exported_data.xlsx" }): JSX.Element => {
const local = useLocal({
data: [] as any[]
data: [] as any[],
});
local.data = data;
local.render();
const getAllKeys = (arr: Array<Record<string, any>>): string[] => {
const keysSet = new Set<string>();
arr.forEach(obj => {
Object.keys(obj).forEach(key => keysSet.add(key));
arr.forEach((obj) => {
Object.keys(obj).forEach((key) => keysSet.add(key));
});
return Array.from(keysSet);
@ -52,27 +50,27 @@ export const ExportExcel: FC<{
const handleExport = async (e: MouseEvent<HTMLButtonElement>) => {
try {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("Sheet 1");
// const workbook = new ExcelJS.Workbook();
// const worksheet = workbook.addWorksheet("Sheet 1");
const columns = getAllKeys(local.data);
worksheet.addRow(columns);
// const columns = getAllKeys(local.data);
// worksheet.addRow(columns);
local.data.forEach((row) => {
const values = columns.map((col) => row[col]);
worksheet.addRow(values);
});
// local.data.forEach((row) => {
// const values = columns.map((col) => row[col]);
// worksheet.addRow(values);
// });
const buffer = await workbook.xlsx.writeBuffer();
// const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
// const blob = new Blob([buffer], {
// type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// });
const a = document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.download = "my-exported-data.xlsx";
a.click();
// const a = document.createElement("a");
// a.href = window.URL.createObjectURL(blob);
// a.download = "my-exported-data.xlsx";
// a.click();
console.log("Data exported");
} catch (error) {
@ -82,7 +80,9 @@ export const ExportExcel: FC<{
return (
<div>
<button onClick={handleExport} style={{ background: '#00ffff' }}>Export</button>
<button onClick={handleExport} style={{ background: "#00ffff" }}>
Export
</button>
</div>
);
};

View File

@ -1,10 +1,10 @@
import type * as exceljs from "exceljs";
export type ExcelJS = typeof exceljs;
const w = window as unknown as {
_exceljs: any;
};
export const importExcelJs = async (): Promise<ExcelJS> => {
if (w._exceljs) return w._exceljs;
w._exceljs = await import("exceljs");
return w._exceljs;
};
// import type * as exceljs from "exceljs";
// export type ExcelJS = typeof exceljs;
// const w = window as unknown as {
// _exceljs: any;
// };
// export const importExcelJs = async (): Promise<ExcelJS> => {
// if (w._exceljs) return w._exceljs;
// // w._exceljs = await import("exceljs");
// return w._exceljs;
// };

View File

@ -3,7 +3,7 @@ import { GFCol, createItem, parseGenField } from "../utils";
import { on_load } from "./on_load";
import { codeBuild, codeBuildTest } from "../master_detail/utils";
// import * as Excel from "exceljs";
import ExcelJS from "exceljs";
// import ExcelJS from "exceljs";
export const gen_export = async (
modify: (data: any) => void,
@ -52,22 +52,22 @@ export const gen_export = async (
`SELECT ${selectFields} FROM ${tableName};`
);
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("Sheet 1");
// const workbook = new ExcelJS.Workbook();
// const worksheet = workbook.addWorksheet("Sheet 1");
const columns = Object.keys(result[0]);
worksheet.addRow(columns);
// const columns = Object.keys(result[0]);
// worksheet.addRow(columns);
result.forEach((row) => {
const values = columns.map((col) => row[col]);
worksheet.addRow(values);
});
// result.forEach((row) => {
// const values = columns.map((col) => row[col]);
// worksheet.addRow(values);
// });
const buffer = await workbook.xlsx.writeBuffer();
// const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
// const blob = new Blob([buffer], {
// type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// });
// FileSaver.saveAs(blob, "exported_data.xlsx");