JS导入Excel实战

JS导入Excel实战_第1张图片

JS导入Excel实战_第2张图片

JS导入Excel实战_第3张图片

下载ZIP后解压得到xlsx.full.min.js

/**
 * 对导入的excel进行解析
 */
function importExcel(importExcel){
	//判断结论时准则不可以为空
	if (!$("#testSpec").val()) {
		helper.alertDialog("Test Spec can not be empty!","danger");
		$("#import_Excel").val("");
		return
	}
	if (!$("#measuredWiringCs").val()) {
		helper.alertDialog("Cross Section can not be empty!","danger");
		$("#import_Excel").val("");
		return
	}
	if (!$("#baseMaterial").val()) {
		helper.alertDialog("Terminal Base Material can not be empty!","danger");
		$("#import_Excel").val("");
		return
	}
	bootbox.confirm("

Are you sure to select the excel?

", function(result){ if(result) { importExcelDataToReport(importExcel); } else { $("#import_Excel").val(""); } }); } /** * 将excel数据导入Report */ function importExcelDataToReport(importExcel){ //获取导入的excel var excel = importExcel.files[0]; //创建fileReader对象和其加载方法 var fileReader = new FileReader(); fileReader.onload = function(excel) { //获取工作簿 var workbook = XLSX.read(excel.target.result, {type: 'binary'}); //获取指定sheet页数据 var pullForce_Rows = XLSX.utils.sheet_to_json(workbook.Sheets["Pull Force"]); var cableSealRetention_Rows = XLSX.utils.sheet_to_json(workbook.Sheets["Cable Seal Retention"]); var voltageDrop_Rows = XLSX.utils.sheet_to_json(workbook.Sheets["Voltage Drop"]); var electricalCurrentCycling_Rows = XLSX.utils.sheet_to_json(workbook.Sheets["Electrical Current Cycling"]); var env_Rows = XLSX.utils.sheet_to_json(workbook.Sheets["ENV"]); //填充选择的试验项对应的数据 var testItems = $("#testItems").val(); if (testItems.indexOf("Pull Force")!=-1) { //填充Pull Force数据 for (var i = 0; i < pullForce_Rows.length; i++) { $("#pfcch1pf"+(i+1)).val(pullForce_Rows[i].拉力Force1); $("#pfcch2pf"+(i+1)).val(pullForce_Rows[i].拉力Force2); $("#pfcch3pf"+(i+1)).val(pullForce_Rows[i].拉力Force3); $("#pfcch4pf"+(i+1)).val(pullForce_Rows[i].拉力Force4); $("#pfcch5pf"+(i+1)).val(pullForce_Rows[i].拉力Force5); } //计算Pull Force结论 $("#pfcch1pf1").blur(); $("#pfcch2pf1").blur(); $("#pfcch3pf1").blur(); $("#pfcch4pf1").blur(); $("#pfcch5pf1").blur(); } if (testItems.indexOf("Cable Seal Retention")!=-1) { //填充Cable Seal Retention数据 for (var i = 0; i < cableSealRetention_Rows.length; i++) { $("#ich1cabs"+(i+1)).val(cableSealRetention_Rows[i].拉力Force1); $("#ich2cabs"+(i+1)).val(cableSealRetention_Rows[i].拉力Force1); $("#ich3cabs"+(i+1)).val(cableSealRetention_Rows[i].拉力Force1); } //计算Cable Seal Retention结论 $("#ich1cabs1").blur(); $("#ich2cabs1").blur(); $("#ich3cabs1").blur(); } if (testItems.indexOf("Voltage Drop Version")!=-1) { //填充Voltage Drop数据 for (var i = 0; i < voltageDrop_Rows.length; i++) { $("input[name='voltagedropCCH1.t_value"+(i+1)+"']").val(voltageDrop_Rows[i].总电压降1); $("input[name='voltagedropCCH1.s_value"+(i+1)+"']").val(voltageDrop_Rows[i].焊接样线压降1); $("input[name='voltagedropCCH2.t_value"+(i+1)+"']").val(voltageDrop_Rows[i].总电压降2); $("input[name='voltagedropCCH2.s_value"+(i+1)+"']").val(voltageDrop_Rows[i].焊接样线压降2); $("input[name='voltagedropCCH3.t_value"+(i+1)+"']").val(voltageDrop_Rows[i].总电压降3); $("input[name='voltagedropCCH3.s_value"+(i+1)+"']").val(voltageDrop_Rows[i].焊接样线压降3); $("input[name='voltagedropCCH4.t_value"+(i+1)+"']").val(voltageDrop_Rows[i].总电压降4); $("input[name='voltagedropCCH4.s_value"+(i+1)+"']").val(voltageDrop_Rows[i].焊接样线压降4); $("input[name='voltagedropCCH5.t_value"+(i+1)+"']").val(voltageDrop_Rows[i].总电压降5); $("input[name='voltagedropCCH5.s_value"+(i+1)+"']").val(voltageDrop_Rows[i].焊接样线压降5); //计算Voltage Drop结论 $("input[name='voltagedropCCH1.t_value"+(i+1)+"']").blur(); $("input[name='voltagedropCCH2.t_value"+(i+1)+"']").blur(); $("input[name='voltagedropCCH3.t_value"+(i+1)+"']").blur(); $("input[name='voltagedropCCH4.t_value"+(i+1)+"']").blur(); $("input[name='voltagedropCCH5.t_value"+(i+1)+"']").blur(); } } if (testItems.indexOf("Current Cycling Version")!=-1) { //填充Electrical Current Cycling数据 for (var i = 0; i < electricalCurrentCycling_Rows.length; i++) { $("input[name='cchBeforeElec1.res"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验前压接电阻1); $("input[name='cchBeforeElec1.voltage"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验后压接电压降1); $("input[name='cchBeforeElec2.res"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验前压接电阻2); $("input[name='cchBeforeElec2.voltage"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验后压接电压降2); $("input[name='cchBeforeElec3.res"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验前压接电阻3); $("input[name='cchBeforeElec3.voltage"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验后压接电压降3); $("input[name='cchBeforeElec4.res"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验前压接电阻4); $("input[name='cchBeforeElec4.voltage"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验后压接电压降4); $("input[name='cchBeforeElec5.res"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验前压接电阻5); $("input[name='cchBeforeElec5.voltage"+(i+1)+"']").val(electricalCurrentCycling_Rows[i].试验后压接电压降5); } //计算Electrical Current Cycling结论 $("input[name='cchBeforeElec1.res1']").blur(); $("input[name='cchBeforeElec1.voltage1']").blur(); $("input[name='cchBeforeElec2.res1']").blur(); $("input[name='cchBeforeElec2.voltage1']").blur(); $("input[name='cchBeforeElec3.res1']").blur(); $("input[name='cchBeforeElec3.voltage1']").blur(); $("input[name='cchBeforeElec4.res1']").blur(); $("input[name='cchBeforeElec4.voltage1']").blur(); $("input[name='cchBeforeElec5.res1']").blur(); $("input[name='cchBeforeElec5.voltage1']").blur(); } if (testItems.indexOf("Voltage Drop ENV")!=-1) { //填充ENV数据 for (var i = 0; i < env_Rows.length; i++) { $("input[name='envCCH1.a_value"+(i+1)+"']").val(env_Rows[i].试验前压接电阻1); $("input[name='envCCH1.b_value"+(i+1)+"']").val(env_Rows[i].热冲击试验后压接电压降1); $("input[name='envCCH1.c_value"+(i+1)+"']").val(env_Rows[i].温湿度循环试验后压接电压降1); $("input[name='envCCH2.a_value"+(i+1)+"']").val(env_Rows[i].试验前压接电阻2); $("input[name='envCCH2.b_value"+(i+1)+"']").val(env_Rows[i].热冲击试验后压接电压降2); $("input[name='envCCH2.c_value"+(i+1)+"']").val(env_Rows[i].温湿度循环试验后压接电压降2); $("input[name='envCCH3.a_value"+(i+1)+"']").val(env_Rows[i].试验前压接电阻3); $("input[name='envCCH3.b_value"+(i+1)+"']").val(env_Rows[i].热冲击试验后压接电压降3); $("input[name='envCCH3.c_value"+(i+1)+"']").val(env_Rows[i].温湿度循环试验后压接电压降3); $("input[name='envCCH4.a_value"+(i+1)+"']").val(env_Rows[i].试验前压接电阻4); $("input[name='envCCH4.b_value"+(i+1)+"']").val(env_Rows[i].热冲击试验后压接电压降4); $("input[name='envCCH4.c_value"+(i+1)+"']").val(env_Rows[i].温湿度循环试验后压接电压降4); $("input[name='envCCH5.a_value"+(i+1)+"']").val(env_Rows[i].试验前压接电阻5); $("input[name='envCCH5.b_value"+(i+1)+"']").val(env_Rows[i].热冲击试验后压接电压降5); $("input[name='envCCH5.c_value"+(i+1)+"']").val(env_Rows[i].温湿度循环试验后压接电压降5); } //计算ENV结论 $("input[name='envCCH1.a_value1']").blur(); $("input[name='envCCH1.b_value1']").blur(); $("input[name='envCCH1.c_value1']").blur(); $("input[name='envCCH2.a_value1']").blur(); $("input[name='envCCH2.b_value1']").blur(); $("input[name='envCCH2.c_value1']").blur(); $("input[name='envCCH3.a_value1']").blur(); $("input[name='envCCH3.b_value1']").blur(); $("input[name='envCCH3.c_value1']").blur(); $("input[name='envCCH4.a_value1']").blur(); $("input[name='envCCH4.b_value1']").blur(); $("input[name='envCCH4.c_value1']").blur(); $("input[name='envCCH5.a_value1']").blur(); $("input[name='envCCH5.b_value1']").blur(); $("input[name='envCCH5.c_value1']").blur(); } //清除选中的file文件使可以重新选择 //$("#import_Excel").val(""); //禁用excel使不可以重新选择 //$("#import_Excel").attr("disabled",true); }; //手动触发fileReader的加载方法 fileReader.readAsBinaryString(excel); }

JS导入Excel实战_第4张图片

你可能感兴趣的:(JS导入Excel实战)