fo: 강수현황 excel문제 해결
This commit is contained in:
@@ -7,9 +7,14 @@
|
||||
export const roundNumber = (value : any, decimals: number) => {
|
||||
if (typeof value === 'number') {
|
||||
return addCommas((Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals)).toFixed(decimals));
|
||||
} else if (typeof value === 'string' && !isNaN(Number(value))) {
|
||||
return addCommas(Number(value).toFixed(decimals));
|
||||
} else if (typeof value === 'string') {
|
||||
// 콤마가 포함된 문자열 처리 ('1,083.0' 같은 경우)
|
||||
const cleanedValue = value.replace(/,/g, '');
|
||||
if (!isNaN(Number(cleanedValue))) {
|
||||
return addCommas(Number(cleanedValue).toFixed(decimals));
|
||||
}
|
||||
}
|
||||
return value; // 변환 불가능한 경우 원래 값 반환
|
||||
}
|
||||
|
||||
export const addCommas = (value: string | number): any => {
|
||||
|
||||
@@ -9,7 +9,7 @@ import CalcRainEffect from "@/app/components/common/CalcRainEffect";
|
||||
import { roundNumber } from "@/app/components/common/DataForm";
|
||||
import { useFoStore } from "@/app/store";
|
||||
import type { PrecipitationPropsDetailProps } from "@/app/store/facilityPrecipitation";
|
||||
import { DatePickerRange, downloadExcel, UiTable } from "@common/business_components/ui";
|
||||
import { DatePickerRange, 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, useRef, useState } from "react";
|
||||
@@ -45,7 +45,7 @@ export default function PrecipitationModal(props: PrecipitationModalProps) {
|
||||
const standYear = parseInt(props.searchYear);
|
||||
return [{
|
||||
start: new Date(standYear, 0, 1),
|
||||
end: new Date(standYear, 11, 31)
|
||||
end: new Date(standYear, 11, 31) //월의 index는 0부터 시작
|
||||
}];
|
||||
};
|
||||
|
||||
@@ -112,7 +112,6 @@ export default function PrecipitationModal(props: PrecipitationModalProps) {
|
||||
const getTableData = () => {
|
||||
const totals = calculateTotals();
|
||||
totals.infoRnDay = roundNumber(totals.infoRnDay, 1);
|
||||
|
||||
return [
|
||||
{
|
||||
bscFctCd: "",
|
||||
@@ -157,19 +156,25 @@ export default function PrecipitationModal(props: PrecipitationModalProps) {
|
||||
|
||||
const excelDownload = () => {
|
||||
const totData = getTableData();
|
||||
const excelDownList: any[] = [];
|
||||
|
||||
// 헤더 순서를 명시적으로 정의
|
||||
const headers = ["날짜", "강수량(mm/일)", "강우일(일)", "강우영향일(일)"] as const;
|
||||
|
||||
const excelDownList: Record<string, string | number>[] = [];
|
||||
if(totData.length > 0){
|
||||
totData.forEach((item) => {
|
||||
excelDownList.push({
|
||||
"날짜": item.rainDt,
|
||||
"강수량(mm/일)": roundNumber(item.infoRnDay, 1),
|
||||
"강우일(일)": roundNumber(item.rainDay, 0),
|
||||
"강우영향일(일)": roundNumber(item.rainEffectDay, 0),
|
||||
});
|
||||
// 순서를 보장하기 위해 헤더 순서대로 객체 생성
|
||||
const row: Record<string, string | number> = {};
|
||||
row[headers[0]] = item.rainDt;
|
||||
row[headers[1]] = roundNumber(item.infoRnDay, 1);
|
||||
row[headers[2]] = roundNumber(item.rainDay, 0);
|
||||
row[headers[3]] = roundNumber(item.rainEffectDay, 0);
|
||||
excelDownList.push(row);
|
||||
});
|
||||
}
|
||||
|
||||
if (excelDownList && excelDownList.length > 0) {
|
||||
downloadExcel({ 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' });
|
||||
} else if (totData.length == 0) {
|
||||
alert('다운받을 데이터가 없습니다.');
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ type ExcelDownloadProps = {
|
||||
fileName: string;
|
||||
sheetNameArray: string[];
|
||||
headerRange?: string;
|
||||
headers?: string[]|undefined;
|
||||
decimalArray0? : number[];
|
||||
decimalArray1? : number[];
|
||||
decimalArray2? : number[];
|
||||
@@ -19,6 +20,15 @@ export const downloadExcel = ({ data, fileName = new Date().getTime().toString()
|
||||
excelStyle(XLSX.write(workbook, { bookType: "xlsx", type: "buffer" }), headerRange, fileName);
|
||||
};
|
||||
|
||||
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 workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetNameArray[0]);
|
||||
excelStyle(XLSX.write(workbook, { bookType: "xlsx", type: "buffer" }), headerRange, fileName);
|
||||
};
|
||||
|
||||
export const downloadExcelFormat = ({ data, fileName = new Date().getTime().toString() + '.xlsx', sheetNameArray = ["Sheet1"], headerRange='A1:V3', decimalArray0=[], decimalArray1=[], decimalArray2=[], decimalArray3=[]}: ExcelDownloadProps) => {
|
||||
const worksheet = XLSX.utils.json_to_sheet(Array.isArray(data) ? data : [data]);
|
||||
const workbook = XLSX.utils.book_new();
|
||||
|
||||
Reference in New Issue
Block a user