作者 xudada

前端解析主单excel文件,前端赋值

... ... @@ -26,6 +26,15 @@
<el-col style="margin-left: 30px" :span="2">
<el-button type="warning" plain @click="cancleBtn">清空</el-button>
</el-col>
<el-col :span="2">
<el-upload
action=""
:on-change="handleChange"
:auto-upload="false"
:show-file-list="false">
<el-button slot="trigger" type="primary">Excel文件</el-button>
</el-upload>
</el-col>
</el-row>
<!-- 发货人信息-->
<el-row>
... ... @@ -2077,6 +2086,56 @@ Handling Information
import {loginedUserInfo} from "../../api/user";
import {getList} from "../../api/consigner/consigner";
import jsutil from "@/common/js/util";
import XLSX from 'xlsx';
function cleanString(str, maxLength = 20) {
return str.replace(/[^a-zA-Z0-9\s]/g, '').slice(0, maxLength);
}
/*function parseCustomDate(dateStr) {
if (typeof dateStr !== 'string') {
return '无效日期';
}
// 自定义日期格式正则表达式
const regex = /(\d{1,2})\/([a-zA-Z]{3})\/(\d{4})/;
const match = dateStr.match(regex);
if (match) {
const day = match[1];
const month = match[2];
const year = match[3];
// 月份缩写映射
const monthMap = {
Jan: '01', Feb: '02', Mar: '03', Apr: '04',
May: '05', Jun: '06', Jul: '07', Aug: '08',
Sep: '09', Oct: '10', Nov: '11', Dec: '12'
};
const formattedMonth = monthMap[month.toUpperCase()];
if (formattedMonth) {
return `${year}-${formattedMonth}-${day.padStart(2, '0')}`;
}
}
return '无效日期';
}*/
function formatAF23(value) {
if (typeof value !== 'string') {
return '无效值';
}
// 删除 DIMS: 行和 VOL: 行
const cleanedValue = value.replace(/DIMS:\s*/gi, '').replace(/VOL:\s*\d+\.\d+\s*CBM/gi, '');
// 将剩余内容按行分割,并去除空行
const lines = cleanedValue.split('\n').filter(line => line.trim() !== '');
// 将非空行重新组合成字符串,并用换行符分隔
return lines.join('\n').trim();
}
export default {
components: {
... ... @@ -2123,23 +2182,23 @@ Handling Information
cvd_amount_of_insurance:'XXX',
cvd_charge_code:'PP',
cvd_charge_prepaid:'PP',
cvd_currency_code:'EUR',
cvd_currency_code:'CNY',
cvd_value_for_carriage:'NVD',
cvd_value_for_customs:'NCV',
},
flt:{
cariier:"HQ",
day:'02',
day:'08',
flightNumber:'405',
cariier2:"HQ",
day2:'02',
day2:'08',
flightNumber2:'4055',
cariier3:"",
day3:'',
flightNumber3:'',
},
isu:{
isu_day_mounth_year:'2024-11-01',
isu_day_mounth_year:'2024-11-07',
isu_place_or_airport_code:'ZHENGZHOU',
isu_signature:'CHINA',
},
... ... @@ -2190,11 +2249,11 @@ Handling Information
rtd_charge_weight:'',
rtd_combination_point:'',
rtd_commodity_NUM:'',
rtd_goods_DES:'',
rtd_goods_DES:'CONSOLIDATED CARGO',
rtd_goods_consol_DES:'',
rtd_gross_weight:'',
rtd_number_pieces:'',
rtd_rate_charge:'',
rtd_rate_charge:'42.66',
rtd_rate_class:'Q',
rtd_total:'',
//体积
... ... @@ -2224,10 +2283,10 @@ Handling Information
ssr_request_content:["BUP DO NOT BREAKDOWN"],
},
cer:{
cer_signature:'GATE INTERNATIONAL'
cer_signature:' DHL GLOBAL'
}
},
ssr_content:"BUP DO NOT BREAKDOWN",
ssr_content:"NO SOLID WOODEN PACKING MATERIALS",
cne_contact:{
contact_id:'TE',
contact_number:'',
... ... @@ -2341,12 +2400,12 @@ Handling Information
cvd_amount_of_insurance:'XXX',
cvd_charge_prepaid:'PP',
cvd_charge_code:'',
cvd_currency_code:'',
cvd_currency_code:'USD',
},
hbs:{
hbs_origin:'CGO',
hbs_destination:'',
hbs_manifest_description:'',
hbs_destination:'AMS',
hbs_manifest_description:'CELL PHONE',
hbs_pieces:'',
hbs_serial_number:'',
hbs_weight:'',
... ... @@ -2391,6 +2450,8 @@ Handling Information
gridData:[],
dialogVisible:false,
dialogVisible1:false,
fileContent: null,
currentDate:new Date(),
}
},
methods:{
... ... @@ -2909,6 +2970,113 @@ Handling Information
},
cancleBtn(){
Object.assign(this.$data, this.$options.data());
},
handleChange(file, fileList) {
const files = file.raw;
const reader = new FileReader();
reader.onload = (e) => {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const worksheetName = '格式化打印'; // 指定工作表名称
const worksheet = workbook.Sheets[worksheetName];
if (worksheet) {
const AF1 = (worksheet['AF1'] && worksheet['AF1'].v) || '单元格不存在';
const A16 = (worksheet['A16'] && worksheet['A16'].v) || '单元格不存在';
const C16 = (worksheet['C16'] && worksheet['C16'].v) || '单元格不存在';
const K16 = (worksheet['K16'] && worksheet['K16'].v) || '单元格不存在';
const L16 = (worksheet['L16'] && worksheet['L16'].v) || '单元格不存在';
const S16 = (worksheet['S16'] && worksheet['S16'].v) || '单元格不存在';
const I18 = (worksheet['I18'] && worksheet['I18'].v) || '单元格不存在';
const M18 = (worksheet['M18'] && worksheet['M18'].v) || '单元格不存在';
const A20 = (worksheet['A20'] && worksheet['A20'].v) || '单元格不存在';
const A22 = (worksheet['A22'] && worksheet['A22'].v) || '单元格不存在';
const C22 = (worksheet['C22'] && worksheet['C22'].v) || '单元格不存在';
const K22 = (worksheet['K22'] && worksheet['K22'].v) || '单元格不存在';
const P22 = (worksheet['P22'] && worksheet['P22'].v) || '单元格不存在';
const U22 = (worksheet['U22'] && worksheet['U22'].v) || '单元格不存在';
const AF22 = (worksheet['AF22'] && worksheet['AF22'].v) || '单元格不存在';
const AF23 = (worksheet['AF23'] && worksheet['AF23'].v) || '单元格不存在';
const O34 = (worksheet['O34'] && worksheet['O34'].v) || '单元格不存在';
//const O38 = (worksheet['O38'] && worksheet['O38'].v) || '单元格不存在';
const X38 = (worksheet['X38'] && worksheet['X38'].v) || '单元格不存在';
const AF38 = (worksheet['AF38'] && worksheet['AF38'].v) || '单元格不存在';
const inFlightInfo = I18.split('/');
const outFlightInfo = M18.split('/');
const inFlightNumber = inFlightInfo[0];
const inFlightDate = inFlightInfo.length > 1 ? inFlightInfo[1].split(' ')[0] : '';
const outFlightNumber = outFlightInfo[0];
const outFlightDate = outFlightInfo.length > 1 ? outFlightInfo[1].split(' ')[0] : '';
const SSRInfo = A20.replace(/[^a-zA-Z0-9\s]/g, '');
//const signatureDate = parseCustomDate(O38);
const formattedAF23 = formatAF23(AF23);
this.fileContent = {
AF1: AF1,
A16: A16,
C16: C16,
K16: K16,
L16: L16,
S16: S16,
inFlightNumber: inFlightNumber,
inFlightDate: inFlightDate,
outFlightNumber: outFlightNumber,
outFlightDate: outFlightDate,
SSRInfo: SSRInfo,
A22: A22,
C22: C22,
K22: K22,
P22: P22,
U22: U22,
AF22: cleanString(AF22),
AF23: formattedAF23,
O34: cleanString(O34),
//signatureDate: signatureDate,
X38: X38,
AF38: cleanString(AF38),
};
this.form.bill.waybillNum=this.fileContent.AF1;
this.form.cvd.cvd_currency_code=this.fileContent.S16;
this.form.rtg.destinationAirport=this.fileContent.A16;
this.form.rtg.destinationCarrier=this.fileContent.C16;
this.form.rtg.onwardAirport=this.fileContent.K16;
this.form.rtg.onwardCarrier=this.fileContent.L16;
this.form.flt.cariier=this.fileContent.inFlightNumber.substring(0,2);
this.form.flt.day=this.fileContent.inFlightDate.substring(0,2);
this.form.flt.flightNumber=this.fileContent.inFlightNumber.substring(2);
this.form.flt.cariier2=this.fileContent.outFlightNumber.substring(0,2);
this.form.flt.day2=this.fileContent.outFlightDate.substring(0,2);
this.form.flt.flightNumber2=this.fileContent.outFlightNumber.substring(2);
this.ssr_content=this.fileContent.SSRInfo;
this.form.bill.quantity_picecs=this.fileContent.A22;
this.form.bill.quantity_weight=parseFloat(this.fileContent.K22).toFixed(2);
this.form.rtd.rtd_charge_weight=parseFloat(this.fileContent.C22).toFixed(2);
this.form.rtd.rtd_rate_charge=this.fileContent.P22;
this.form.rtd.rtd_total=parseFloat(this.fileContent.U22).toFixed(2);
this.form.rtd.rtd_goods_DES=this.fileContent.AF22;
this.form.ppd.ppd_weight_amount=parseFloat(this.fileContent.U22).toFixed(2);
this.form.ppd.ppd_charge_summary_total=parseFloat(this.fileContent.U22).toFixed(2);
this.form.cer.cer_signature=this.fileContent.O34;
this.form.isu.isu_signature=this.fileContent.AF38;
this.dimension_textarea=this.fileContent.AF23;
this.convertAndCalculateVolume();
this.form.isu.isu_day_mounth_year=this.formattedDate();
} else {
this.fileContent = { AF1: '工作表不存在' };
}
};
reader.readAsArrayBuffer(files);
},
formattedDate() {
// 格式化日期为 yyyy-MM-dd
const date = this.currentDate;
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
},
activated(){
... ... @@ -2974,7 +3142,7 @@ Handling Information
this.form.flt.cariier2 = cleanedValue.slice(0, 2);
this.form.flt.flightNumber2 = cleanedValue.slice(2);
}
}
},
},
watch:{
ssr_content: {
... ...