171a4b2f by lxs

入表功调整

1 parent f2779784
...@@ -440,8 +440,8 @@ const addSameData = (rIndex, name, level, tData) => { ...@@ -440,8 +440,8 @@ const addSameData = (rIndex, name, level, tData) => {
440 const lastCode = rowData[`code${level - 1}`]; 440 const lastCode = rowData[`code${level - 1}`];
441 const tCode = tData[`code${level}`].split(lastCode)[1]; 441 const tCode = tData[`code${level}`].split(lastCode)[1];
442 const codeVal = parseInt(tCode, 10) < 10 ? `0${parseInt(tCode, 10) + 1}` : parseInt(tCode, 10) + 1; 442 const codeVal = parseInt(tCode, 10) < 10 ? `0${parseInt(tCode, 10) + 1}` : parseInt(tCode, 10) + 1;
443 for(var r in rowData){ 443 for (var r in rowData) {
444 if(r.indexOf('name') == -1 && r.indexOf('code') == -1) rowData[r] = ''; 444 if (r.indexOf('name') == -1 && r.indexOf('code') == -1) rowData[r] = '';
445 } 445 }
446 rowData[`name${level}`] = name; 446 rowData[`name${level}`] = name;
447 rowData[`code${level}`] = `${lastCode}${codeVal}`; 447 rowData[`code${level}`] = `${lastCode}${codeVal}`;
...@@ -458,8 +458,8 @@ const addLowerData = (rIndex, name, level, len) => { ...@@ -458,8 +458,8 @@ const addLowerData = (rIndex, name, level, len) => {
458 const hasLowerItem = rowData[`code${level + 1}`] ? true : false; 458 const hasLowerItem = rowData[`code${level + 1}`] ? true : false;
459 const lastCode = rowData[`code${level}`]; 459 const lastCode = rowData[`code${level}`];
460 const codeVal = (len + 1) < 10 ? '0' + (len + 1) : len + 1; 460 const codeVal = (len + 1) < 10 ? '0' + (len + 1) : len + 1;
461 for(var r in rowData){ 461 for (var r in rowData) {
462 if(r.indexOf('name') == -1 && r.indexOf('code') == -1) rowData[r] = ''; 462 if (r.indexOf('name') == -1 && r.indexOf('code') == -1) rowData[r] = '';
463 } 463 }
464 rowData[`name${level + 1}`] = name; 464 rowData[`name${level + 1}`] = name;
465 rowData[`code${level + 1}`] = `${lastCode}${codeVal}`; 465 rowData[`code${level + 1}`] = `${lastCode}${codeVal}`;
...@@ -928,6 +928,25 @@ const s2ab = (s) => { ...@@ -928,6 +928,25 @@ const s2ab = (s) => {
928 return buf; 928 return buf;
929 } 929 }
930 930
931 // 定义边框样式 - 实线边框
932 const borderStyle = {
933 top: { style: 'thin', color: { rgb: 'D9D9D9' } },
934 bottom: { style: 'thin', color: { rgb: 'D9D9D9' } },
935 left: { style: 'thin', color: { rgb: 'D9D9D9' } },
936 right: { style: 'thin', color: { rgb: 'D9D9D9' } }
937 };
938
939 // 字体配置
940 const defaultFont = {
941 name: '宋体', // 字体名称
942 sz: 14 * 0.75, // 字体大小(磅)
943 color: { rgb: '212121' } // 黑色
944 };
945
946 // 设置行高(单位:磅,1px≈0.75磅)
947 const defaultRowHeightInPx = 36; // 默认行高36px
948 const headerRowHeightInPx = 32; // 表头行高32px
949
931 // 生成成本设置表 950 // 生成成本设置表
932 const exportDetailsToExcel = async () => { 951 const exportDetailsToExcel = async () => {
933 // 准备工作表数据 952 // 准备工作表数据
...@@ -1002,14 +1021,29 @@ const exportDetailsToExcel = async () => { ...@@ -1002,14 +1021,29 @@ const exportDetailsToExcel = async () => {
1002 // 自动调整列宽 1021 // 自动调整列宽
1003 const colWidths = fields.map((field, index) => { 1022 const colWidths = fields.map((field, index) => {
1004 const defaultWidth = field.width || 100 // 默认宽度100 1023 const defaultWidth = field.width || 100 // 默认宽度100
1005 const colWidth = defaultWidth / 14 + 2; // 留一些余地 1024 const colWidth = defaultWidth * 0.75; // 留一些余地
1006 1025
1007 // 取表头宽度和内容宽度的较大值 1026 // 取表头宽度和内容宽度的较大值
1008 return { wpx: colWidth * 12 } // 使用像素宽度 1027 return { wpx: colWidth } // 使用像素宽度
1009 }) 1028 })
1010 1029
1011 ws['!cols'] = colWidths 1030 ws['!cols'] = colWidths
1012 1031
1032 // 转换为磅(Excel使用磅作为单位)
1033 const defaultRowHeight = defaultRowHeightInPx * 0.75;
1034 const headerRowHeight = headerRowHeightInPx * 0.75;
1035
1036 // 初始化行高设置
1037 ws['!rows'] = [];
1038
1039 // 设置表头行高
1040 ws['!rows'][0] = { hpt: headerRowHeight, customHeight: true };
1041
1042 // 设置数据行高
1043 for (let i = 1; i < wsData.length; i++) {
1044 ws['!rows'][i] = { hpt: defaultRowHeight, customHeight: true };
1045 }
1046
1013 // 设置合并单元格 1047 // 设置合并单元格
1014 const merges = [] 1048 const merges = []
1015 1049
...@@ -1051,13 +1085,16 @@ const exportDetailsToExcel = async () => { ...@@ -1051,13 +1085,16 @@ const exportDetailsToExcel = async () => {
1051 alignment: { 1085 alignment: {
1052 vertical: 'center', 1086 vertical: 'center',
1053 horizontal: 'left' 1087 horizontal: 'left'
1054 } 1088 },
1089 font: defaultFont,
1090 border: borderStyle // 添加边框样式
1055 } 1091 }
1056 1092
1057 // 表头样式 1093 // 表头样式
1058 if (R === 0) { 1094 if (R === 0) {
1059 ws[cellAddress].s = { 1095 ws[cellAddress].s = {
1060 ...defaultStyle, 1096 ...defaultStyle,
1097 fill: { fgColor: { rgb: 'F2F2F2' } } // 表头背景色(可选)
1061 } 1098 }
1062 continue 1099 continue
1063 } 1100 }
...@@ -1138,14 +1175,29 @@ const exportBookToExcel = async () => { ...@@ -1138,14 +1175,29 @@ const exportBookToExcel = async () => {
1138 // 自动调整列宽 1175 // 自动调整列宽
1139 const colWidths = bookHeaders.value.map((field, index) => { 1176 const colWidths = bookHeaders.value.map((field, index) => {
1140 const defaultWidth = field.width || 100 // 默认宽度100 1177 const defaultWidth = field.width || 100 // 默认宽度100
1141 const colWidth = defaultWidth / 14 + 2; // 留一些余地 1178 const colWidth = defaultWidth * 0.75; // 留一些余地
1142 1179
1143 // 取表头宽度和内容宽度的较大值 1180 // 取表头宽度和内容宽度的较大值
1144 return { wpx: colWidth * 12 } // 使用像素宽度 1181 return { wpx: colWidth } // 使用像素宽度
1145 }) 1182 })
1146 1183
1147 ws['!cols'] = colWidths 1184 ws['!cols'] = colWidths
1148 1185
1186 // 转换为磅(Excel使用磅作为单位)
1187 const defaultRowHeight = defaultRowHeightInPx * 0.75;
1188 const headerRowHeight = headerRowHeightInPx * 0.75;
1189
1190 // 初始化行高设置
1191 ws['!rows'] = [];
1192
1193 // 设置表头行高
1194 ws['!rows'][0] = { hpt: headerRowHeight, customHeight: true };
1195
1196 // 设置数据行高
1197 for (let i = 1; i < wsData.length; i++) {
1198 ws['!rows'][i] = { hpt: defaultRowHeight, customHeight: true };
1199 }
1200
1149 // 设置单元格样式 1201 // 设置单元格样式
1150 const range = XLSXS.utils.decode_range(ws['!ref']) 1202 const range = XLSXS.utils.decode_range(ws['!ref'])
1151 // 数值字段(除title外的所有字段) 1203 // 数值字段(除title外的所有字段)
...@@ -1165,14 +1217,16 @@ const exportBookToExcel = async () => { ...@@ -1165,14 +1217,16 @@ const exportBookToExcel = async () => {
1165 alignment: { 1217 alignment: {
1166 vertical: 'center', 1218 vertical: 'center',
1167 horizontal: 'left' 1219 horizontal: 'left'
1168 } 1220 },
1221 font: defaultFont,
1222 border: borderStyle // 添加边框样式
1169 } 1223 }
1170 1224
1171 // 表头样式(第1行) 1225 // 表头样式(第1行)
1172 if (R === 0) { 1226 if (R === 0) {
1173 ws[cellAddress].s = { 1227 ws[cellAddress].s = {
1174 ...defaultStyle, 1228 ...defaultStyle,
1175 // font: { bold: true } // 表头加粗 1229 fill: { fgColor: { rgb: 'F2F2F2' } } // 表头背景色(可选)
1176 } 1230 }
1177 continue 1231 continue
1178 } 1232 }
...@@ -1187,10 +1241,11 @@ const exportBookToExcel = async () => { ...@@ -1187,10 +1241,11 @@ const exportBookToExcel = async () => {
1187 // 数值列特殊处理(无论是否最后一行) 1241 // 数值列特殊处理(无论是否最后一行)
1188 if (isNumeric) { 1242 if (isNumeric) {
1189 ws[cellAddress].s = { 1243 ws[cellAddress].s = {
1244 ...defaultStyle,
1190 alignment: { 1245 alignment: {
1191 vertical: 'center', 1246 vertical: 'center',
1192 horizontal: 'right' 1247 horizontal: 'right'
1193 } 1248 },
1194 }; 1249 };
1195 1250
1196 // 确保数值被正确识别为数字类型 1251 // 确保数值被正确识别为数字类型
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!