优化excel导出

master
LiuJiaNan 2025-12-30 15:42:47 +08:00
parent 14aa36fb66
commit 85d7346394
3 changed files with 289 additions and 257 deletions

View File

@ -31,7 +31,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-to-print": "^3.2.0",
"zy-react-library": "^1.1.10"
"zy-react-library": "^1.1.12"
},
"devDependencies": {
"@antfu/eslint-config": "^5.4.1",

View File

@ -1,8 +1,8 @@
import { Permission } from "@cqsjjb/jjb-common-decorator/permission";
import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Button, Form, message, Modal, Space, Spin } from "antd";
import { Button, Checkbox, Form, message, Modal, Space } from "antd";
import dayjs from "dayjs";
import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useReactToPrint } from "react-to-print";
import FormBuilder from "zy-react-library/components/FormBuilder";
import AddIcon from "zy-react-library/components/Icon/AddIcon";
@ -18,7 +18,7 @@ import { FORM_ITEM_RENDER_ENUM } from "zy-react-library/enum/formItemRender";
import { HIDDEN_RECTIFICATION_TYPE_ENUM, HIDDEN_SOURCE_ENUM, HIDDEN_STATE_ENUM } from "zy-react-library/enum/hidden/gwj";
import useDownloadBlob from "zy-react-library/hooks/useDownloadBlob";
import useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { getLabelName, getUnmatchedItems } from "zy-react-library/utils";
import { IS_RELATED_ENUM } from "~/enumerate/constant";
import { NS_LEDGER } from "~/enumerate/namespace";
import "./index.less";
@ -28,7 +28,6 @@ function List(props) {
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
const [exportByColumnModalOpen, setExportByColumnModalOpen] = useState(false);
const [printModalOpen, setPrintModalOpen] = useState(false);
const { loading: downloadBlobLoading, downloadBlob } = useDownloadBlob();
const { tableProps, getData } = useTable(props["ledgerList"], {
form,
transform: formData => ({
@ -51,31 +50,8 @@ function List(props) {
});
};
const onExportExcel = async () => {
const hiddenFindTime = form.getFieldValue("hiddenFindTime");
if (!hiddenFindTime) {
message.error("请选择隐患发现时间");
return;
}
if (hiddenFindTime.filter(Boolean).length === 0) {
message.error("请选择隐患发现时间");
return;
}
Modal.confirm({
title: "导出确认",
content: "确定要导出excel吗",
onOk: async () => {
await downloadBlob("/hidden/hidden/exportHidden", {
params: { hiddenFindTime: hiddenFindTime[0], hiddenFindTimeLe: hiddenFindTime[1] },
});
message.success("导出成功");
},
});
};
return (
<Page isShowAllAction={false}>
<Spin spinning={downloadBlobLoading}>
<Search
options={[
{ name: "source", label: "隐患来源", render: FORM_ITEM_RENDER_ENUM.SELECT, items: HIDDEN_SOURCE_ENUM },
@ -99,7 +75,7 @@ function List(props) {
name: "state",
label: "隐患状态",
render: FORM_ITEM_RENDER_ENUM.SELECT,
items: HIDDEN_STATE_ENUM.filter(item => !["98", "102"].includes(item.bianma)),
items: getUnmatchedItems({ list: HIDDEN_STATE_ENUM, value: ["98", "102"] }),
},
{ name: "confirmUserName", label: "确认人" },
{ name: "rectificationUserName", label: "整改人" },
@ -158,37 +134,16 @@ function List(props) {
打印
</Button>
)}
{props.permission("fgs-tz-dc") && (
<Button
type="primary"
icon={<ExportIcon />}
ghost
onClick={() => {
onExportExcel();
}}
>
导出
</Button>
)}
{props.permission("fgs-tz-aldc") && (
<Button
type="primary"
icon={<ExportIcon />}
ghost
onClick={() => {
const hiddenFindTime = form.getFieldValue("hiddenFindTime");
if (!hiddenFindTime) {
message.error("请选择隐患发现时间");
return;
}
if (hiddenFindTime.filter(Boolean).length === 0) {
message.error("请选择隐患发现时间");
return;
}
setExportByColumnModalOpen(true);
}}
>
按列导出
导出
</Button>
)}
</>
@ -289,16 +244,16 @@ function List(props) {
]}
{...tableProps}
/>
</Spin>
{exportByColumnModalOpen && (
<ExportByColumnModal
hiddenFindTime={form.getFieldValue("hiddenFindTime")}
selectedRowKeys={selectedRowKeys}
onCancel={() => setExportByColumnModalOpen(false)}
/>
)}
{printModalOpen && (
<PrintModal
data={selectedRowKeys}
selectedRowKeys={selectedRowKeys}
onCancel={() => setPrintModalOpen(false)}
/>
)}
@ -308,32 +263,89 @@ function List(props) {
const ExportByColumnModalComponent = (props) => {
const [form] = Form.useForm();
const exportFields = Form.useWatch("exportFields", form);
const { loading, downloadBlob } = useDownloadBlob();
const [items, setItems] = useState([]);
const indeterminate = useMemo(
() => {
if (!exportFields)
return false;
return exportFields.length > 0 && exportFields.length < items.length;
},
[exportFields],
);
const getExportColumn = async () => {
const { data } = await props["hiddenExportColumn"]();
setItems(data.map(item => ({ bianma: item, name: item })));
const defaultExportFields = data.filter(item => !item.includes("图片"));
form.setFieldsValue({ exportFields: [...defaultExportFields] });
};
useEffect(() => {
getExportColumn();
}, []);
const onSubmit = async (values) => {
const hiddenFindTime = props.hiddenFindTime;
const selectedRowKeys = props.selectedRowKeys;
// 检查导出字段是否包含"图片"
const hasPictureField = values.exportFields && values.exportFields.some(field => field.includes("图片"));
if (hasPictureField) {
// 包含图片字段的验证逻辑
if (!selectedRowKeys || selectedRowKeys.length === 0) {
message.error("请选择要导出的数据");
return;
}
if (selectedRowKeys.length > 10) {
message.error("选择的数据超过10条请重新选择");
return;
}
// 执行导出
Modal.confirm({
title: "导出确认",
content: "确定要导出excel吗",
onOk: async () => {
await downloadBlob("/hidden/hidden/exportHidden", {
params: { hiddenFindTime: props.hiddenFindTime[0], hiddenFindTimeLe: props.hiddenFindTime[1], ...values },
params: { ids: props.selectedRowKeys.join(","), ...values },
});
message.success("导出成功");
props.onCancel();
},
});
}
else {
// 不包含图片字段的验证逻辑
if (!hiddenFindTime || hiddenFindTime.length === 0) {
message.error("请选择隐患发现时间");
return;
}
// 执行导出
Modal.confirm({
title: "导出确认",
content: "确定要导出excel吗",
onOk: async () => {
await downloadBlob("/hidden/hidden/exportHidden", {
params: { hiddenFindTime: hiddenFindTime[0], hiddenFindTimeLe: hiddenFindTime[1], ...values },
});
message.success("导出成功");
props.onCancel();
},
});
}
};
return (
<Modal
title="按列导出"
width={600}
title="导出"
width={800}
open
maskClosable={false}
onCancel={props.onCancel}
@ -344,18 +356,38 @@ const ExportByColumnModalComponent = (props) => {
loading={loading}
form={form}
span={24}
labelCol={{ span: 8 }}
labelCol={{ span: 6 }}
showActionButtons={false}
onFinish={onSubmit}
options={[
{
key: "indeterminate",
label: "",
render: (
<Checkbox
indeterminate={indeterminate}
onChange={(e) => {
const checked = e.target.checked;
if (checked) {
form.setFieldsValue({ exportFields: items.map(item => item.bianma) });
}
else {
form.setFieldsValue({ exportFields: [] });
}
}}
checked={items.length === (exportFields || []).length}
>
全选为保障导出效率导出带图片的excel最多10条
</Checkbox>
),
required: false,
},
{
name: "exportFields",
label: "导出内容",
render: FORM_ITEM_RENDER_ENUM.SELECT,
render: FORM_ITEM_RENDER_ENUM.CHECKBOX,
items,
componentProps: {
mode: "multiple",
},
checkboxCol: 6,
},
]}
/>
@ -384,7 +416,7 @@ const PrintModalComponent = (props) => {
const getData = async () => {
const { data } = await props["hiddenPrintList"]({
ids: props.data.join(","),
ids: props.selectedRowKeys.join(","),
});
setList(data);
};

View File

@ -17,7 +17,7 @@ import { HIDDEN_RECTIFICATION_TYPE_ENUM, HIDDEN_SOURCE_ENUM, HIDDEN_STATE_ENUM }
import useDownloadBlob from "zy-react-library/hooks/useDownloadBlob";
import useGetUrlQuery from "zy-react-library/hooks/useGetUrlQuery";
import useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { getLabelName, getUnmatchedItems } from "zy-react-library/utils";
import { IS_RELATED_ENUM } from "~/enumerate/constant";
import { NS_AVERAGE } from "~/enumerate/namespace";
import "../../../../BranchCompany/Average/Ledger/List/index.less";
@ -86,7 +86,7 @@ function HiddenList(props) {
name: "state",
label: "隐患状态",
render: FORM_ITEM_RENDER_ENUM.SELECT,
items: HIDDEN_STATE_ENUM.filter(item => !["98", "102"].includes(item.bianma)),
items: getUnmatchedItems({ list: HIDDEN_STATE_ENUM, value: ["98", "102"] }),
},
{ name: "confirmUserName", label: "确认人" },
{ name: "rectificationUserName", label: "整改人" },