fix
This commit is contained in:
parent
40857be95c
commit
7e30a03672
|
|
@ -1,23 +1,21 @@
|
||||||
import { useLocal } from "@/utils/use-local";
|
import { useLocal } from "@/utils/use-local";
|
||||||
import { FC, MouseEvent } from "react";
|
import { FC, MouseEvent } from "react";
|
||||||
import ExcelJS from "exceljs";
|
// import ExcelJS from "exceljs";
|
||||||
|
|
||||||
export const ExportExcel: FC<{
|
export const ExportExcel: FC<{
|
||||||
data: any[],
|
data: any[];
|
||||||
fileName?: string
|
fileName?: string;
|
||||||
}> = ({
|
}> = ({ data, fileName = "exported_data.xlsx" }): JSX.Element => {
|
||||||
data, fileName = "exported_data.xlsx"
|
|
||||||
}): JSX.Element => {
|
|
||||||
const local = useLocal({
|
const local = useLocal({
|
||||||
data: [] as any[]
|
data: [] as any[],
|
||||||
});
|
});
|
||||||
local.data = data;
|
local.data = data;
|
||||||
local.render();
|
local.render();
|
||||||
const getAllKeys = (arr: Array<Record<string, any>>): string[] => {
|
const getAllKeys = (arr: Array<Record<string, any>>): string[] => {
|
||||||
const keysSet = new Set<string>();
|
const keysSet = new Set<string>();
|
||||||
|
|
||||||
arr.forEach(obj => {
|
arr.forEach((obj) => {
|
||||||
Object.keys(obj).forEach(key => keysSet.add(key));
|
Object.keys(obj).forEach((key) => keysSet.add(key));
|
||||||
});
|
});
|
||||||
|
|
||||||
return Array.from(keysSet);
|
return Array.from(keysSet);
|
||||||
|
|
@ -52,27 +50,27 @@ export const ExportExcel: FC<{
|
||||||
|
|
||||||
const handleExport = async (e: MouseEvent<HTMLButtonElement>) => {
|
const handleExport = async (e: MouseEvent<HTMLButtonElement>) => {
|
||||||
try {
|
try {
|
||||||
const workbook = new ExcelJS.Workbook();
|
// const workbook = new ExcelJS.Workbook();
|
||||||
const worksheet = workbook.addWorksheet("Sheet 1");
|
// const worksheet = workbook.addWorksheet("Sheet 1");
|
||||||
|
|
||||||
const columns = getAllKeys(local.data);
|
// const columns = getAllKeys(local.data);
|
||||||
worksheet.addRow(columns);
|
// worksheet.addRow(columns);
|
||||||
|
|
||||||
local.data.forEach((row) => {
|
// local.data.forEach((row) => {
|
||||||
const values = columns.map((col) => row[col]);
|
// const values = columns.map((col) => row[col]);
|
||||||
worksheet.addRow(values);
|
// worksheet.addRow(values);
|
||||||
});
|
// });
|
||||||
|
|
||||||
const buffer = await workbook.xlsx.writeBuffer();
|
// const buffer = await workbook.xlsx.writeBuffer();
|
||||||
|
|
||||||
const blob = new Blob([buffer], {
|
// const blob = new Blob([buffer], {
|
||||||
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
// type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
});
|
// });
|
||||||
|
|
||||||
const a = document.createElement("a");
|
// const a = document.createElement("a");
|
||||||
a.href = window.URL.createObjectURL(blob);
|
// a.href = window.URL.createObjectURL(blob);
|
||||||
a.download = "my-exported-data.xlsx";
|
// a.download = "my-exported-data.xlsx";
|
||||||
a.click();
|
// a.click();
|
||||||
|
|
||||||
console.log("Data exported");
|
console.log("Data exported");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -82,7 +80,9 @@ export const ExportExcel: FC<{
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<button onClick={handleExport} style={{ background: '#00ffff' }}>Export</button>
|
<button onClick={handleExport} style={{ background: "#00ffff" }}>
|
||||||
|
Export
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
import type * as exceljs from "exceljs";
|
// import type * as exceljs from "exceljs";
|
||||||
export type ExcelJS = typeof exceljs;
|
// export type ExcelJS = typeof exceljs;
|
||||||
const w = window as unknown as {
|
// const w = window as unknown as {
|
||||||
_exceljs: any;
|
// _exceljs: any;
|
||||||
};
|
// };
|
||||||
export const importExcelJs = async (): Promise<ExcelJS> => {
|
// export const importExcelJs = async (): Promise<ExcelJS> => {
|
||||||
if (w._exceljs) return w._exceljs;
|
// if (w._exceljs) return w._exceljs;
|
||||||
w._exceljs = await import("exceljs");
|
// // w._exceljs = await import("exceljs");
|
||||||
return w._exceljs;
|
// return w._exceljs;
|
||||||
};
|
// };
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { GFCol, createItem, parseGenField } from "../utils";
|
||||||
import { on_load } from "./on_load";
|
import { on_load } from "./on_load";
|
||||||
import { codeBuild, codeBuildTest } from "../master_detail/utils";
|
import { codeBuild, codeBuildTest } from "../master_detail/utils";
|
||||||
// import * as Excel from "exceljs";
|
// import * as Excel from "exceljs";
|
||||||
import ExcelJS from "exceljs";
|
// import ExcelJS from "exceljs";
|
||||||
|
|
||||||
export const gen_export = async (
|
export const gen_export = async (
|
||||||
modify: (data: any) => void,
|
modify: (data: any) => void,
|
||||||
|
|
@ -52,22 +52,22 @@ export const gen_export = async (
|
||||||
`SELECT ${selectFields} FROM ${tableName};`
|
`SELECT ${selectFields} FROM ${tableName};`
|
||||||
);
|
);
|
||||||
|
|
||||||
const workbook = new ExcelJS.Workbook();
|
// const workbook = new ExcelJS.Workbook();
|
||||||
const worksheet = workbook.addWorksheet("Sheet 1");
|
// const worksheet = workbook.addWorksheet("Sheet 1");
|
||||||
|
|
||||||
const columns = Object.keys(result[0]);
|
// const columns = Object.keys(result[0]);
|
||||||
worksheet.addRow(columns);
|
// worksheet.addRow(columns);
|
||||||
|
|
||||||
result.forEach((row) => {
|
// result.forEach((row) => {
|
||||||
const values = columns.map((col) => row[col]);
|
// const values = columns.map((col) => row[col]);
|
||||||
worksheet.addRow(values);
|
// worksheet.addRow(values);
|
||||||
});
|
// });
|
||||||
|
|
||||||
const buffer = await workbook.xlsx.writeBuffer();
|
// const buffer = await workbook.xlsx.writeBuffer();
|
||||||
|
|
||||||
const blob = new Blob([buffer], {
|
// const blob = new Blob([buffer], {
|
||||||
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
// type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
});
|
// });
|
||||||
|
|
||||||
// FileSaver.saveAs(blob, "exported_data.xlsx");
|
// FileSaver.saveAs(blob, "exported_data.xlsx");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue