excel导入
<extend name="Public/base"/>
<block name="page-title">导入试题<small></small></block>
<block name="body">
<div class="panel">
<div class="panel-operate">
<div class="row">
<div class="col-md-12">
<button class="btn btn-default" type="button" onclick="doUpload();" id="btnRun">开始导入</button>
<button class="btn btn-default" type="button" onclick="downLoadExcel();" id="btnRun">下载模板</button>
</div>
</div>
</div>
<div class="panel-body">
<div class="row">
<form id="uploadForm">
<div class="col-md-6">
<div class="form-group">
<label class="item-label"></label>
<div class="controls">
<input type="file" name="examExcel" accept=".xls,.xlsx"/>
</div>
</div>
<div class="form-group">
<label class="item-label">导入进度</label>
<div id="div_count">
</div>
<div class="progress">
<div>
</div>
<div class="form-group">
<label class="item-label">执行日志</label>
<div id="div_error">
</div>
</div>
</form>
</div>
</div>
</div>
</block>
<block name="script">
<script type="text/javascript">
var rowCount = 0; //数据总行数
var startIndex = 0; //数据导入时的开始项
var size = 10; //数据每一次进行数据导入的个数
var errorCount = 0; //导入数据时失败的个数
var rightCount = 0; //导入数据时成功的个数
var repeat = 0; //导入数据时重复的个数
function doUpload() {
var formData = new FormData($("#uploadForm")[0]); $.ajax({ url: "{:U('Exam/uploadExcel')}", type: 'POST', data: formData, dataType: "json", async: false, cache: false, contentType: false, processData: false, success: function (result) { if (result.status == 1) { var allpath = result.allpath; var dataCount = result.dataCount; var md5Path = result.md5path; //console.info(result); runImport(allpath, md5Path, dataCount); } else { alert(result.msg); } }, error: function (data) { console.info(data); alert(data.msg); } }); } function runImport(allPath, md5Path, dataCount) { // console.info(allPath); $("#btnRun").attr("disabled", true); //修改"导入数据"按钮是否可用的属性 $("#btnRun").attr("value", "正在导入"); //修改"导入数据"按钮显示的属性 rowCount = dataCount; //获得数据总行数 size = Math.ceil(parseFloat(rowCount) / 20.00); //计算出数据每一次应该执行的个数 $("#div_count").html(''); $("#div_error").html(''); $("#div_count").append("共读取到" + rowCount + "条数据 "); $("#div_error").append((nowdata()).toString() + " 共读取到" + rowCount + "条数据!<br/><br/>准备导入……<br/><br/>正在导入……<br/><br/>"); insertData(allPath, md5Path); } //获取执行的时间 function nowdata() { var now = new Date(); var year = now.getYear(); var month = now.getMonth() + 1; var day = now.getDate(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); return nowdate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; } //开始导入数据 function insertData(allPath, md5Path) { // console.info(startIndex); // console.info(rowCount); // console.info('insertData' + allPath); if (startIndex < rowCount) { //"allPath": allPath, $.getJSON("{:U('Exam/inputData')}" + "&tspan=" + md5Path, {"md5Path": md5Path, "startIndex": startIndex, "size": size}, function (data) { //处理data的值(成功个数和失败个数) console.info(data); errorCount += parseInt(data.errorCount); //修改总共失败的个数 rightCount += parseInt(data.rightCount); //修改总共成功的个数 repeat += parseInt(data.repeat); //修改总共重复的个数 //计算滚动条宽度 var width = (parseFloat(rightCount + errorCount + repeat) / parseFloat(rowCount)) * $(".progress").width(); $(".progress div").css("width", width); $("#div_count").html("共" + rowCount + "条,成功" + rightCount + "条,失败" + errorCount + "条,重复" + repeat + "条"); if (data.errorCount > 0) { $("#div_error").append($.sp.format("导入失败,请检查Excel文件中的第{0}行至第{1}行!<br/>", startIndex + 2, startIndex + parseInt(data.errorCount) + 2)); } if (data.repeat > 0) { $("#div_error").append($.sp.format((nowdata()).toString() + "检测到Excel文件中的第{0}行至第{1}行之间存在{2}条重复!<br/>", startIndex + 2, startIndex + parseInt(data.errorCount) + parseInt(data.rightCount) + parseInt(data.repeat) + 2, data.repeat)); } startIndex += size; setTimeout("insertData('" + allPath + "','" + md5Path + "')", 100); //用延迟方法执行导入数据 }); } else { $("#div_error").append("导入完成!"); $("#btnRun").attr("disabled", false); $("#btnRun").attr("value", "导入数据"); rowCount = 0; //数据总行数 startIndex = 0; //数据导入时的开始项 size = 10; //数据每一次进行数据导入的个数 errorCount = 0; //导入数据时失败的个数 rightCount = 0; //导入数据时成功的个数 repeat = 0; //导入数据时重复的个数 alert("导入完成"); } } function downLoadExcel(){ location.href="/Uploads/Excels/试题导入模板.xls"; } </script> </block>