diff --git a/src/components/DataStandard/NewReportForm/Viewer/hotTable.tsx b/src/components/DataStandard/NewReportForm/Viewer/hotTable.tsx index 70ed5d06554e82a3300ea7ab31dc7d803e1882c0..4003dd36bd32528a403355f400594a2076390e53 100644 --- a/src/components/DataStandard/NewReportForm/Viewer/hotTable.tsx +++ b/src/components/DataStandard/NewReportForm/Viewer/hotTable.tsx @@ -307,13 +307,50 @@ const HotTableView: React.FC<{ }, [props.variablesData]); const exportToExcel = () => { const hot = hotRef.current.hotInstance; - const data = hot.getData(); // 获取当前表格数据 - const ws = XLSX.utils.aoa_to_sheet(data); // 转换为工作表 - const wb = XLSX.utils.book_new(); // 创建新的工作簿 - XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); // 将工作表加入工作簿 - XLSX.writeFile(wb, '当前表单.xlsx'); // 导出文件 + const data = hot.getData(); + // 获取浮动行数据,从cacheValue获取 + const floatRowData = props.form.cacheValue; + + // 创建一个数组用于存储导出数据 + const mergedData = data.map((row) => [...row]); + + // 遍历cacheValue来处理浮动行数据 + Object.keys(floatRowData).forEach((floatCode) => { + const floatRows = floatRowData[floatCode]; + if (Array.isArray(floatRows) && floatRows.length > 0) { + floatRows.forEach((floatRow) => { + const mergedRow = new Array(data[0].length).fill(''); + + Object.keys(floatRow).forEach((colKey) => { + const colIndex = getColumnIndex(colKey); + if (colIndex >= 0 && colIndex < mergedRow.length) { + mergedRow[colIndex] = floatRow[colKey]; + } + }); + + mergedData.push(mergedRow); + }); + } + }); + + // 创建 Excel 文件 + const ws = XLSX.utils.aoa_to_sheet(mergedData); + const wb = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); + XLSX.writeFile(wb, '当前表单.xlsx'); }; + function getColumnIndex(column) { + const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let index = 0; + + for (let i = 0; i < column.length; i++) { + index = index * 26 + (letters.indexOf(column[i].toUpperCase()) + 1); + } + + return index - 1; + } + const updateHot = useCallback(async (setting: XSheetConfig, sheetCell: XSheetCells) => { const hot = hotRef.current.hotInstance; @@ -601,7 +638,6 @@ const HotTableView: React.FC<{ } const tableData = subData.slice(0, rowCount); - const updatedFloatRowsData = { ...floatRowsData, [floatSetting.attributeId!]: {