From c60c45d9da3ac89e7c58c3dd49114f8894293907 Mon Sep 17 00:00:00 2001 From: HanLong <404402223@qq.com> Date: Sat, 28 Feb 2026 14:07:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:[=E6=A8=A1=E6=9D=BF=E7=AE=A1=E7=90=86]excel?= =?UTF-8?q?=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.js | 18 +++++++ .../comps/template/mixins/templateMixin.js | 57 ++++------------------ 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 2eb7065..7c1eceb 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -563,3 +563,21 @@ export function isRegent(item, fieldCode = 'type') { ] return typeList.includes(type) } +/** + * 估算字符串在 Excel 中的显示宽度(简单规则:中文字符算2,英文字符算1) + * @param {string} str 要计算的字符串 + * @returns {number} 估算宽度 + */ +export function getStringWidth(str) { + if (!str) return 0 + let width = 0 + for (let char of str.toString()) { + // 中文字符范围(可根据需要扩展) + if (/[\u4e00-\u9fa5]/.test(char)) { + width += 2 + } else { + width += 1 + } + } + return width +} diff --git a/src/views/business/comps/template/mixins/templateMixin.js b/src/views/business/comps/template/mixins/templateMixin.js index 46b73ea..7ccff69 100644 --- a/src/views/business/comps/template/mixins/templateMixin.js +++ b/src/views/business/comps/template/mixins/templateMixin.js @@ -1,10 +1,11 @@ import moment from 'moment' import { getLatestSn, getLatestSnArr } from '@/api/template'; -import { isValueEmpty } from '@/utils/index'; +import { isValueEmpty, getStringWidth } from '@/utils/index'; import { isCommonUnit } from "@/utils/conTools"; import { sj_subpackage, sj_startConfiguration, sj_configurationCompleted } from '@/api/business/sj/sj'; import {convertConcentration} from "@/utils/conConverter";//浓度单位转换 import {volumeConverter} from "@/utils/volConverter";//体积单位转换 +import * as XLSX from 'xlsx' export default { dicts: [ 'business_pztj', @@ -602,54 +603,16 @@ export default { } }, // 导出excel模板 - exportExcel(rows, title) { + exportExcel(headerArray, title = '导出模板') { this.$modal.loading() - // 生成表头 - let tableHtml = '
| ' + item + ' | '; - }); - tableHtml += '
|---|