基于yaf框架和uploadify插件,做的一個(gè)導(dǎo)入excel文件,查看并保存數(shù)據(jù)的功能
思路:
1.首先,頁(yè)面前端,上傳附件,提交給后臺(tái),并帶一個(gè)隨機(jī)性的參數(shù)(可以用時(shí)間戳);
2.后端接收附件,做一系列的邏輯處理,無誤后,將對(duì)應(yīng)的文件存儲(chǔ)在上傳的目錄下;
3.然后前端,上傳附件成功后,進(jìn)行請(qǐng)求后端,讀取數(shù)據(jù),后端接口對(duì)應(yīng)將附件數(shù)據(jù)讀取出來,前端進(jìn)行顯示(ajax請(qǐng)求);
4.前端展示數(shù)據(jù),用戶對(duì)數(shù)據(jù)檢測(cè)無誤,點(diǎn)擊保存(ajax請(qǐng)求后端保存代碼的接口),當(dāng)然也可以有選擇性的選擇某些數(shù)據(jù)記錄進(jìn)行保存,樓主這里做的是全部保存(后端處理接口,自動(dòng)過濾重復(fù)數(shù)據(jù));
5.拿到對(duì)應(yīng)的所需有用數(shù)據(jù)即可, 對(duì)應(yīng)的excel表格,因?yàn)樾枰@取到人員排期數(shù)據(jù),所以樓主是通過判斷單元格的背景色來識(shí)別
代碼如下:(關(guān)鍵代碼)
前端 對(duì)應(yīng)html:
<!--導(dǎo)入數(shù)據(jù)操作層-->
<div id="import" class="modal fade bs-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-primary">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">文件導(dǎo)入</h4>
</div>
<div class="modal-body">
<div style="text-align:right;padding:5px">
<a href="/public/uploadFile/人員資源動(dòng)態(tài)匹配表-模板.xlsx" onclick="javascript:;">
<img alt="人員資源動(dòng)態(tài)匹配表-模板" src="/public/images/excel.jpg" />
<span style="font-size:larger;font-weight:200;color:red">人員資源動(dòng)態(tài)匹配表-模板.xlsx</span>
</a>
</div>
<hr/>
<form id="ffImport" method="post">
<div title="Excel導(dǎo)入操作" style="padding: 5px" data-options="iconCls:'icon-key'">
<input class="easyui-validatebox" type="hidden" id="AttachGUID" name="AttachGUID" />
<input id="file_upload" name="file_upload" type="file" multiple="multiple">
<a href="javascript:;" class="btn btn-primary" id="btnUpload" onclick="javascript: $('#file_upload').uploadify('upload', '*')">上傳</a>
<a href="javascript:;" class="btn btn-default" id="btnCancelUpload" onclick="javascript: $('#file_upload').uploadify('cancel', '*')">取消</a>
<div id="fileQueue" class="fileQueue"></div>
<br />
<hr style="width:98%" />
<div id="div_files"></div>
<br />
</div>
</form>
<!--數(shù)據(jù)顯示表格-->
<table id="gridImport" class="table table-striped table-bordered table-hover" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
<thead id="gridImport_head">
<tr>
<th>項(xiàng)目名稱</th>
<th>項(xiàng)目編號(hào)</th>
<th>功 能</th>
<th>人 員</th>
<th>日 期</th>
</tr>
</thead>
<tbody id="gridImport_body"></tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="close_window">關(guān)閉</button>
<button type="button" class="btn btn-primary" onclick="javascript:SaveImport();">保存</button>
</div>
</div>
</div>
</div>
對(duì)應(yīng)js代碼:
<script type="text/javascript">
//保存導(dǎo)入的數(shù)據(jù)
function SaveImport(){
var guid = $("#AttachGUID").val();
if (guid == '' || typeof guid == 'undefined') {
alert('請(qǐng)先上傳excel文件!');
return false;
}
$.ajax({
url: '/lazy/CheckExcelColumns?type=save&guid=' + guid,
type: 'get',
dataType: 'json',
success: function (data) {
alert(data.msg);
$('#close_window').click();
console.log('報(bào)存數(shù)據(jù)成功!');
},
error:function(){
console.log('出錯(cuò)了!');
}
});
}
$(function(){
//導(dǎo)入層的js
$("#import_schedule").bind('click', function(){
$("#gridImport_body").html("");
$("#import").modal("show");
});
//導(dǎo)入對(duì)應(yīng)的函數(shù)
$('#file_upload').uploadify({
'swf': '/public/uploadify/uploadify.swf', //FLash文件路徑
'buttonText': '瀏 覽', //按鈕文本
'uploader': '{{url("lazy/uploadExcel")}}', //后臺(tái)處理程序的路徑
'queueID': 'fileQueue', //隊(duì)列的ID
'queueSizeLimit': 1, //隊(duì)列最多可上傳文件數(shù)量,默認(rèn)為999
'auto': false, //選擇文件后是否自動(dòng)上傳,默認(rèn)為true
'multi': false, //是否為多選,默認(rèn)為true
'removeCompleted': true, //是否完成后移除序列,默認(rèn)為true
'fileSizeLimit': '10MB', //單個(gè)文件大小,0為無限制,可接受KB,MB,GB等單位的字符串值
'fileTypeDesc': 'Excel Files', //文件描述
'fileTypeExts': '*.xlsx', //上傳的文件后綴過濾器
'onQueueComplete': function (event, data) { //所有隊(duì)列完成后事件
//業(yè)務(wù)處理代碼
//提示用戶Excel格式是否正常,如果正常加載數(shù)據(jù)
var guid = $("#AttachGUID").val();
$.ajax({
url: '/lazy/CheckExcelColumns?type=check&guid=' + guid,
type: 'get',
dataType: 'json',
success: function (data) {
if (data.status) {
// InitGrid(); //重新刷新表格數(shù)據(jù)
$.each(data.rows, function (i, item) {
var tr = "<tr>";
tr += "<td>" + item['name']+ "</td>";
tr += "<td>" + item['identifier'] + "</td>";
tr += "<td>" + item['subject'] + "</td>";
tr += "<td>" + item['user'] + "</td>";
tr += "<td>" + item['getExcelTime'] + "</td>";
tr += "</tr>";
$("#gridImport_body").append(tr);
});
}else{
alert(data.msg);
}
}
});
},
'onUploadStart': function (file) {
InitUpFile(); //重置GUID(每次不同,用時(shí)間戳代替)
$("#gridImport_body").html("");
//動(dòng)態(tài)傳參數(shù)
var guid = $("#AttachGUID").val();
var salt = 'test' ; //md5加密輔助串
var token = hex_md5(salt+guid) ; //校驗(yàn)參數(shù)
$("#file_upload").uploadify(
"settings",
'formData', {
'folder': '數(shù)據(jù)導(dǎo)入excel文件',
'guid': guid,
'token':token,
}
);
},
'onUploadError': function (event, queueId, fileObj, errorObj) {
alert(errorObj.type + ":" + errorObj.info);
}
});
function InitUpFile(){
var timestamp = Date.parse(new Date());
$('#AttachGUID').val(timestamp);
}
</script>
后端代碼:
//上傳文件處理
public function uploadExcelAction()
{
$targetFolder = '/public/uploadFile/'; // Relative to the root
$salt = 'test';
$verifyToken = md5($test . $_POST['guid']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $verifyToken.'.xlsx';
$fileTypes = array('xlsx');
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}else{
echo 'Invalid params.';
}
die;
}
處理excel數(shù)據(jù),就說兩個(gè)關(guān)鍵點(diǎn):取單元格的值和背景色
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($targetFile);
$sheet = $objPHPExcel->getSheet();
$sheetRows = $sheet->getHighestDataRow(); // 取得總行數(shù)
$sheetColumns = PHPExcel_Cell::columnIndexFromString($sheet->getHighestDataColumn()); //列數(shù)
//讀取單元格
$value = $objPHPExcel->getActiveSheet()->getCell($columns[$k] . $j)->getValue(); //獲取每個(gè)單元格的值
$fillColor = $objPHPExcel->getActiveSheet()->getStyle($columns[$k] . $j)->getFill()->getStartColor()->getARGB(); //背景色
下面附圖:
導(dǎo)入界面:

excel表:

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- ThinkPHP、Zend?Framework2、Yaf、Laravel框架路由大比拼
- PHP擴(kuò)展框架之Yaf框架的安裝與使用
- PHP Yaf框架的簡(jiǎn)單安裝使用教程(推薦)
- 詳解php框架Yaf路由重寫
- yii,CI,yaf框架+smarty模板使用方法
- 在PHP 7下安裝Swoole與Yar,Yaf的方法教程
- ubuntu14.04LTS安裝nginx+mariaDB+php7+YAF的方法
- PHP7.1安裝yaf擴(kuò)展的方法
- Ubuntu上安裝yaf擴(kuò)展的方法
- 詳解Yaf框架PHPUnit集成測(cè)試方法
- Yaf框架封裝的MySQL數(shù)據(jù)庫(kù)操作示例
相關(guān)文章
php加密之discuz內(nèi)容經(jīng)典加密方式實(shí)例詳解
這篇文章主要介紹了php加密之discuz內(nèi)容經(jīng)典加密方式,結(jié)合具體實(shí)例形式詳細(xì)分析了discuz加密的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
php以fastCGI的方式運(yùn)行時(shí)文件系統(tǒng)權(quán)限問題及解決方法
在IIS7.0上以FastCGI方式配置好PHP運(yùn)行環(huán)境,測(cè)試可以正常運(yùn)行PHP程序后,將PHP程序部署上去,導(dǎo)入程序原來的數(shù)據(jù)和配置信息。很快就有問題出來啦下面我們就詳細(xì)記錄下。2015-05-05
php中使用接口實(shí)現(xiàn)工廠設(shè)計(jì)模式的代碼
php實(shí)現(xiàn)工廠設(shè)計(jì)模式,使用接口實(shí)現(xiàn),表面上接口沒有什么用,因?yàn)閜hp是類型自動(dòng)轉(zhuǎn)換的。實(shí)現(xiàn)上使用接口可以約束類的定義,從而實(shí)現(xiàn)一致的訪問2012-06-06
php intval的測(cè)試代碼發(fā)現(xiàn)問題
測(cè)試php intval函數(shù)的代碼:2008-07-07
php對(duì)大文件進(jìn)行讀取操作的實(shí)現(xiàn)代碼
在php中,對(duì)于文件的讀取時(shí),最快捷的方式莫過于使用一些諸如file、file_get_contents之類的函數(shù),簡(jiǎn)簡(jiǎn)單單的幾行代碼就能很漂亮的完成我們所需要的功能。但當(dāng)所操作的文件是一個(gè)比較大的文件時(shí),這些函數(shù)可能就顯的力不從心, 下面將從一個(gè)需求入手來說明對(duì)于讀取大文件時(shí),常用的操作方法2013-01-01
PHP中國(guó)際化的字符串排序和比較對(duì)象詳解
這篇文章主要給大家介紹了關(guān)于PHP中國(guó)際化字符串排序和比較對(duì)象的相關(guān)資料,這在日常開發(fā)中經(jīng)常會(huì)用到,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

