Browse Source

fix:[模板管理]excel导出

lkf
HanLong 1 month ago
parent
commit
c60c45d9da
2 changed files with 28 additions and 47 deletions
  1. +18
    -0
      src/utils/index.js
  2. +10
    -47
      src/views/business/comps/template/mixins/templateMixin.js

+ 18
- 0
src/utils/index.js View File

@ -563,3 +563,21 @@ export function isRegent(item, fieldCode = 'type') {
] ]
return typeList.includes(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
}

+ 10
- 47
src/views/business/comps/template/mixins/templateMixin.js View File

@ -1,10 +1,11 @@
import moment from 'moment' import moment from 'moment'
import { getLatestSn, getLatestSnArr } from '@/api/template'; import { getLatestSn, getLatestSnArr } from '@/api/template';
import { isValueEmpty } from '@/utils/index';
import { isValueEmpty, getStringWidth } from '@/utils/index';
import { isCommonUnit } from "@/utils/conTools"; import { isCommonUnit } from "@/utils/conTools";
import { sj_subpackage, sj_startConfiguration, sj_configurationCompleted } from '@/api/business/sj/sj'; import { sj_subpackage, sj_startConfiguration, sj_configurationCompleted } from '@/api/business/sj/sj';
import {convertConcentration} from "@/utils/conConverter";//浓度单位转换 import {convertConcentration} from "@/utils/conConverter";//浓度单位转换
import {volumeConverter} from "@/utils/volConverter";//体积单位转换 import {volumeConverter} from "@/utils/volConverter";//体积单位转换
import * as XLSX from 'xlsx'
export default { export default {
dicts: [ dicts: [
'business_pztj', 'business_pztj',
@ -602,54 +603,16 @@ export default {
} }
}, },
// 导出excel模板 // 导出excel模板
exportExcel(rows, title) {
exportExcel(headerArray, title = '导出模板') {
this.$modal.loading() this.$modal.loading()
// 生成表头
let tableHtml = '<table border="1" class="html-tabel">';
tableHtml += '<tr style="background:#eee;">';
rows.forEach(item => {
tableHtml += '<th style="text-align: center;">' + item + '</th>';
});
tableHtml += '</tr></table>'; // 正确闭合
// Worksheet 名称
const worksheet = title ? title : '导入模板';
// 完整的 HTML 模板(包含编码声明和 Excel 兼容命名空间)
const exportTemplate = `<!DOCTYPE html>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>${worksheet}</x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body>
${tableHtml}
</body>
</html>`;
// 使用 Blob 生成文件(指定 MIME 类型为 application/vnd.ms-excel)
const blob = new Blob([exportTemplate], { type: 'application/vnd.ms-excel;charset=utf-8' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = worksheet + '.xls';
link.click();
URL.revokeObjectURL(link.href); // 释放内存
const ws = XLSX.utils.aoa_to_sheet([headerArray]);
const colWidths = headerArray.map(cell => getStringWidth(cell) + 2);
ws['!cols'] = colWidths.map(width => ({ wch: width }));
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
XLSX.writeFile(wb, `${title}.xlsx`);
this.$modal.closeLoading() this.$modal.closeLoading()
}, },
} }
} }

Loading…
Cancel
Save