1 回答

TA貢獻1909條經驗 獲得超7個贊
好消息:我有答案解析響應表與 PHPExcel 導入 Excel 文件以輸入標簽(例如:自動填充輸入 id=“fname” 到 Hello 和輸入 id=“lname” 到 World)用我的完整源代碼。
我有鏈接的答案。
excelimport.xlsx
(Excel文件)
excelimport.php
(PHP 代碼)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
h2 {display: inline;}
</style>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="myform1" id="myform1">
<h2 for="myfile1">Select files : </h2><input type="file" name="excelFile" id="excelFile" /><br><br>
<h2 for="fname">First name : </h2><input type="text" id="fname" name="fname"><br><br>
<h2 for="lname">Last name : </h2><input type="text" id="lname" name="lname"><br><br>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
// ????????????????????? evnet submit ??????
$("#excelFile").on("change",function(e){
e.preventDefault(); // ???????????? submit ???? ??????????????? ajax
// ???????????? form ????????????? FormData Object
var formData = new FormData($("#myform1")[0]);
// ????????? POST ????????? read_excel.php ?????? ajax ???????
$.ajax({
url: 'read_excel.php',
type: 'POST',
data: formData,
/*async: false,*/
cache: false,
contentType: false,
processData: false
}).done(function(data){
console.log(data); // ???????????? ?????????? console
/* ????????? console log ????? debug javascript ?? chrome firefox ??? ie
http://www.ninenik.com/content.php?arti_id=692 via @ninenik */
$("#fname").val(data.A2);
$("#lname").val(data.B2);
});
});
});
</script>
</body>
</html>
read_excel.php(PHP 代碼)
<?php
header("Content-type:application/json; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Bangkok');
// http://php.net/manual/en/timezones.php
require_once("PHPExcel/Classes/PHPExcel.php");
?>
<?php
if(isset($_FILES['excelFile']['name']) && $_FILES['excelFile']['name']!=""){
$tmpFile = $_FILES['excelFile']['tmp_name'];
$fileName = $_FILES['excelFile']['name']; // ????????????
$_fileup = $_FILES['excelFile'];
$info = pathinfo($fileName);
$allow_file = array("csv","xls","xlsx");
/* print_r($info); // ??????????
print_r($_fileup);*/
if($fileName!="" && in_array($info['extension'],$allow_file)){
// ??????????? path temp ?????????????????????
$objPHPExcel = PHPExcel_IOFactory::load($tmpFile);
// ???????????????????????????????????????????????????? array
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
// ???????????????
$v=1;
$json_data = array();
foreach ($cell_collection as $cell) {
// ???????????????????????????? ???? A B C ....
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
// ??????????????????????????????? ???? 1 2 3 .....
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
// ???????????????????????? ???? A1 B1 C1 ....
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
// ????????????????????????????????????????????????? ?????????????????????????
// ?????????????????????? ??????????????
$json_data["$column$row"] = $data_value;
// echo $v." ---- ".$data_value."<br>";
$v++;
}
// ???? array ?????????? json string
if(isset($json_data)){
$json= json_encode($json_data);
if(isset($_GET['callback']) && $_GET['callback']!=""){
echo $_GET['callback']."(".$json.");";
}else{
echo $json;
}
}
}
}
?>
我使用 PHPExcel 庫通過鏈接下載。
- 1 回答
- 0 關注
- 92 瀏覽
添加回答
舉報