fo: excel 컬럼 순서 fix
This commit is contained in:
@@ -174,7 +174,8 @@ export default function PrecipitationModal(props: PrecipitationModalProps) {
|
||||
}
|
||||
|
||||
if (excelDownList && excelDownList.length > 0) {
|
||||
downloadExcelHeaders({ data: JSON.parse(JSON.stringify(excelDownList)), fileName: props.searchYear+'년 '+props.fcltyName + ' 강수현황', sheetNameArray: ['지하수유입량'], headerRange: 'A1:D1' });
|
||||
downloadExcelHeaders({ data: JSON.parse(JSON.stringify(excelDownList)), fileName: props.searchYear+'년 '+props.fcltyName + ' 강수현황'
|
||||
, sheetNameArray: ['지하수유입량'], headerRange: 'A1:D1', headers });
|
||||
} else if (totData.length == 0) {
|
||||
alert('다운받을 데이터가 없습니다.');
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import { roundNumber } from "@/app/components/common/DataForm";
|
||||
import { useFoStore } from "@/app/store";
|
||||
import type { modalTableType } from "@/app/store/sewageOccur";
|
||||
import { DateMonthPicker, downloadExcel, UiTable } from "@common/business_components/ui";
|
||||
import { DateMonthPicker, downloadExcelHeaders, UiTable } from "@common/business_components/ui";
|
||||
import ListDiv from "@common/business_components/ui/list_div";
|
||||
import { Button, Table, TableCell, TableHead, TableRow } from "@common/components/ui";
|
||||
import { useEffect, useState } from "react";
|
||||
@@ -69,20 +69,25 @@ export default function SewageOccurModal(props: { fcltyName: string; facilityCd
|
||||
}
|
||||
}, [endYearMonth, startYearMonth]);
|
||||
|
||||
const excelDownload = () => {
|
||||
const excelDownload = () => {
|
||||
// 헤더 순서를 명시적으로 정의
|
||||
const headers = ["날짜", "물사용량(m³/일)", "오수전환율(%)", "오수발생량 추정(m³/일)"] as const;
|
||||
|
||||
const excelDownList: any[] = [];
|
||||
if(detailList.length > 0){
|
||||
detailList.forEach((item) => {
|
||||
excelDownList.push({
|
||||
"날짜": item.msrDt,
|
||||
"물사용량(m³/일)": roundNumber(item.waterUsage, 0),
|
||||
"오수전환율(%)": roundNumber(item.swageChangePercent, 0),
|
||||
"오수발생량 추정(m³/일)": roundNumber(item.estimationSewage, 0),
|
||||
});
|
||||
// 순서를 보장하기 위해 헤더 순서대로 객체 생성
|
||||
const row: Record<string, string | number> = {};
|
||||
row[headers[0]] = item.msrDt;
|
||||
row[headers[1]] = roundNumber(item.waterUsage, 0);
|
||||
row[headers[2]] = roundNumber(item.swageChangePercent, 0);
|
||||
row[headers[3]] = roundNumber(item.estimationSewage, 0);
|
||||
excelDownList.push(row);
|
||||
});
|
||||
}
|
||||
if (excelDownList && excelDownList.length > 0) {
|
||||
downloadExcel({ data: JSON.parse(JSON.stringify(excelDownList)), fileName: props.searchYear+'년 '+props.fcltyName + ' 오수발생량 추정', sheetNameArray: [props.fcltyName + ' 오수발생량 추정'], headerRange: 'A1:D1' });
|
||||
downloadExcelHeaders({ data: JSON.parse(JSON.stringify(excelDownList)), fileName: props.searchYear+'년 '+props.fcltyName + ' 오수발생량 추정'
|
||||
, sheetNameArray: [props.fcltyName + ' 오수발생량 추정'], headerRange: 'A1:D1', headers });
|
||||
} else if (detailList.length == 0) {
|
||||
alert('다운받을 데이터가 없습니다.');
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ type ExcelDownloadProps = {
|
||||
fileName: string;
|
||||
sheetNameArray: string[];
|
||||
headerRange?: string;
|
||||
headers?: string[]|undefined;
|
||||
headers?: readonly string[]|undefined;
|
||||
decimalArray0? : number[];
|
||||
decimalArray1? : number[];
|
||||
decimalArray2? : number[];
|
||||
@@ -23,7 +23,7 @@ export const downloadExcel = ({ data, fileName = new Date().getTime().toString()
|
||||
export const downloadExcelHeaders = ({ data, fileName = new Date().getTime().toString() + '.xlsx', sheetNameArray = ["Sheet1"], headerRange='A1:V3', headers}: ExcelDownloadProps) => {
|
||||
const dataArray = Array.isArray(data) ? data : [data];
|
||||
// headers가 제공되면 열 순서를 강제함
|
||||
const worksheet = XLSX.utils.json_to_sheet(dataArray, headers ? { header: headers } : undefined);
|
||||
const worksheet = XLSX.utils.json_to_sheet(dataArray, headers ? { header: [...headers] } : undefined);
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetNameArray[0]);
|
||||
excelStyle(XLSX.write(workbook, { bookType: "xlsx", type: "buffer" }), headerRange, fileName);
|
||||
|
||||
Reference in New Issue
Block a user